/**
* 獲得數據庫連接
* @return
*/
public Connection getConnection(){
try {
//判斷有問題
//if(conn != null)
//應該判斷連接是否已關閉
if(conn != null && !conn.isClosed())
return conn;
String dbDriver = DBDRIVER;
String dbUrl = DBURL;
String dbUser = DBUSER;
String dbPwd = DBPWD;
Class.forName(dbDriver).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
if(conn == null)
throw new Exception("獲取數據庫連接失敗!");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
轉載自:https://blog.csdn.net/hongweigg/article/details/7441648
