JAVA JDBC 读取配置文件链接数据库(oracle)

----db.properties--------

dbDriver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@192.168.1.186:1521:jfglorcl
userName=jfgl
password=jfgl

------------  JDBConnection  ----------------------------

public class JDBConnection {
public Connection connection = null;
public JDBConnection() {
ResourceBundle bundle = ResourceBundle.getBundle("db");
String driver = bundle.getString("dbDriver");
String url = bundle.getString("url");
String user = bundle.getString("userName");
String password = bundle.getString("password");
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
System.out.println("数据库加载失败");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

--------------------------xxxDao---------------

public class xxxDao {
private Connection connection = null; //定义连接的对象
private PreparedStatement ps = null; //定义预准备的对象
private JDBConnection jdbc = null; //定义数据库连接对象
public jfljDao() {
jdbc = new JDBConnection();
connection = jdbc.connection; //利用构造方法取得数据库连接
}
public void xx(){
String sql="xxxx";
try {
ps = connection.prepareStatement(sql);
ps.executeUpdate();
ps.close();
}
catch (SQLException ex) {
}
}

}

原文地址:https://www.cnblogs.com/java727/p/3467521.html