📄 mxzdao.java
字号:
//---------------------------------------------------------
// Application: Ch03
// Author : helpsoft
// File : MxzDAO.java
//
// Copyright 2004 www.helpsoft.org
//开发时间:2004-11-01 01:03:28 CST 2004
// 作者:曹广鑫
// 访问 http://www.helpsoft.org
//---------------------------------------------------------
package org.helpsoft.ch03.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import org.helpsoft.ch03.model.*;
import org.helpsoft.ch03.util.CacheManager;
public class MxzDAO extends DAO {
public MxzDAO(DataSource ds) {
super(ds);
}
public void insert(Mxz mxz) throws SQLException {
String sql;
sql = "INSERT INTO mxz (id, zy, sr, zc, rq) VALUES (?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mxz.getId());
pstmt.setString(2, mxz.getZy());
pstmt.setFloat(3, mxz.getSr());
pstmt.setFloat(4, mxz.getZc());
pstmt.setString(5, mxz.getRq());
pstmt.executeUpdate();
pstmt.close();
conn.commit();
} catch (SQLException sqle) {
close(rs);
close(pstmt);
rollback(conn);
sqle.printStackTrace();
throw sqle;
} finally {
close(conn);
}
}
public void update(Mxz mxz) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE mxz SET zy=?, sr=?, zc=?, rq=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mxz.getZy());
pstmt.setFloat(2, mxz.getSr());
pstmt.setFloat(3, mxz.getZc());
pstmt.setString(4, mxz.getRq());
pstmt.setString(5, mxz.getId());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(String id) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM mxz WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"Mxz", String.valueOf(id)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public Mxz retrieve(String id) throws SQLException {
String[] objKeys = {"Mxz", String.valueOf(id)};
String objKey = CacheManager.createKey(objKeys);
Mxz mxz = (Mxz) DAOCacheManager.getCache(objKey);
if (mxz != null)
return mxz;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM mxz WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
mxz = new Mxz();
populate(mxz, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(mxz, objKey, 1);
return mxz;
}
public List list() throws SQLException {
String[] objKeys = {"Mxz", "list"};
String objKey = CacheManager.createKey(objKeys);
ArrayList list = (ArrayList) DAOCacheManager.getCache(objKey);
if (list != null)
return list;
list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT zy, sr, zc, rq, id FROM mxz";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Mxz mxz = new Mxz();
populate(mxz, rs);
list.add(mxz);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(list, objKey, 1);
return list;
}
public List list(int offset, int limit) throws SQLException {
String[] objKeys = {"Mxz", "list", String.valueOf(offset), String.valueOf(limit)};
String objKey = CacheManager.createKey(objKeys);
ArrayList list = (ArrayList) DAOCacheManager.getCache(objKey);
if (list != null)
return list;
list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT zy, sr, zc, rq, id FROM mxz";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Mxz mxz = new Mxz();
populate(mxz, rs);
list.add(mxz);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(list, objKey, 1);
return list;
}
public List newQuery(int offset, int limit, float sr, float zc, String rq) throws SQLException {
String[] objKeys = {"Mxz", "newQuery", String.valueOf(offset), String.valueOf(limit), String.valueOf(sr), String.valueOf(zc), String.valueOf(rq)};
String objKey = CacheManager.createKey(objKeys);
ArrayList list = (ArrayList) DAOCacheManager.getCache(objKey);
if (list != null)
return list;
list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT zy, sr, zc, rq, id mxz";
sql += " WHERE zy ? AND sr =? AND zc =? AND rq =?";
pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1, sr);
pstmt.setFloat(2, zc);
pstmt.setString(3, rq);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Mxz mxz = new Mxz();
populate(mxz, rs);
list.add(mxz);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(list, objKey, 1);
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -