📄 countrydao.java
字号:
//---------------------------------------------------------
// Application: Company Applcation
// Author : Cao guangxin
// File : CountryDAO.java
//
// Copyright 2006 RelationInfo Software
// Writed at Wed Apr 12 08:58:55 CST 2006
// writed by Eclipse SDK
// Visit http://www.37signals.cn
//---------------------------------------------------------
package net.cn37signals.company.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import net.cn37signals.company.model.*;
import net.cn37signals.company.util.CacheManager;
public class CountryDAO extends DAO {
public CountryDAO(DataSource ds) {
super(ds);
}
public void insert(Country country) throws SQLException {
String sql;
sql = "INSERT INTO country (name) VALUES (?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, country.getName());
pstmt.executeUpdate();
pstmt.close();
sql = "SELECT MAX(id) FROM country";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
country.setId(rs.getInt(1));
}
close(rs);
close(pstmt);
conn.commit();
} catch (SQLException sqle) {
close(rs);
close(pstmt);
rollback(conn);
sqle.printStackTrace();
throw sqle;
} finally {
close(conn);
}
}
public void update(Country country) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE country SET name=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, country.getName());
pstmt.setLong(2, country.getId());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(long id) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM country WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, id);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"Country", String.valueOf(id)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public Country retrieve(long id) throws SQLException {
String[] objKeys = {"Country", String.valueOf(id)};
String objKey = CacheManager.createKey(objKeys);
Country country = (Country) DAOCacheManager.getCache(objKey);
if (country != null)
return country;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM country WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
country = new Country();
populate(country, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(country, objKey, 1);
return country;
}
public List list() throws SQLException {
String[] objKeys = {"Country", "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 name, id FROM country";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Country country = new Country();
populate(country, rs);
list.add(country);
}
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 = {"Country", "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 name, id FROM country";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Country country = new Country();
populate(country, rs);
list.add(country);
}
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 + -