【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,ConnectToDatabase.java,希望对大家有所帮助!
package ejava.jdbcch09;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.ClassNotFoundException;
import java.util.StringTokenizer;
/**
This class makes a connection to the database using configuration
information provided in a properties file.
*/
public class ConnectToDatabase
{
private String driverClass; // Driver class name
private String dbURL; // Database URL
private Properties dbProperties; // Properties read from properties file
private Connection connection; // A database connection object
public ConnectToDatabase()
{
}
public ConnectToDatabase(String propertiesFile)
throws FileNotFoundException, IOException
{
this.setProperties(propertiesFile);
}
/**
Reads the connection/configuration properties from the file and loads it
into the dbProperties object.
*/
public void setProperties(String fileName)
throws FileNotFoundException, IOException
{
dbProperties = new Properties();
FileInputStream propertiesFileStream = new FileInputStream(fileName);
dbProperties.load(propertiesFileStream);
}
/**
Checks to see if the DRIVER_CLASS, DATABASE_URL, username, password
and subproperites from the properties object were created from reading
the file. Makes the connection to the database if the connection is
not made yet.
*/
public Connection getConnection()
throws ClassNotFoundException, SQLException, Exception
{
if(connection != null){
return this.connection;
}
// Get connection properties from properties file
driverClass = (String)dbProperties.get("DRIVER_CLASS");
dbURL = (String)dbProperties.get("DATABASE_URL");
String userName = ((String)dbProperties.get("UserName")).trim();
String password = ((String)dbProperties.get("Password")).trim();
String otherProperties = (String)dbProperties.get("SUB_PROPERTIES");
if(driverClass == null || dbURL == null){
throw new Exception("Driver Class or Driver URL should not be null");
}
System.out.println("Registering driver : "+driverClass);
// Load driver
Class.forName(driverClass);
// Create connection in one of three ways depending on whether or not
// a user name, password, or sub-properties were in properties file
if( (userName.length() == 0) && (password.length() == 0)
&& (otherProperties.length() == 0) ){
System.out.println("Connecting to Database :"+dbURL);
connection = DriverManager.getConnection(dbURL);
}
else if(otherProperties.length() == 0){
System.out.println("Connecting to DataBase :"+dbURL
+" UserName :"+userName +"Password :"+password);
connection = DriverManager.getConnection(dbURL, userName, password);
}
else if(otherProperties.length() != 0){
Properties subProperties = convertStringToProperties(otherProperties);
System.out.println("Connecting to DataBase :"+dbURL
+" Properties :"+subProperties.toString());
connection = DriverManager.getConnection(dbURL, subProperties);
}
return connection;
}
public Properties getProperties()
{
return this.dbProperties;
}
/**
Convenience method to convert a string with tokens into
a properites object.
*/
public Properties convertStringToProperties(String s)
{
Properties g = new Properties();
StringTokenizer st = new StringTokenizer(s,",");
int i = 0;
while(st.hasMoreTokens()){
String nthToken = (String)st.nextElement();
StringTokenizer stsub = new StringTokenizer(nthToken,"=");
String name = null;
String value = null;
while(stsub.hasMoreElements()){
name = (String)stsub.nextElement();
value = (String)stsub.nextElement();
}
if(value == null){
value ="";
}
name =