DBHelper.java(数据库连接类)
package com.vcredit.ddcash.batch.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public class DBHelper {
private static final Logger logger=Logger.getLogger(DBHelper.class);
/**
* 关闭所有连接
*/
public static void closeAll(Connection con,Statement statement,ResultSet rs){
try{
logger.info("关闭所有连接: Connection:"+con+" | Statement:"+statement+" | ResultSet:"+rs);
if(null!=con)
con.close();
if(null!=statement)
statement.close();
if(null!=rs)
rs.close();
}catch (SQLException e) {
logger.info("关闭所有连接失败"+e.getMessage());
}
}
/**
* 建立连接
* @return
*/
public static Connection getConnection(){
Connection con=null;
String driver="com.mysql.jdbc.Driver";
String url=PropertiesUtil.getPropertiesByName("databaseURL");
String user=PropertiesUtil.getPropertiesByName("databaseUserName");
String pwd=PropertiesUtil.getPropertiesByName("databasePwd");
try {
Class.forName(driver);
logger.info("Database driver is successfully added!");
} catch (ClassNotFoundException e) {
logger.error("Database driver get failed!");
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,user,pwd);
logger.info("Database connection is successful");
} catch (SQLException e) {
logger.error("Database connection is failed");
e.printStackTrace();
}
return con;
}
}