📄 linkdao.java
字号:
/*
* @(#)DelModifyDAO.java
*
* 创建日期 2005-4-21
* Copyright 2005 东软 国际合作事业部. All rights reserved.
*/
package liyong.model;
import liyong.form.*;
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;
/**
* 这个类用来处理LinkAction中动作事件需要操作的数据库,
* 包过添加,删除,更新,查询等
* 它将获得LinkAction类传递过来的要处理的参数
* 并处理分页的操作
*
* @see View
* @see LinkAction
* @see ViewForm
* @see Connection
* @author 李勇
*/
public class LinkDAO {
/** 建立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='LinkAction.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 LinkDAO(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() + "')");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected 2");
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (stmt != null)
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected 3");
}
}
}
/** 该方法是更新数据库更新数据库记录 */
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命令,修改city表记录 */
st.executeUpdate("UpDate city set fno='" + m_View.getFno()
+ "',cityname='" + m_View.getCityname() + "',ctime='"
+ m_View.getCtime() + "',gtime='" + m_View.getGtime()
+ "' where fno='" + m_View.getFno() + "' and cityname='"
+ m_View.getCityname() + "'");
} 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");
}
}
}
/** 该方法是删除记录 */
public void remove(String sql) {
AppMode.registerUser();
PreparedStatement ps = null;
try {
/** 判断数据库连接是不是正常,不正常的话抛出异常 */
if (con.isClosed()) {
throw new IllegalStateException("没有连接到数据库");
}
ps = con.prepareStatement(sql);
if (ps.executeUpdate() != 1) {
throw new SQLException("error.removed.View");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
/** 处理指定关键字的flight表删除操作 */
public void removeID(String keyID) {
AppMode.registerUser();
/** 要操作的sql语句 */
String sql = "DELETE FROM flight WHERE ";
sql += "fno";
sql += " = ?";
PreparedStatement ps = null;
try {
/** 判断数据库连接是不是正常,不正常的话抛出异常 */
if (con.isClosed()) {
throw new IllegalStateException("没有连接到数据库");
}
ps = con.prepareStatement(sql);
/** 按照关键字删除数据 */
ps.setString(1, keyID.trim());
if (ps.executeUpdate() != 1) {
throw new SQLException("error.removed.View");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
/** 查询指定关键字的记录 */
public View findByPrimaryKey(String keyID) throws SQLException {
AppMode.registerUser();
/** 建立PreparedStatement对象,以执行SQL命令 */
PreparedStatement ps = null;
ResultSet rs = null;
View m_View = null;
/** 要操作的sql语句 */
String sql = "SELECT fno,cityname,ctime,gtime from city where fno=?";
try {
/** 判断数据库连接是不是正常,不正常的话抛出异常 */
if (con.isClosed()) {
throw new IllegalStateException("没有连接到数据库");
}
ps = con.prepareStatement(sql);
ps.setString(1, keyID.trim());
rs = ps.executeQuery();
if (rs.next()) {
m_View = new View();
/** 将关键字查询到的记录赋给各个字段 */
m_View.setFno(rs.getString(1));
m_View.setCityname(rs.getString(2));
m_View.setCtime(rs.getString(3));
m_View.setGtime(rs.getString(4));
return m_View;
} else {
throw new SQLException("error.removed.View");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
/** 处理不是一个结果集的查询,返回多条记录 */
public Collection findSQL(String sql, int ipage) {
AppMode.registerUser();
/** 建立Statement对象,以执行SQL命令 */
Statement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
/** 判断数据库连接是不是正常,不正常的话抛出异常 */
if (con.isClosed()) {
throw new IllegalStateException("没有连接到数据库");
}
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
/** 执行SQL命令得到记录 */
rs = stmt.executeQuery(sql);
rs.absolute(-1);
/** 得到查询后的行数 */
rowCount = rs.getRow();
int offset = 1;
int pagesize = getLength();
if (getLength() < 1) {
pagesize = rowCount;
pageCount = 1;
} else {
pageCount = rowCount / getLength()
+ ((rowCount % getLength()) > 0 ? 1 : 0);
offset = (ipage - 1) * getLength() + 1;
if (offset < 1)
offset = 1;
if (offset > rowCount)
offset = rowCount;
}
rs.absolute(offset);
for (int i = 0; i < pagesize && offset < rowCount + 1; i++,
offset++) {
View m_View = new View();
/** 将关键字查询到的记录赋给各个字段 */
m_View.setFno(rs.getString(1));
m_View.setCityname(rs.getString(2));
m_View.setCtime(rs.getString(3));
m_View.setGtime(rs.getString(4));
rs.next();
list.add(m_View);
}
return list;
} catch (SQLException e) {
return list;
} finally {
AppMode.loginoutUser();
try {
/** 关闭数据库连接 */
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -