对于数据库的连接,有一些特性的东西;积累了几个方法,可以作为一个可复用的模块。
/********************************************************************/
类JbfcmsConnect :
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import java.lang.*;
/**
* @author *
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class JbfcmsConnect {
private String drive = "";
private String dbURL = "";
private String prop_server_name = "";
private String prop_db_name = "";
private String dbUserName = "";
private String dbUserPassword = "";
private String prop_db_provider;
/**
* onstructor for DBConnect
*/
public JbfcmsConnect() {
init();
}
/**
* Method init.
*/
private void init() {
System.out.println("enter init");
Properties defaultSettings = new Properties();
defaultSettings.put("DB_NAME", "JBMMS");
defaultSettings.put("DB_PROVIDER","db2");
defaultSettings.put("USERNAME", "root");
defaultSettings.put("PASSWERD", " ");
defaultSettings.put("DRIVER_TYPE", "net");
System.out.println("put default settings");
String prop_driver_type = defaultSettings.getProperty("DRIVER_TYPE");
prop_db_name = defaultSettings.getProperty("DB_NAME");
prop_db_provider=defaultSettings.getProperty("DB_PROVIDER");
dbUserName = defaultSettings.getProperty("USERNAME");
dbUserPassword = defaultSettings.getProperty("PASSWORD");
System.out.println("set default settings");
Properties settings = new Properties();
try {
System.out.println("Start search Properties File");
FileInputStream sf = new FileInputStream("c:\\jbfcmsconnect.prop");
settings.load(sf);
prop_driver_type = settings.getProperty("DRIVER_TYPE");
prop_db_name = settings.getProperty("DB_NAME");
prop_db_provider=settings.getProperty("DB_PROVIDER");
dbUserName = settings.getProperty("USERNAME");
dbUserPassword = settings.getProperty("PASSWORD");
prop_server_name = settings.getProperty("SERVER_NAME");
sf.close();
System.out.println("Properties File exist");
} catch (FileNotFoundException e) {
System.out.println("Properties File was not found!");
} catch (IOException e) {
System.out.println("IOException is found!");
}
/////////////////
if (prop_db_provider.equalsIgnoreCase("db2"))
{ if(prop_driver_type.equalsIgnoreCase("net"))
{ //System.out.println("be in-----");
drive="COM.ibm.db2.jdbc.net.DB2Driver";
String prop_server_name=settings.getProperty("SERVER_NAME");
dbURL="jdbc:db2://" + prop_server_name + "/" + prop_db_name;
}else{
drive="COM.ibm.db2.jdbc.app.DB2Driver";
dbURL="jdbc:db2:" + prop_db_name;
}
System.out.println("DB2 Connection ==> \n driverName="+ drive + "\n url="+dbURL+"\n");}
//MYSQL数据库连接
else if (prop_db_provider.equalsIgnoreCase("mysql"))
{ if(prop_