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 {
}
}
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 {
}
}