java连接数据库的工具类
2013-04-17 14:14
489 查看
声明以下代码并非本人所写,也不是转载。只是参与项目中,总结出来的而已。 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-数据库连接工具类 DataSourceUtil.java
- java连接数据库的工具类
- java中常用的工具类(三)连接数据库的综合类,DES加密和解密,Base64,ExcelUtil工具类,
- java中常用的工具类(三)连接数据库的综合类,DES加密和解密,Base64,ExcelUtil工具类,
- MyBatisUtil.java.工具类,连接数据库库
- java 连接数据库通用工具类
- java常用工具类之数据库连接类(可以连接多种数据库)
- java使用jdbc连接数据库工具类和jdbc连接mysql数据示例
- java(2014版)连接数据库的工具类
- Java常用工具类封装——连接数据库的工具类
- java 数据库连接工具类
- java连接数据库工具类—JDBC
- java使用jdbc连接数据库工具类和jdbc连接mysql数据示例
- java(2014版)连接数据库的工具类
- JDBC之数据库连接的java配置工具类dbUtils
- Java常用工具类封装——连接数据库的工具类
- java连接数据库工具类写法
- JavaWeb之JDBC(二)采用读取配置文件方式编写JDBC的工具类,获取数据库的连接
- 关于数据库连接操作的工具类DBUtil.java
- java实现数据库连接的工具类