package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConn {
public static Connection getConnection(){
//定义一个连接对象
Connection conn=null;
//定义连接数据库的URL资源
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
//定义连接数据库的用户名称与密码
String username="system";
String password="123456";
//加载数据库连接驱动
String className="oracle.jdbc.driver.OracleDriver";
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取数据库的连接对象
try {
conn=DriverManager.getConnection(url,username,password);
System.out.println("数据库连接建立成功...");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回连接对象
return conn;
}
public static void closeDB(Connection conn,PreparedStatement ps, ResultSet rs) throws SQLException{
if(conn!=null){
conn.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
}
}