java連接數據庫的工具類

聲明以下代碼並非本人所寫,也不是轉載。只是參與項目中,總結出來的而已。
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);
		}
	}


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章