----db.properties--------sql
dbDriver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@192.168.1.186:1521:jfglorcl
userName=jfgl
password=jfgl數據庫
------------ JDBConnection ----------------------------oracle
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();
}
}
}url
--------------------------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) {
}
}get
}io