📄 viewdao.java
字号:
/*
* @(#)ViewDAO.java
*
* 创建日期 2005-4-21
* Copyright 2005 东软 国际合作事业部. All rights reserved.
*/
package liyong.model;
import liyong.form.View;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.ArrayList;
import zhangchunliang.model.AppMode;
/**
* 这个类用来处理ViewAction中动作事件需要操作的数据库,
* 包过添加,删除,更新,查询等
* 它将获得ViewAction类传递过来的要处理的参数
* 并处理分页操作
*
* @see View
* @see ViewAction
* @see ViewForm
* @see Connection
* @author 李勇
*/
public class ViewDAO {
/** 建立Connection 对象 */
private Connection con;
/** 总的行数 */
private int rowCount;
/** 总的页面数 */
private int pageCount;
/** 要显示的行数 */
private int length;
/** 分页字符串,分页内容 */
private String pagestr;
/** 得到要显示的行数 */
public int getLength() {
return (this.length);
}
/** 设定要显示的行数 */
public void setLength(int length) {
this.length = length;
}
/** 获得分页字符串,分页内容 */
public String getPagestr(int ipage) {
/** 分页字符串要显示的内容 */
String strPage = "";
if (getLength() > 0) {
strPage += "共";
strPage += String.valueOf(rowCount);
strPage += "条记录,共";
strPage += String.valueOf(pageCount);
strPage += "页,当前是第";
strPage += String.valueOf(ipage);
strPage += "页, ";
/** 设定页面显示的第一条记录。和最后一条记录 */
int istart, iend;
istart = ipage - 5;
if (istart < 0) {
istart = 0;
}
iend = istart + 10;
if (iend > pageCount) {
iend = pageCount;
}
istart = iend - 10;
if (istart < 0) {
istart = 0;
}
for (int i = istart; i < iend; i++) {
strPage += "<a href='ViewAction.do?action=find&search=search&"
+ "page=";
strPage += String.valueOf(i + 1);
strPage += "'>";
strPage += String.valueOf(i + 1);
strPage += "</a>";
strPage += " ";
}
}
this.pagestr = strPage;
return strPage;
}
/** 构造函数开始初始化,通过构造函数连接连接池 */
public ViewDAO(Connection con) {
this.con = con;
}
/** 这个方法是用来添加新记录的 */
public void create(View m_View) throws SQLException {
AppMode.registerUser();
/** 建立Statement对象,以后用来执行SQL语句 */
Statement stmt = null;
try {
/** 判断数据库连接是不是正常,不正常的话抛出异常 */
if (con.isClosed()) {
throw new IllegalStateException("没有连接到数据库");
}
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
/** 执行SQL命令,添加新记录到flight表 */
stmt.executeUpdate("insert into flight (fno,fcountry,fcom,"
+ "iout,fcity) values ('" + m_View.getFno() + "','"
+ m_View.getFcountry() + "','" + m_View.getFcom() + "','"
+ m_View.getIout() + "','" + m_View.getFcity() + "')");
/**
* 执行SQL命令,添加新记录到city表 这个代表的是添加航班,
* 它的航班号和经过的第一个城市及出发时间
*/
stmt.executeUpdate("insert into city (fno,cityname,gtime)"
+ " values ('" + m_View.getFno() + "','"
+ m_View.getFcity1() + "','" + m_View.getText1() + "')");
/**
* 判断航班是否是直达的是不是经过一个城市中转的
* 如果是经过一个城市中传的话,
* 就加入经过该城市的航班的航班号
* 城市名称和降落时间,起飞时间
*/
if (m_View.getText3() != null)
stmt.executeUpdate("insert into city (fno,cityname,ctime,"
+ "gtime) " + "values ('" + m_View.getFno() + "','"
+ m_View.getFcity2() + "','" + m_View.getText2()
+ "','" + m_View.getText3() + "')");
/**
* 不经过中传的话就直接添加航班到达目的地的航班号 目的地名,和降落时间
*
*/
else
stmt
.executeUpdate("insert into city (fno,cityname,ctime)"
+ " values ('" + m_View.getFno() + "','"
+ m_View.getFcity2() + "','"
+ m_View.getText2() + "')");
/** 判断航班是否经过第二个中转城市 */
if (m_View.getFcity3() != null) {
/**
* 如果是经过第二个中转城市则加入第二个城市的
* 航班号,城市名,
* 和降落,起飞时间
*/
if (m_View.getText5() != null)
stmt.executeUpdate("insert into city (fno,cityname,"
+ "ctime,gtime) " + "values ('" + m_View.getFno()
+ "','" + m_View.getFcity3() + "','"
+ m_View.getText4() + "','" + m_View.getText5()
+ "')");
/**
* 不经过的话,则该城市就是目的地城市
* 则加入经过该城市的航班号,城市名和降落时间
*/
else
stmt.executeUpdate("insert into city (fno,cityname,"
+ "ctime) values ('" + m_View.getFno() + "','"
+ m_View.getFcity3() + "','" + m_View.getText4()
+ "')");
}
/** 判断航班是否经过第三个中转城市 */
if (m_View.getFcity4() != null)
stmt
.executeUpdate("insert into city (fno,cityname,ctime"
+ ") values ('" + m_View.getFno() + "','"
+ m_View.getFcity4() + "','"
+ m_View.getText6() + "')");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected ");
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (stmt != null)
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected ");
}
}
}
/** 该方法是更新数据库更新数据库记录 */
public void update(View m_View, String keyID) {
AppMode.registerUser();
/** 建立Statement对象,以执行SQL命令 */
Statement st = null;
try {
/** 判断数据库连接是不是正常,不正常的话抛出异常 */
if (con.isClosed()) {
throw new IllegalStateException("没有连接到数据库");
}
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
/** 执行SQL命令,修改flight表记录 */
st.executeUpdate("UpDate flight set fno='" + m_View.getFno()
+ "',fcountry='" + m_View.getFcountry() + "',fcom='"
+ m_View.getFcom() + "',iout='" + m_View.getIout()
+ "' where fno='" + m_View.getFno() + "'");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -