声明以下代码并非本人所写,也不是转载。只是参与项目中,总结出来的而已。
public class DBHelper {
private final static Log log = LogFactory.getLog(DBHelper.class.getClass());
//获得properties文件的各个参数
private static String driver = Utils.getString("db_driver");
private static String url = Utils.getString("db_url");
private static String dbName = Utils.getString("db_name");
private static String user = Utils.getString("db_user");
private static String pass = Utils.getString("db_pass");
private Connection con = null;
private Statement statement = null;
public boolean trans = false;
//建立数据库连接
public synchronized Connection getCon() {
if (con == null) {
try {
if (con != null && !con.isClosed()) {
return con;
}
log.debug("Create a connection.");
Class.forName(driver);
log.debug("Load driver " + driver + " success.");
con = DriverManager.getConnection(url + dbName, user, pass);
log.debug("Connect " + dbName + " with user " + user
+ " success.");
//设置不自动提交
con.setAutoCommit(false);
log.debug("This connect must commit by user.");
} catch (Exception e) {
log.error(e.toString(), e);
}
}
return con;
}
public static void configSession(String driver, String url, String dbName,
String user, String pass) {
DBHelper.driver = driver;
DBHelper.url = url;
DBHelper.dbName = dbName;
DBHelper.user = user;
DBHelper.pass = pass;
}
public void setCon(Connection con) {
this.con = con;
}
public String getDbName() {
return dbName;
}
public DBHelper() {
try {
if (con != null && !con.isClosed()) {
return;
}
log.debug("Create a connection.");
Class.forName(driver);
log.debug("Load driver " + driver + " success.");
con = DriverManager.getConnection(url + dbName, user, pass);
log.debug("Connect " + dbName + " with user " + user + " success.");
con.setAutoCommit(true);
log.debug("This connect is Auto commited.");
this.setCon(con);
} catch (Exception e) {
log.error(e.toString(), e);
}
}
public Statement createStatement() throws SQLException {
return (statement = this.getCon().createStatement());
}
public PreparedStatement createStatement(String sql) throws SQLException {
return (PreparedStatement) (statement = this.getCon().prepareStatement(
sql));
}
//回滚
public void rollback() throws SQLException {
con.rollback();
close();
}
//提交
public void commit() throws SQLException {
try {
con.commit();
close();
} catch (SQLException e) {
log.debug("commit fail.", e);
rollback();
}
}
//关闭连接
public void close() {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
log.error(e.toString(), e);
} finally {
log.debug("close statement.");
statement = null;
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
log.error(e.toString(), e);
} finally {
log.debug("close connection.");
con = null;
}
}
}
/**@description:启用事物
* @author:yehui
* @return:void
*/
protected void beginTrans() {
this.trans = true;
}
/**@description:结束事物
* @author:yehui
* @return:void
*/
protected void closeTrans() {
this.trans = false;
try {
this.commit();
} catch (SQLException e) {
log.error("close trans fail.", e);
}
}
}
java连接数据库的工具类
最新推荐文章于 2021-10-21 21:49:48 发布