MDi JLib
1.7.4

com.microdeveloper.db
Class Connector

java.lang.Object
  extended by com.microdeveloper.db.Connector

public class Connector
extends Object

Connector for connecting to database source using a database names property file. Requires username, password, and database aliases for connection to the desired database. These may be set through property values, or direct calls

A typical usage would be

Connection conn = new Connector("myDb");

this will take care of the driver, url, username, password, setup and connection in one simple step. Furthermore, the passwords are stored in an encrypted manner keeping your database keys in one place and keeping them secure.

v1.2 fixes failure to connect when instantiated
v1.1 adds new constructors that initializes the database target
v1.0 initial version

Since:
1.0
Version:
1.2
Author:
Gregg Lagnese
See Also:
ConnectionPool, DesEncrypter

Constructor Summary
Connector()
          Constructor Connector creates an empty Connector without any established properties.
Connector(String dbAlias)
          Creates a new instance of Connector and connects to database dbName.
Connector(String dbAlias, String name, String pw)
          Creates a new instance of Connector and connects to the database using the parameters given.
Connector(String driver, String url, String name, String pw)
          Creates a new instance of Connector and connects to the database using the parameters given.
 
Method Summary
 void close()
          Closes the database connection.
 Connection connect()
          Method open opens the connection to the database using pre-established properties.
 void disconnect()
          Closes the database connection.
 Connection getConnection()
          Returns the current java.sql.Connection object.
 String getDb()
          Deprecated. as of version 1.1, no longer necessary
 String getDbAlias()
          Returns the database alias in effect
 String getDbDriver()
          Returns the currently set driver
 String getDbUrl()
          Returns the current database URL
 String getPassword()
          Returns the currently set password for establishing new connections with.
 String getUsername()
          Returns the currently set username for establishing new connections with
 boolean isComplete()
          Determines if the necessary parameters are set to form a database connection with.
 boolean isOpen()
          Method isOpen returns the open state of the connection.
 boolean setDb(String dbAlias)
          Sets the current connection parameters to those specified by the given alias parameter in the properties file.
 boolean setDbAlias(String dbAlias)
          Sets the current connection parameters to those specified by the given alias parameter in the properties file.
 void setDbDriver(String driver)
          Sets the driver property to the String given.
 void setDbUrl(String url)
          Sets the database URL property to the String given.
 void setPassword(String pw)
          Sets the password for the current user.
 void setUsername(String name)
          Sets the username property to the String given.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Connector

public Connector()
Constructor Connector creates an empty Connector without any established properties. You must set the driver, database url, then username, and password. This can be done by simply specifying the database name as stored in the DbNames property file or by specifying one or more elements manually. Note that you may load some components from the DbNames property file and pass others in, such as username and password.

See Also:
Connector(String), Connector(String,String,String), Connector(String,String,String,String)

Connector

public Connector(String dbAlias)
Creates a new instance of Connector and connects to database dbName. The parameters are derived from the properties file.

Parameters:
dbAlias - name of the database to establish a connection to.

Connector

public Connector(String dbAlias,
                 String name,
                 String pw)
Creates a new instance of Connector and connects to the database using the parameters given. The connection parameters, such as the driver, are derived from the properties file. The username and password given set the appropriate properties for the connection also.

Parameters:
dbAlias - the alias used in the dbnames.properties file (or designated file)
name - the name of the user to connect as
pw - the password for the user name

Connector

public Connector(String driver,
                 String url,
                 String name,
                 String pw)
Creates a new instance of Connector and connects to the database using the parameters given. The connection parameters, such as the driver, are derived from the given parameters. The arguments given cause the corresponding properties to be set accordingly.

Parameters:
driver - the driver to use for the connection
url - the url to connect to
name - the name of the user to connect as
pw - the password for the user name
Method Detail

connect

public Connection connect()
                   throws SQLException
Method open opens the connection to the database using pre-established properties. This assumes you have set the connection properties ahead of time either by reference to a complete DbNames property or manually.

Returns:
the resulting Connection object
Throws:
SQLException - if the connection cannot be established or any other connection error occurs (e.g., bad database URL)
See Also:
setDb(dbName)

getConnection

public Connection getConnection()
Returns the current java.sql.Connection object.

Returns:
Connection object

getDb

public String getDb()
Deprecated. as of version 1.1, no longer necessary

Returns:
the registered Sid, if any

getDbAlias

public String getDbAlias()
Returns the database alias in effect

Returns:
alias currently in use, if any

getDbUrl

public String getDbUrl()
Returns the current database URL

Returns:
database URL string

getDbDriver

public String getDbDriver()
Returns the currently set driver

Returns:
driver string

getUsername

public String getUsername()
Returns the currently set username for establishing new connections with

Returns:
username string

getPassword

public String getPassword()
Returns the currently set password for establishing new connections with. This password is always returned in an encrypted form.

Returns:
encrypted password String
See Also:
DesEncrypter

isOpen

public boolean isOpen()
Method isOpen returns the open state of the connection. True if the passed in connection is currently connected to the database, false otherwise.

Returns:
true if the connection is currently established or false otherwise

isComplete

public boolean isComplete()
Determines if the necessary parameters are set to form a database connection with. If the Connector is established without using the properties and the connection parameters are all set manually, this method will determine if the necessary pre-requisites have been met.

Returns:
true if the necessary parameters are in place, otherwise false.

setDbAlias

public boolean setDbAlias(String dbAlias)
Sets the current connection parameters to those specified by the given alias parameter in the properties file.

Parameters:
dbAlias - the database name alias to be looked up in the properties file.
Returns:
true if the alias was found, otherwise false.

setDb

public boolean setDb(String dbAlias)
Sets the current connection parameters to those specified by the given alias parameter in the properties file.

Parameters:
dbAlias - the database name alias to be looked up in the properties file.
Returns:
true if the alias was found, otherwise false.

setDbUrl

public void setDbUrl(String url)
Sets the database URL property to the String given.

Parameters:
url - the fully qualified database URL (e.g., jdbc:oracle:thin:@host:1521:orcl)

setDbDriver

public void setDbDriver(String driver)
Sets the driver property to the String given.

Parameters:
driver - a fully qualified driver (e.g., oracle.jdbc.driver.OracleDriver)

setUsername

public void setUsername(String name)
Sets the username property to the String given.

Parameters:
name - username to connect to the database as

setPassword

public void setPassword(String pw)
Sets the password for the current user. Should be given in clear text. This will not update the properties file.

Parameters:
pw - clear text password for the connection

disconnect

public void disconnect()
Closes the database connection. If the connection is not open, no error is thrown.


close

public void close()
Closes the database connection. If the connection is not open, no error is thrown.


MDi JLib
1.7.4

Copyright©2001-2007 MicroDeveloper, Inc. All Rights Reserved.