使用硬连接Oracle数据库
部分代码
import java.util.*;
import java.sql.*;
import java.io.*;
public class DBConBean {
private String jdbcdriver = new String();
private String url = new String();
private String user = new String();
private String password = new String();
private Connection conn = null;
public DBConBean(){
getConnection();
}
private void getConnection(){
//从properties文件读取预设的数据库相关信息
Properties prop = new Properties();
try{
InputStream is = getClass().getResourceAsStream("/database-conf.properties");
prop.load(is);
if(is!=null)is.close();
}catch (IOException e){
System.out.print("[DBConnection]打开文件出错");
}
jdbcdriver = prop.getProperty("jdbcdriver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
try{
Class.forName(jdbcdriver);
}catch(ClassNotFoundException e){
}
//产生连接
try{
conn = DriverManager.getConnection(url,user,password);
}catch(SQLException e){
}
}
public Connection getConn(){
//返回连接的接口
return conn;
}
public void close() throws SQLException{
if(conn!=null) conn.close();
}
}