📄 saledao.java
字号:
//---------------------------------------------------------
// Application: eShop of Network
// Author : eSingle
// File : SaleDAO.java
//
// Copyright 2002 LandSoft Corp.
// Generated at Mon Nov 18 20:15:26 CST 2002
// Created by caoguangxin
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------
package com.landsoft.eshop.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import com.landsoft.eshop.model.*;
import com.landsoft.eshop.util.CacheManager;
public class SaleDAO extends DAO {
public SaleDAO(DataSource ds) {
super(ds);
}
public void insert(Sale sale) throws SQLException {
String sql;
sql = "INSERT INTO sale (salename, address, phone, mobile) VALUES (?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, sale.getSalename());
pstmt.setString(2, sale.getAddress());
pstmt.setString(3, sale.getPhone());
pstmt.setString(4, sale.getMobile());
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(Sale sale) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE sale SET address=?, phone=?, mobile=? WHERE salename=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, sale.getAddress());
pstmt.setString(2, sale.getPhone());
pstmt.setString(3, sale.getMobile());
pstmt.setString(4, sale.getSalename());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(String salename) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM sale WHERE salename=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, salename);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"Sale", String.valueOf(salename)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public Sale retrieve(String salename) throws SQLException {
String[] objKeys = {"Sale", String.valueOf(salename)};
String objKey = CacheManager.createKey(objKeys);
Sale sale = (Sale) DAOCacheManager.getCache(objKey);
if (sale != null)
return sale;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM sale WHERE salename=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, salename);
rs = pstmt.executeQuery();
if (rs.next()) {
sale = new Sale();
populate(sale, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(sale, objKey, 1);
return sale;
}
public List list() throws SQLException {
String[] objKeys = {"Sale", "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 address, phone, mobile, salename FROM sale";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Sale sale = new Sale();
populate(sale, rs);
list.add(sale);
}
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 = {"Sale", "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 address, phone, mobile, salename FROM sale";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Sale sale = new Sale();
populate(sale, rs);
list.add(sale);
}
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 + -