📄 citydao.java
字号:
//---------------------------------------------------------
// Application: Crm of Enterprice
// Author : eSingle
// File : CityDAO.java
//
// Copyright 2002 LandSoft Corp.
// Generated at Sun Nov 17 23:11:00 CST 2002
// Created by 曹广鑫
// mail to bandsoft@163.com
//---------------------------------------------------------
package com.landsoft.crm.dao;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import com.landsoft.crm.model.*;
import com.landsoft.crm.util.*;
public class CityDAO extends DAO {
public CityDAO(DataSource ds) {
super(ds);
}
public void insert(City city) throws SQLException {
String sql;
sql = "INSERT INTO city (citycode, nationcode, provincecode, cityname) VALUES (?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, city.getCitycode());
pstmt.setString(2, city.getNationcode());
pstmt.setString(3, city.getProvincecode());
pstmt.setString(4, city.getCityname());
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(City city) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE city SET cityname=? WHERE citycode=? AND nationcode=? AND provincecode=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, city.getCityname());
pstmt.setString(2, city.getCitycode());
pstmt.setString(3, city.getNationcode());
pstmt.setString(4, city.getProvincecode());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(String citycode, String nationcode, String provincecode) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM city WHERE citycode=? AND nationcode=? AND provincecode=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, citycode);
pstmt.setString(2, nationcode);
pstmt.setString(3, provincecode);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"City", String.valueOf(citycode), String.valueOf(nationcode), String.valueOf(provincecode)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public City retrieve(String citycode, String nationcode, String provincecode) throws SQLException {
String[] objKeys = {"City", String.valueOf(citycode), String.valueOf(nationcode), String.valueOf(provincecode)};
String objKey = CacheManager.createKey(objKeys);
City city = (City) DAOCacheManager.getCache(objKey);
if (city != null)
return city;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM city WHERE citycode=? AND nationcode=? AND provincecode=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, citycode);
pstmt.setString(2, nationcode);
pstmt.setString(3, provincecode);
rs = pstmt.executeQuery();
if (rs.next()) {
city = new City();
populate(city, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(city, objKey, 1);
return city;
}
public List list() throws SQLException {
String[] objKeys = {"City", "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 citycode, nationcode, provincecode, cityname FROM city";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
City city = new City();
city.setCitycode(rs.getString(1));
city.setNationcode(rs.getString(2));
city.setProvincecode(rs.getString(3));
city.setCityname(rs.getString(4));
populate(city, rs);
list.add(city);
}
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 = {"City", "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 citycode, nationcode, provincecode, cityname FROM city";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
City city = new City();
city.setCitycode(rs.getString(1));
city.setNationcode(rs.getString(2));
city.setProvincecode(rs.getString(3));
city.setCityname(rs.getString(4));
populate(city, rs);
list.add(city);
}
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 + -