共计两个类:
类1:
package com.java7.util;
import java.io.IOException;
import snaq.db.ConnectionPoolManager;
public class MyCPM {
private static final MyCPM mycpm = new MyCPM();
private static ConnectionPoolManager cpm = null;
static{
try {
cpm = ConnectionPoolManager.getInstance("/dbpool.properties");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private MyCPM() {
}
public static MyCPM getInstance() {
return mycpm;
}
public static ConnectionPoolManager getCPM(){
return cpm;
}
public static void release(){
cpm.release();
}
}
类2:
package com.java7.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
public static Connection getConn(){
Connection conn = null;
try {
if(conn == null)
conn = MyCPM.getCPM().getConnection("pooltest");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement getPstmt (Connection conn, String sql){
PreparedStatement pstmt = null;
try {
if (conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement getPstmt(Connection conn , String sql , int autoGeneratedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGeneratedKeys);
}
}catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStmt (Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
}catch(SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getRs (Statement stmt ,String sql) {
ResultSet rs = null;
try{
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
}catch(SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void executeUpdata(Statement stmt ,String sql) {
try {
if(stmt != null) {
stmt.executeUpdate(sql);
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void Close(Connection conn) {
try{
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void Close(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void Close(ResultSet rs) {
try {
if(rs != null) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}