FileDocCategorySizeDatePackage
ConnectionUtil.javaAPI DocExample3053Wed Jun 16 19:04:20 BST 2004com.darwinsys.sql

ConnectionUtil

public class ConnectionUtil extends Object
Encapsulate the Connection-related operations that every JDBC program seems to use.

Fields Summary
public static final String
DEFAULT_NAME
The default config filename, relative to ${user.home}
private static String
configFileName
The current config filename
Constructors Summary
Methods Summary
public static java.sql.ConnectioncreateConnection(java.lang.String db_driver, java.lang.String db_url, java.lang.String db_user, java.lang.String db_password)



		// Load the database driver
		System.out.println("Loading driver " + db_driver);
		Class.forName(db_driver);

		System.out.println("Connecting to DB " + db_url);
		return DriverManager.getConnection(
			db_url, db_user, db_password);
	
public static java.lang.StringgetConfigFileName()
Returns the full path of the configuration file being used.

return
Returns the configFileName.

		return configFileName;
	
public static java.sql.ConnectiongetConnection(java.lang.String config)
Get a Connection for the given config using the default or set property file name


	                
	       
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(configFileName));
			return getConnection(p, config);
		} catch (IOException ex) {
			throw new DataBaseException(ex.toString());
		}
	
public static java.sql.ConnectiongetConnection(java.util.Properties p, java.lang.String config)
Get a Connection for the given config name from a provided Properties

		try {
			String db_driver = p.getProperty(config  + "." + "DBDriver");
			String db_url = p.getProperty(config  + "." + "DBURL");
			String db_user = p.getProperty(config  + "." + "DBUser");
			String db_password = p.getProperty(config  + "." + "DBPassword");
			if (db_driver == null || db_url == null) {
				throw new IllegalStateException("Driver or URL null: " + config);
			}
			return createConnection(db_driver, db_url, db_user, db_password);
		} catch (ClassNotFoundException ex) {
			throw new DataBaseException(ex.toString());
	
		} catch (SQLException ex) {
			throw new DataBaseException(ex.toString());
		}
	
public static voidsetConfigFileName(java.lang.String configFileNam)
Sets the full path of the config file to read.

param
configFileNam The FileName of the configuration file to use.

		configFileName = configFileNam;
		File file = new File(configFileName);
		if (!file.canRead()) {
			throw new IllegalArgumentException("Unreadable: " + configFileName);
		}
		try {
			ConnectionUtil.configFileName = file.getCanonicalPath();
		} catch (IOException ex) {
			System.err.println("Warning: IO error checking path: " + configFileName);
			ConnectionUtil.configFileName = configFileName;
		}