您的位置:首页 > Web前端 > JavaScript

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:新浪就不敢增加个贴代码模块。。。要不是在新浪开博早,铁定换地方
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: