package cn.sjy.org.dbconn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.sjy.org.exception.MyRunTimeException;
public class OracleDBConn {
private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ora9i";
private static final String DBUSER = "sjy";
private static final String DBPASSWORD = "sjy";
private static OracleDBConn oracledbconn;
Connection conn;
private OracleDBConn (){//单子模式
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new MyRunTimeException("数据库加载失败", e);
}
}
public static OracleDBConn getOracleDBConn(){
if(oracledbconn == null)
oracledbconn = new OracleDBConn();
return oracledbconn;
}
public Connection getConnection(){
try {
conn = DriverManager.getConnection(this.DBURL, this.DBUSER, this.DBPASSWORD);
} catch (SQLException e) {
e.printStackTrace();
throw new MyRunTimeException("连接数据库失败", e);
}
return conn;
}
public void colse(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if(rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}