package com.zhenghong.database;
import com.config.DbConfigUtil.DBConfigUtil;
import java.sql.*;
public abstract class DBManager {
static {
try {
Class.forName(DBConfigUtil.getValue(“driver”)) ;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection openConnection() throws SQLException {
return DriverManager.getConnection( DBConfigUtil.getValue("url") ,DBConfigUtil.getValue("userName"),DBConfigUtil.getValue("userPwd")) ;
}
public void closeConnection( Connection conn ) throws SQLException{
if( conn != null && !conn.isClosed()){
conn.close();
}
}
public int update( Connection conn , String sql , Object...obs) throws SQLException{
int count = 0 ;
if ( conn != null && !conn.isClosed()){
PreparedStatement psment = conn.prepareStatement( sql ) ;
if ( obs != null){
for (int i = 0; i < obs.length; i++) {
psment.setObject(i+1,obs[i]);
}
}
count = psment.executeUpdate() ;
}
return count ;
}
public ResultSet query( Connection conn , String sql , Object...obs) throws SQLException{
ResultSet rs = null ;
if ( !conn.isClosed() && conn != null){
PreparedStatement psment = conn.prepareStatement( sql ) ;
if ( obs != null ){
for (int i = 0; i < obs.length; i++) {
psment.setObject(i+1,obs[i]);
}
}
rs = psment.executeQuery() ;
}
return rs ;
}
}