jdbc連接數據庫

寫了一個簡單的jdbc連接:
package Struts2.Util;

import java.io.IOException;

public class BaseDao {
private Connection conn;
private PreparedStatement pt;
private ResultSet rs;

private String url;
private String driver;
private String user;
private String pwd;

/**
*
* 得到配置文件
*/
public void getProperties() {
Properties pt = new Properties();
InputStream in = Object.class.getResourceAsStream("/dbconfig.properties");
try {
pt.load(in);
driver = pt.getProperty("jdbc.driverClassName").trim();
url = pt.getProperty("jdbc.url").trim();
user = pt.getProperty("jdbc.username").trim();
pwd = pt.getProperty("jdbc.password").trim();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public BaseDao() {
super();
}

/*
*
* 得到數據庫連接
*/
private Connection getConn() {
getProperties();
try {
// 註冊數據庫驅動
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/*
*關閉數據庫連接
*
*/
public void CloseConn(){
if(rs != null) try{ rs.close(); }catch(Exception e){}
if(pt != null) try{ pt.close(); }catch(Exception e){}
if(conn != null) try{ conn.close(); }catch(Exception e){}
}
/*public static void main(String[] args){
BaseDao basedao=new BaseDao();
System.out.println(basedao.getConn());
}*/
}

其中dbconfig.properties是在src目錄下面,其代碼爲:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.9.206.168:1521:orcl
jdbc.username=omsystem
jdbc.password=tj123456
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
jdbc.initialPoolSize=10
hibernate.show_sql=true
hibernate.cache.use_query_cache=true
hibernate.substitutions=true 1, false 0, yes 'Y', no 'N'
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章