public class JdbcUtils {
private static String url="jdbc:oracle:thin:@localhost:1521:orcl";
private static String user="scott";
private static String pwd="tiger";
private JdbcUtils(){
}
// 1、注册驱动oracle.jdbc.driver.OracleDriver
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
// 2、建立一个连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, pwd);
}
// 5、关闭资源
public static void free(ResultSet rs,Statement stmt,Connection conn){
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(stmt!=null)
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static String url="jdbc:oracle:thin:@localhost:1521:orcl";
private static String user="scott";
private static String pwd="tiger";
private JdbcUtils(){
}
// 1、注册驱动oracle.jdbc.driver.OracleDriver
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
// 2、建立一个连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, pwd);
}
// 5、关闭资源
public static void free(ResultSet rs,Statement stmt,Connection conn){
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(stmt!=null)
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}