首页 > 数据库技术 > 详细

获取数据库连接对象的方法

时间:2016-09-02 06:18:44      阅读:204      评论:0      收藏:0      [点我收藏+]

public class DBHelper {
private static final String url="jdbc:oracle:thin:@localhost:1521:orcl";//连接字符串
private static final String user="nga";//用户名
private static final String password="123";//密码
private static Connection con =null;
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 每次在做数据库操作之前都先获取连接对象
* @throws SQLException
*/
public static void getConnection() throws SQLException{
if(con!=null){
if(con.isClosed()){
con = DriverManager.getConnection(url,user,password);
}
}else{
con = DriverManager.getConnection(url,user,password);
}
}
/**
* 每次用完连接之后就需要关掉
* @throws SQLException
*/
public static void closeConnection() throws SQLException{
if(con !=null){
con.close();
}
}

获取数据库连接对象的方法

原文:http://www.cnblogs.com/tutuwowo/p/5831920.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!