java通过配置属性文件连接Oracle数据库
???? 做web项目开发中,通过配置属性文件实现连接数据库(Oracle)的功能。
???1.将属性文件放在WEB-INF/classes目录下
???? 例如属性文件:demo_db.properties
???? DRIVERS=oracle.jdbc.driver.OracleDriver
???? URL=jdbc:oracle:thin:@localhost:1521:MyOra
???? USER=user
??? ?PASSWORD=123456
???2.写一个连接数据库的类
??? 例如:DBUtils.java
- package?com.ctcjz.db;
- import?java.sql.Connection;
- import?java.sql.DriverManager;
- import?java.sql.ResultSet;
- import?java.sql.SQLException;
- import?java.sql.Statement;
- import?java.util.ResourceBundle;
- public?class?DBUtils?{
- ????private?static?final?String?OPTION_FILE_NAME?=?"demo_db";
- ????private?static?String?drivers;
- ????private?static?String?url;
- ????private?static?String?user;
- ????private?static?String?password;
- ????static?{
- ????????ResourceBundle?res?=?ResourceBundle.getBundle(OPTION_FILE_NAME);
- ????????drivers?=?res.getString("DRIVERS").trim();
- ????????url?=?res.getString("URL").trim();
- ????????user?=?res.getString("USER").trim();
- ????????password?=?res.getString("PASSWORD").trim();
- ????}
- ????public?static?Connection?getConnection()?throws?SQLException?{
- ????????Connection?conn?=?null;
- ????????try?{
- ????????????Class.forName(drivers).newInstance();
- ????????????conn?=?DriverManager.getConnection(url,?user,?password);
- ????????}?catch?(Exception?e)?{
- ????????????e.printStackTrace();
- ????????}
- ????????if?(conn?==?null)?{
- ????????????throw?new?SQLEx