【Java开源代码栏目提醒】:网学会员Java开源代码为您提供ClientDAO.java参考,解决您在ClientDAO.java学习中工作中的难题,参考学习。
//---------------------------------------------------------
// Application: Gsm of Application
// Author : esingle
// File : ClientDAO.java
//
// Copyright 2004 landsoft corp
// Generated at Wed Mar 10 15:35:57 CST 2004
// created by 曹广鑫
// mailto:
ClientDAO.java为[网学网-网友上传,谢谢支持]。
//---------------------------------------------------------
package com.landsoft.gsm.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import com.landsoft.gsm.model.*;
import com.landsoft.gsm.util.CacheManager;
public class ClientDAO extends DAO {
public ClientDAO(DataSource ds) {
super(ds);
}
public void insert(Client client) throws SQLException {
String sql;
sql = "INSERT INTO client (mobile, name, sfzh) VALUES (?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, client.getMobile());
pstmt.setString(2, client.getName());
pstmt.setString(3, client.getSfzh());
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(Client client) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "UPDATE client SET name=?, sfzh=? WHERE mobile=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, client.getName());
pstmt.setString(2, client.getSfzh());
pstmt.setString(3, client.getMobile());
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
}
public void delete(String mobile) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String sql = "DELETE FROM client WHERE mobile=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mobile);
pstmt.executeUpdate();
close(pstmt);
conn.commit();
} catch (SQLException e) {
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
String[] objKeys = {"Client", String.valueOf(mobile)};
String objKey = CacheManager.createKey(objKeys);
DAOCacheManager.invalidate(objKey);
}
public Client retrieve(String mobile) throws SQLException {
String[] objKeys = {"Client", String.valueOf(mobile)};
String objKey = CacheManager.createKey(objKeys);
Client client = (Client) DAOCacheManager.getCache(objKey);
if (client != null)
return client;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
String sql = "SELECT * FROM client WHERE mobile=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mobile);
rs = pstmt.executeQuery();
if (rs.next()) {
client = new Client();
populate(client, rs);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
DAOCacheManager.putCache(client, objKey, 1);
return client;
}
public List list() throws SQLException {
String[] objKeys = {"Client", "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, sfzh, mobile FROM client";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Client client = new Client();
populate(client, rs);
list.add(client);
}
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 = {"Client", "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, sfzh, mobile FROM client";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Client client = new Client();
populate(client, rs);
list.add(client);
}
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 newQuery(int offset, int limit, String mobile, String name, String sfzh) throws SQLException {
String[] objKeys = {"Client", "newQuery", String.valueOf(offset), String.valueOf(limit), String.valueOf(mobile), String.valueOf(name), String.valueOf(sfzh)};
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, sfzh, mobile client";
sql += " WHERE mobile =? AND name =? AND sfzh =?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mobile);
pstmt.setString(2, name);
pstmt.setString(3, sfzh);
rs = pstmt.executeQuery();
if(offset > 0) rs.absolute(offset);
int recCount = 0;
while((recCount++ < limit) && rs.next()) {
Client client = new Client();
populate(client, rs);
list.add(client);
}
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;
}
}
上一篇:
ClientContextMgr.java
下一篇:
哈弗F7 VS名爵HS,谁更懂得年轻人的心?