有完整的程序和源码
源代码在线查看: business.jav
//---------------------------------------------------------
// Application: Personal of System
// Author : eSingle
// File : BusinessDAO.java
//
// Copyright 2002 LandSoft Corp.
// Generated at Mon Nov 18 10:18:19 CST 2002
// Created by caoguangxin
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------
package com.landsoft.personal.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import com.landsoft.personal.model.*;
import com.landsoft.personal.util.CacheManager;
public class BusinessDAO extends DAO {
public BusinessDAO(DataSource ds) {
super(ds);
}
public void insert(Business business) throws SQLException {
String sql;
sql = "INSERT INTO business (businessname) VALUES (?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, business.getBusinessname());
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(Business business) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE business SET WHERE businessname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, business.getBusinessname());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(String businessname) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM business WHERE businessname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, businessname);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"Business", String.valueOf(businessname)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public Business retrieve(String businessname) throws SQLException {
String[] objKeys = {"Business", String.valueOf(businessname)};
String objKey = CacheManager.createKey(objKeys);
Business business = (Business) DAOCacheManager.getCache(objKey);
if (business != null)
return business;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM business WHERE businessname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, businessname);
rs = pstmt.executeQuery();
if (rs.next()) {
business = new Business();
business.setBusinessname(rs.getString(1));
populate(business, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(business, objKey, 1);
return business;
}
public List list() throws SQLException {
String[] objKeys = {"Business", "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 businessname FROM business";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Business business = new Business();
business.setBusinessname(rs.getString(1));
populate(business, rs);
list.add(business);
}
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 = {"Business", "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 businessname FROM business";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Business business = new Business();
business.setBusinessname(rs.getString(1));
populate(business, rs);
list.add(business);
}
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;
}
}