java是如何連接數(shù)據(jù)庫
2016-09-09 07:22:00
11750
我的config.properties放在了web工程的src目錄下。
properties配置文件內(nèi)容
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@10.1.55.241:1521:test
jdbc.username=landui
jdbc.password=landui
jdbc.show_sql=true
調(diào)用這些信息連接來數(shù)據(jù)庫.一般是在dao類里.
ResourceBundle bundle = ResourceBundle.getBundle("DBConfig");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}