single pattern

package curveprj;

import java.sql.*;

public class DBManager {
private static DBManager instance = null;
private Connection con=null;

private DBManager() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static synchronized DBManager getInstance() {

//这个方法比上面有所改进,不用每次都进行生成对象,只是第一次     
//使用时生成实例,提高了效率!
if (instance==null)
instance=new DBManager();
return instance;
}

public boolean ConnectionDB(String url,String user ,String password){
if(con ==null){
try {
Class.forName("oracle.jdbc.driver.OracleDriver", true,
this.getClass().getClassLoader());
} catch (ClassNotFoundException ex) {
System.out.println("Could not load the driver");
ex.printStackTrace() ;
return false;
}

try {
con = DriverManager.getConnection("jdbc:oracle:thin:@" + url, user,password);
//conn = DriverManager.getConnection("jdbc:oracle:thin:@61.167.119.194:1521:shuhui", "shuhuitest","test");
//stmt=con.createStatement();
} catch (SQLException ex1) {
System.out.println("Could not connect the oracle");
ex1.printStackTrace() ;
return false;
}
}


return true;

}

public void CloseDB(){
try{
//dbcm.freeConnection(dbname, this.con);
con.close();
}
catch(Exception e){
System.out.println("con clsoe make errer") ;
e.printStackTrace();
}
}


public static void main(String[] args) {
DBManager dbmanager = new DBManager();
}

public Connection getCon() {
return con;
}

public void setCon(Connection con) {
this.con = con;
}

private void jbInit() throws Exception {
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值