JDBC接口封装
package cn.piesat.sparkproject.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import cn.piesat.sparkproject.conf.ConfigurationManager;
import cn.piesat.sparkproject.constant.Constants;
/**
* jdbc辅助组件
* @author Administrator
*
*/
public class JDBCHelper {
private static JDBCHelper instance=null;
/**
* 数据库连接池
*/
private LinkedList<Connection> dataSource=new LinkedList<Connection>();
static{
try {
Class.forName(ConfigurationManager.getProperty(Constants.JDBC_DRIVER));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 私有化构造方法
*/
private JDBCHelper(){
int datasourceSize=ConfigurationManager.getInteger(Constants.JDBC_DATASOURCE_SIZE);
for(int i=0;i<datasourceSize;i++){
try {
Connection conn=DriverManager.getConnection(
ConfigurationManager.getProperty(Constants.JDBC_URL),
ConfigurationManager.getProperty(Constants.JDBC_USER),
ConfigurationManager.getProperty(Constants.JDBC_PASSWORD));
dataSource.push(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
};
/**
* 获取实例
* @return
*/
public static JDBCHelper getInstance(){
if(instance==null){
synchronized(JDBCHelper.class){
if(instance==null){
instance=new JDBCHelper();
}
}
}
return instance;
}
/**
* 获取数据库连接
* @return
*/
public synchronized Connection getConnection(){
while(dataSource.size()<=0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return dataSource.poll();
}
/**
* 执行增删改SQL语句
* @param sql
* @param params
* @return
*/
public int executeUpdate(String sql,Object[] params){
Connection conn=null;
PreparedStatement pstmt=null;
int rnt=0;
try{
conn=getConnection();
pstmt = conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
rnt = pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
dataSource.push(conn);
}
}
return rnt;
}
/**
* 执行查询SQL语句
* @param sql
* @param params
*/
public void executeQuery(String sql,Object[] params,QueryCallback callback){
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try{
conn=getConnection();
pstmt=conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
}
rs=pstmt.executeQuery();
callback.process(rs);
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
dataSource.push(conn);
}
}
}
/**
* 批量执行sql语句
* @param sql
* @param params
* @return 每条sql语句影响的行数
*/
public int[] executeBatch(String sql,List<Object[]> paramsList){
int[] rtn=null;
Connection conn=null;
PreparedStatement pstmt=null;
try{
conn=getConnection();
conn.setAutoCommit(false);
pstmt=conn.prepareStatement(sql);
for(Object[] params:paramsList){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
pstmt.addBatch();
}
rtn=pstmt.executeBatch();
conn.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
dataSource.push(conn);
}
}
return rtn;
}
/**
* 查询回调接口
* @author Administrator
*
*/
public static interface QueryCallback{
void process(ResultSet rs)throws Exception;
}
}