JSP:航班信息查询模块加强版(附工…
2013-02-22 15:45
190 查看
说明:
1.基于三层架构,oracle 10g,JSP
2.增加了分页和部分异常处理
3.通过sql拼接实现信息筛选而非setObject()
4.数据库访问通过数据池做的
项目结构图:
说明:biz业务逻辑层 entity实体类
dao:数据访问对象
dao.imple实现类
代码:
实体类:AirInfo.java
public class AirInfo {
private int aid;
private String takeOff;
private String destination;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getTakeOff() {
return takeOff;
}
public void setTakeOff(String takeOff) {
this.takeOff = takeOff;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}
=============================================================================
数据访问类:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BaseDao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public Connection getConnection(){
try {
Context ctx = new InitialContext();
DataSource ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/scott");
conn = ds.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void closeAll(Connection conn,PreparedStatement
pstmt,ResultSet rs){
try {
if(null!=rs)
rs.close();
if(null!=pstmt)
pstmt.close();
if(null!=conn)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet executeQuerySQL(String sql,String []
params){
getConnection();
try {
pstmt = conn.prepareStatement(sql);
if(null!=params){
for(int i=0;i<params.length;i++){
pstmt.setString(i+1, params[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public int executeUpdateSQL(String sql,String[] params){
getConnection();
int result = 0;
try {
pstmt = conn.prepareStatement(sql);
if(null!=params){
for(int i=0;i<params.length;i++){
pstmt.setString(i+1, params[i]);
}
}
result = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn,pstmt,rs);
}
return result;
}
}
=============================================================================
实现类:
public class AirDaoOracleImpl extends BaseDao implements
AirDao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public List<AirInfo>
getAirInfo(int pageIndex, int pageSize,String dest,String
departure) {
List<AirInfo> list = new
ArrayList<AirInfo>();
//注意字符串拼接和单引号的添加以及to_date()
String sql = "SELECT * FROM (SELECT ROWNUM as r,t.* FROM
(SELECT * FROM AIRINFO WHERE 1=1 ";
if(null!=dest&&""!=dest){
sql += " AND destination='"+dest+"'";
}
if(null!=departure&&""!=departure){
sql += " AND
takeoff=TO_DATE('"+departure+"','yyyy-mm-dd')";
}
sql += " ORDER BY aid) t WHERE
ROWNUM<="+pageSize*pageIndex+") WHERE
r>"+pageSize*(pageIndex-1);
rs = executeQuerySQL(sql, null);
try {
while(rs.next()){
AirInfo ai = new AirInfo();
ai.setAid(rs.getInt("aid"));
ai.setTakeOff(rs.getString("takeoff").substring(0,11));
ai.setDestination(rs.getString("destination"));
list.add(ai);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn, pstmt, rs);
}
return list;
}
public int getAirCount(String dest,String departure) {
int count = 0;
String sql = "SELECT COUNT(*) FROM AIRINFO WHERE 1=1";
if(null!=dest&&""!=dest){
sql += " AND destination='"+dest+"'";
}
if(null!=departure&&""!=departure){
sql += " AND
takeoff=TO_DATE('"+departure+"','yyyy-mm-dd')";
}
rs = executeQuerySQL(sql, null);
try {
if(rs.next())
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn, pstmt, rs);
}
return count;
}
}
=========================================================================
业务层
public class AirBiz {
public List<AirInfo>
showAirInfo(int pageIndex,int pageSize,String dest,String
departure){
AirDao aDao = new AirDaoOracleImpl();
List<AirInfo> list
=aDao.getAirInfo(pageIndex, pageSize, dest, departure);
return list;
}
public int getCount(String dest,String departure){
AirDao aDao = new AirDaoOracleImpl();
int count = aDao.getAirCount(dest, departure);
return count;
}
}
==============================================================================
分页控制:
public class PageControl {
public int getTotalPage(int count,int pageSize){
int total =
(count%pageSize==0)?(count/pageSize):(count/pageSize+1);
return total;
}
}
==============================================================================
首页:
<%@ page language="java" import="java.util.*"
pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>查询页面</title>
<script
type="text/javascript">
function
checkTakeOff(){
var takeoff =
document.getElementByIdx_x_x_x("to").value;
var reg =
/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
if(!reg.test(takeoff)){
alert("日期格式错误,格式应为:yyyy-mm-dd");
return false;
}
return true;
}
</script>
</head>
<body>
<form method="post"
action="airList.jsp" onsubmit="return
checkTakeOff()">
<table
width="780px">
<tr>
<td
width="20%">根据目的地查询</td>
<td><input type="text"
name="destination" id="dest"
/></td>
</tr>
<tr>
<td
width="20%">根据起飞时间查询</td>
<td><input type="text"
name="takeoff" id="to"
/>yyyy-mm-dd</td>
</tr>
<tr>
<td
colspan="2"><input type="submit"
value="查询"
/></td>
</tr>
</table>
</form>
</body>
</html>
========================================================================================
信息显示页(注意"首页,上一页,下一页,末页的实现"):
<%@ page language="java" import="java.util.*"
pageEncoding="GBK"%>
<%@ page import="biz.*"%>
<%@ page
import="entity.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>航班信息</title>
<%
request.setCharacterEncoding("gbk");
String dest = request.getParameter("destination");
String departure = request.getParameter("takeoff");
String currentPage = request.getParameter("pageIndex");
if(null==currentPage){
currentPage = "1";
}
int pageIndex = Integer.parseInt(currentPage);
AirBiz ab = new AirBiz();
int count = ab.getCount(dest,departure);
int pageSize = 3;
PageControl pc = new PageControl();
int totalPage = pc.getTotalPage(count,pageSize);
if(pageIndex<1){
pageIndex = 1;
}
if(pageIndex>totalPage){
pageIndex = totalPage;
}
List<AirInfo> list =
ab.showAirInfo(pageIndex,pageSize,dest,departure);
%>
</head>
<body>
<table
width="780px">
<tr>
<td
width="20%">航班号</td>
<td
width="20%">起飞时间</td>
<td
width="60%">目的地</td>
</tr>
<%
for(AirInfo ai : list){ %>
<tr>
<td
width="20%"><%=ai.getAid()
%></td>
<td
width="20%"><%=ai.getTakeOff()
%></td>
<td
width="60%"><%=ai.getDestination()
%></td>
</tr>
<%
}
%>
</table>
<a
href="airList.jsp?pageIndex=1&destination=<%=dest
%>&takeoff=<%=departure
%>">首页</a>
<a
href="airList.jsp?pageIndex=<%=pageIndex-1
%>&destination=<%=dest
%>&takeoff=<%=departure
%>">上一页</a>
<a
href="airList.jsp?pageIndex=<%=pageIndex+1
%>&destination=<%=dest
%>&takeoff=<%=departure
%>">下一页</a>
<a
href="airList.jsp?pageIndex=<%=totalPage
%>&destination=<%=dest
%>&takeoff=<%=departure
%>">末页</a>
总共<%=totalPage
%>页<br>
<a
href="index.jsp">返回</a>
</body>
</html>
工程项目
数据库表文件
PS:新浪就不敢增加个贴代码模块。。。要不是在新浪开博早,铁定换地方
1.基于三层架构,oracle 10g,JSP
2.增加了分页和部分异常处理
3.通过sql拼接实现信息筛选而非setObject()
4.数据库访问通过数据池做的
项目结构图:
说明:biz业务逻辑层 entity实体类
dao:数据访问对象
dao.imple实现类
代码:
实体类:AirInfo.java
public class AirInfo {
private int aid;
private String takeOff;
private String destination;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getTakeOff() {
return takeOff;
}
public void setTakeOff(String takeOff) {
this.takeOff = takeOff;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}
=============================================================================
数据访问类:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BaseDao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public Connection getConnection(){
try {
Context ctx = new InitialContext();
DataSource ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/scott");
conn = ds.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void closeAll(Connection conn,PreparedStatement
pstmt,ResultSet rs){
try {
if(null!=rs)
rs.close();
if(null!=pstmt)
pstmt.close();
if(null!=conn)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet executeQuerySQL(String sql,String []
params){
getConnection();
try {
pstmt = conn.prepareStatement(sql);
if(null!=params){
for(int i=0;i<params.length;i++){
pstmt.setString(i+1, params[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public int executeUpdateSQL(String sql,String[] params){
getConnection();
int result = 0;
try {
pstmt = conn.prepareStatement(sql);
if(null!=params){
for(int i=0;i<params.length;i++){
pstmt.setString(i+1, params[i]);
}
}
result = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn,pstmt,rs);
}
return result;
}
}
=============================================================================
实现类:
public class AirDaoOracleImpl extends BaseDao implements
AirDao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public List<AirInfo>
getAirInfo(int pageIndex, int pageSize,String dest,String
departure) {
List<AirInfo> list = new
ArrayList<AirInfo>();
//注意字符串拼接和单引号的添加以及to_date()
String sql = "SELECT * FROM (SELECT ROWNUM as r,t.* FROM
(SELECT * FROM AIRINFO WHERE 1=1 ";
if(null!=dest&&""!=dest){
sql += " AND destination='"+dest+"'";
}
if(null!=departure&&""!=departure){
sql += " AND
takeoff=TO_DATE('"+departure+"','yyyy-mm-dd')";
}
sql += " ORDER BY aid) t WHERE
ROWNUM<="+pageSize*pageIndex+") WHERE
r>"+pageSize*(pageIndex-1);
rs = executeQuerySQL(sql, null);
try {
while(rs.next()){
AirInfo ai = new AirInfo();
ai.setAid(rs.getInt("aid"));
ai.setTakeOff(rs.getString("takeoff").substring(0,11));
ai.setDestination(rs.getString("destination"));
list.add(ai);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn, pstmt, rs);
}
return list;
}
public int getAirCount(String dest,String departure) {
int count = 0;
String sql = "SELECT COUNT(*) FROM AIRINFO WHERE 1=1";
if(null!=dest&&""!=dest){
sql += " AND destination='"+dest+"'";
}
if(null!=departure&&""!=departure){
sql += " AND
takeoff=TO_DATE('"+departure+"','yyyy-mm-dd')";
}
rs = executeQuerySQL(sql, null);
try {
if(rs.next())
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn, pstmt, rs);
}
return count;
}
}
=========================================================================
业务层
public class AirBiz {
public List<AirInfo>
showAirInfo(int pageIndex,int pageSize,String dest,String
departure){
AirDao aDao = new AirDaoOracleImpl();
List<AirInfo> list
=aDao.getAirInfo(pageIndex, pageSize, dest, departure);
return list;
}
public int getCount(String dest,String departure){
AirDao aDao = new AirDaoOracleImpl();
int count = aDao.getAirCount(dest, departure);
return count;
}
}
==============================================================================
分页控制:
public class PageControl {
public int getTotalPage(int count,int pageSize){
int total =
(count%pageSize==0)?(count/pageSize):(count/pageSize+1);
return total;
}
}
==============================================================================
首页:
<%@ page language="java" import="java.util.*"
pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>查询页面</title>
<script
type="text/javascript">
function
checkTakeOff(){
var takeoff =
document.getElementByIdx_x_x_x("to").value;
var reg =
/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
if(!reg.test(takeoff)){
alert("日期格式错误,格式应为:yyyy-mm-dd");
return false;
}
return true;
}
</script>
</head>
<body>
<form method="post"
action="airList.jsp" onsubmit="return
checkTakeOff()">
<table
width="780px">
<tr>
<td
width="20%">根据目的地查询</td>
<td><input type="text"
name="destination" id="dest"
/></td>
</tr>
<tr>
<td
width="20%">根据起飞时间查询</td>
<td><input type="text"
name="takeoff" id="to"
/>yyyy-mm-dd</td>
</tr>
<tr>
<td
colspan="2"><input type="submit"
value="查询"
/></td>
</tr>
</table>
</form>
</body>
</html>
========================================================================================
信息显示页(注意"首页,上一页,下一页,末页的实现"):
<%@ page language="java" import="java.util.*"
pageEncoding="GBK"%>
<%@ page import="biz.*"%>
<%@ page
import="entity.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>航班信息</title>
<%
request.setCharacterEncoding("gbk");
String dest = request.getParameter("destination");
String departure = request.getParameter("takeoff");
String currentPage = request.getParameter("pageIndex");
if(null==currentPage){
currentPage = "1";
}
int pageIndex = Integer.parseInt(currentPage);
AirBiz ab = new AirBiz();
int count = ab.getCount(dest,departure);
int pageSize = 3;
PageControl pc = new PageControl();
int totalPage = pc.getTotalPage(count,pageSize);
if(pageIndex<1){
pageIndex = 1;
}
if(pageIndex>totalPage){
pageIndex = totalPage;
}
List<AirInfo> list =
ab.showAirInfo(pageIndex,pageSize,dest,departure);
%>
</head>
<body>
<table
width="780px">
<tr>
<td
width="20%">航班号</td>
<td
width="20%">起飞时间</td>
<td
width="60%">目的地</td>
</tr>
<%
for(AirInfo ai : list){ %>
<tr>
<td
width="20%"><%=ai.getAid()
%></td>
<td
width="20%"><%=ai.getTakeOff()
%></td>
<td
width="60%"><%=ai.getDestination()
%></td>
</tr>
<%
}
%>
</table>
<a
href="airList.jsp?pageIndex=1&destination=<%=dest
%>&takeoff=<%=departure
%>">首页</a>
<a
href="airList.jsp?pageIndex=<%=pageIndex-1
%>&destination=<%=dest
%>&takeoff=<%=departure
%>">上一页</a>
<a
href="airList.jsp?pageIndex=<%=pageIndex+1
%>&destination=<%=dest
%>&takeoff=<%=departure
%>">下一页</a>
<a
href="airList.jsp?pageIndex=<%=totalPage
%>&destination=<%=dest
%>&takeoff=<%=departure
%>">末页</a>
总共<%=totalPage
%>页<br>
<a
href="index.jsp">返回</a>
</body>
</html>
工程项目
数据库表文件
PS:新浪就不敢增加个贴代码模块。。。要不是在新浪开博早,铁定换地方
相关文章推荐
- jsp查询所有图书信息
- 通达OA二次开发 对通达2015版微信查询用户信息模块升级开发(图文)
- jsp+servlet学生信息查询系统小结
- JSP_用户登陆程序(通过tomcat在mysql数据库中查询用户信息监测)
- Android应用源码列车车次与航班信息查询
- 航班信息的查询与检索(C语言版)
- 航班信息检索与查询(基数排序)
- Goods:按bid查询返回desc.jsp页面详细信息
- 05.HTTP编程实战:高校信息查询平台之登录模块
- 7.以新版表格型单表为模板定制模块:人员信息查询
- 基于jsp+servlet图书管理系统之后台用户信息查询操作
- 06.HTTP编程实战:高校信息查询平台之注册模块
- Tomcat+JSP+Oracle信息查询系统开发笔记(1)
- Servlet+JSP+MySQL实现用户管理模块之六、实现用户信息显示
- 基于C语言航班信息查询与检索
- [Java] 用泛型数组实现基于控制台的航班信息查询|增添程序
- 航班信息查询和检索系统
- ###Jsp+Servlet购物商城day04.2:订单模块难点总结。【购物商城最大的难点:【封装一个Order对象】查询封装三张表】
- C#--WinForm查询模块--按学号精确查询学员信息
- 课程设计 航班信息的查询与检索