contactd.jav
来自「Java企业级开发项目实践,这是他的附光盘」· JAV 代码 · 共 216 行
JAV
216 行
//---------------------------------------------------------
// Application: Crm of Enterprice
// Author : eSingle
// File : ContactDAO.java
//
// Copyright 2002 LandSoft Corp.
// Generated at Mon Nov 18 22:41:11 CST 2002
// Created by caoguangxin
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------
package com.landsoft.crm.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import com.landsoft.crm.model.*;
import com.landsoft.crm.util.CacheManager;
public class ContactDAO extends DAO {
public ContactDAO(DataSource ds) {
super(ds);
}
public void insert(Contact contact) throws SQLException {
String sql;
sql = "INSERT INTO contact (contactname, clientname, sex, phone, mobile, address) VALUES (?, ?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, contact.getContactname());
pstmt.setString(2, contact.getClientname());
pstmt.setString(3, contact.getSex());
pstmt.setString(4, contact.getPhone());
pstmt.setString(5, contact.getMobile());
pstmt.setString(6, contact.getAddress());
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(Contact contact) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE contact SET sex=?, phone=?, mobile=?, address=? WHERE contactname=? AND clientname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, contact.getSex());
pstmt.setString(2, contact.getPhone());
pstmt.setString(3, contact.getMobile());
pstmt.setString(4, contact.getAddress());
pstmt.setString(5, contact.getContactname());
pstmt.setString(6, contact.getClientname());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(String contactname, String clientname) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM contact WHERE contactname=? AND clientname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, contactname);
pstmt.setString(2, clientname);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"Contact", String.valueOf(contactname), String.valueOf(clientname)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public Contact retrieve(String contactname, String clientname) throws SQLException {
String[] objKeys = {"Contact", String.valueOf(contactname), String.valueOf(clientname)};
String objKey = CacheManager.createKey(objKeys);
Contact contact = (Contact) DAOCacheManager.getCache(objKey);
if (contact != null)
return contact;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM contact WHERE contactname=? AND clientname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, contactname);
pstmt.setString(2, clientname);
rs = pstmt.executeQuery();
if (rs.next()) {
contact = new Contact();
contact.setContactname(rs.getString(1));
contact.setClientname(rs.getString(2));
contact.setSex(rs.getString(3));
contact.setPhone(rs.getString(4));
contact.setMobile(rs.getString(5));
contact.setAddress(rs.getString(6));
populate(contact, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(contact, objKey, 1);
return contact;
}
public List list() throws SQLException {
String[] objKeys = {"Contact", "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 sex, phone, mobile, address, contactname, clientname FROM contact";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Contact contact = new Contact();
populate(contact, rs);
list.add(contact);
}
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 = {"Contact", "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 sex, phone, mobile, address, contactname, clientname FROM contact";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Contact contact = new Contact();
populate(contact, rs);
list.add(contact);
}
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 + =
减小字号Ctrl + -
显示快捷键?