com.isti.util.database
Class ConnectionJDBC

java.lang.Object
  extended by com.isti.util.database.ConnectionJDBC
Direct Known Subclasses:
MySQLConnectionJDBC, OracleConnectionJDBC

public class ConnectionJDBC
extends java.lang.Object


Field Summary
protected static java.lang.String DEFAULT_DRIVER_NAME
          The default driver name.
static java.lang.String DEFAULT_PROTOCOL
          The default protocol.
static java.lang.String DEFAULT_SUBNAME
          The default subname.
static java.lang.String DEFAULT_SUBPROTOCOL
          The default subprotocol.
static java.lang.String DIALOG_OPTION_STR
          The dialog option string.
static java.lang.String INFO_PASSWORD_KEY
          The password key.
static java.lang.String INFO_USER_KEY
          The user key.
 
Constructor Summary
ConnectionJDBC(java.lang.String url, CallBackCompletion callBack)
          Connect to a JDBC database.
ConnectionJDBC(java.lang.String url, java.util.Properties info, CallBackCompletion callBack)
          Connect to a JDBC database.
ConnectionJDBC(java.lang.String url, java.lang.String userName, java.lang.String passWord, CallBackCompletion callBack)
          Connect to a JDBC database.
 
Method Summary
 void close()
          Closes the database connection.
 void createConnection()
          Creates the database connection.
protected  IstiDialogPopup createPasswordDialog(java.awt.Component parentComp, IstiPasswordPanel passwordPanel)
          Creates a password dialog.
protected  IstiPasswordPanel createPasswordPanel()
          Creates a password panel.
protected  void createStatement()
          Creates the database statement.
static void deregisterDriver()
          Drops the driver from the DriverManager's list.
static void deregisterDriver(java.lang.String driver)
          Drops a Driver from the DriverManager's list.
 QueryJDBC executeQuery(java.lang.String sql)
          Executes an SQL statement that returns a single ResultSet object.
 QueryJDBC executeQuery(java.lang.String sql, java.awt.Component parentComp, IstiDialogPopup popupObj)
          Executes an SQL statement that returns a single ResultSet object.
protected  QueryJDBC executeQueryNow(java.lang.String sql, java.awt.Component parentComp, IstiDialogPopup popupObj)
          Executes an SQL statement that returns a single ResultSet object.
 java.sql.Connection getConnection()
           
protected  java.util.Properties getInfo()
           
 java.sql.Statement getStatement()
           
static java.lang.String getURL(java.lang.String subProtocol, java.lang.String subName)
          Gets a database url of the form jdbc:subprotocol:subname
static java.lang.String getURL(java.lang.String subProtocol, java.lang.String subName, java.lang.String hostName, int port, java.lang.String sid)
          Gets a database url.
 void initConnection(java.awt.Component parentComp, IstiDialogPopup popupObj)
          Initialize the connection with optional password popup.
protected  boolean isClosed()
          Tests to see if a Connection is closed.
protected  void processCallback(java.lang.Object retVal, java.lang.String errorMsg)
          Process a callback
 void promptForPassword(java.awt.Component parentComp)
          Bring up password popup.
static void registerDriver()
          Registers the driver with the DriverManager.
static void registerDriver(java.lang.String driver)
          Registers the given driver with the DriverManager.
static java.util.Properties saveUserPassword(java.util.Properties info, java.lang.String userName, java.lang.String password)
          Saves the username and password in the info properties.
 void setConnection(java.sql.Connection dbConnection)
          Sets the database connection.
 void setStatement(java.sql.Statement dbStatement)
          Sets the database statement.
protected  void setUserPassword(java.lang.String userName, java.lang.String password)
          Sets the username and password.
protected  void startBackgroundThread(java.lang.String sql)
          Starts the background thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DIALOG_OPTION_STR

public static java.lang.String DIALOG_OPTION_STR
The dialog option string.


DEFAULT_DRIVER_NAME

protected static final java.lang.String DEFAULT_DRIVER_NAME
The default driver name.

See Also:
Constant Field Values

DEFAULT_PROTOCOL

public static final java.lang.String DEFAULT_PROTOCOL
The default protocol.

See Also:
Constant Field Values

DEFAULT_SUBPROTOCOL

public static final java.lang.String DEFAULT_SUBPROTOCOL
The default subprotocol.

See Also:
Constant Field Values

DEFAULT_SUBNAME

public static final java.lang.String DEFAULT_SUBNAME
The default subname.


INFO_USER_KEY

public static final java.lang.String INFO_USER_KEY
The user key.

See Also:
Constant Field Values

INFO_PASSWORD_KEY

public static final java.lang.String INFO_PASSWORD_KEY
The password key.

See Also:
Constant Field Values
Constructor Detail

ConnectionJDBC

public ConnectionJDBC(java.lang.String url,
                      CallBackCompletion callBack)
               throws java.lang.ClassNotFoundException,
                      java.lang.InstantiationException,
                      java.lang.IllegalAccessException,
                      java.sql.SQLException
Connect to a JDBC database.

Parameters:
url - a database url of the form jdbc:subprotocol:subname
callBack - callBack method or null to execute on current thread
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

ConnectionJDBC

public ConnectionJDBC(java.lang.String url,
                      java.lang.String userName,
                      java.lang.String passWord,
                      CallBackCompletion callBack)
               throws java.lang.ClassNotFoundException,
                      java.lang.InstantiationException,
                      java.lang.IllegalAccessException,
                      java.sql.SQLException
Connect to a JDBC database.

Parameters:
url - a database url of the form jdbc:subprotocol:subname
userName - user name
passWord - password for the above user name
callBack - callBack method or null to execute on current thread
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

ConnectionJDBC

public ConnectionJDBC(java.lang.String url,
                      java.util.Properties info,
                      CallBackCompletion callBack)
Connect to a JDBC database.

Parameters:
url - a database url of the form jdbc:subprotocol:subname
info - properties information
callBack - callBack method or null to execute on current thread
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.
Method Detail

saveUserPassword

public static java.util.Properties saveUserPassword(java.util.Properties info,
                                                    java.lang.String userName,
                                                    java.lang.String password)
Saves the username and password in the info properties.

Parameters:
info - current info or null if none
userName - user name
password - password for the above user name
Returns:
info properties

getURL

public static java.lang.String getURL(java.lang.String subProtocol,
                                      java.lang.String subName)
Gets a database url of the form jdbc:subprotocol:subname

Parameters:
subProtocol - sub-protocol
subName - sub-name of the protocol
Returns:
database url

getURL

public static java.lang.String getURL(java.lang.String subProtocol,
                                      java.lang.String subName,
                                      java.lang.String hostName,
                                      int port,
                                      java.lang.String sid)
Gets a database url.

Parameters:
subProtocol - sub-protocol
subName - sub-name of the protocol
hostName - host name
port - TCP/IP listener port
sid - database SID (system identifier)
Returns:
database url

registerDriver

public static void registerDriver()
                           throws java.lang.ClassNotFoundException,
                                  java.lang.InstantiationException,
                                  java.lang.IllegalAccessException,
                                  java.sql.SQLException
Registers the driver with the DriverManager. A newly-loaded driver class should call the method registerDriver to make itself known to the DriverManager.

Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

deregisterDriver

public static void deregisterDriver()
                             throws java.lang.ClassNotFoundException,
                                    java.lang.InstantiationException,
                                    java.lang.IllegalAccessException,
                                    java.sql.SQLException
Drops the driver from the DriverManager's list. Applets can only deregister Drivers from their own classloaders.

Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

registerDriver

public static void registerDriver(java.lang.String driver)
                           throws java.lang.ClassNotFoundException,
                                  java.lang.InstantiationException,
                                  java.lang.IllegalAccessException,
                                  java.sql.SQLException
Registers the given driver with the DriverManager. A newly-loaded driver class should call the method registerDriver to make itself known to the DriverManager.

Parameters:
driver - the new JDBC Driver that is to be registered with the DriverManager
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

deregisterDriver

public static void deregisterDriver(java.lang.String driver)
                             throws java.lang.ClassNotFoundException,
                                    java.lang.InstantiationException,
                                    java.lang.IllegalAccessException,
                                    java.sql.SQLException
Drops a Driver from the DriverManager's list. Applets can only deregister Drivers from their own classloaders.

Parameters:
driver - the JDBC Driver to drop
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

initConnection

public void initConnection(java.awt.Component parentComp,
                           IstiDialogPopup popupObj)
                    throws java.lang.ClassNotFoundException,
                           java.lang.InstantiationException,
                           java.lang.IllegalAccessException,
                           java.sql.SQLException
Initialize the connection with optional password popup.

Parameters:
parentComp - parent component for password popup or null for no popup.
popupObj - popup object or null for no popup. This method should only be used when a callback is not provided to the constructor.
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.
See Also:
ConnectionJDBC

createConnection

public void createConnection()
                      throws java.lang.ClassNotFoundException,
                             java.lang.InstantiationException,
                             java.lang.IllegalAccessException,
                             java.sql.SQLException
Creates the database connection.

Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.

createStatement

protected void createStatement()
                        throws java.sql.SQLException
Creates the database statement.

Throws:
java.sql.SQLException - if a database access error occurs.

close

public void close()
Closes the database connection.


isClosed

protected boolean isClosed()
Tests to see if a Connection is closed.

Returns:
true if the connection is closed; false if it's still open

getConnection

public java.sql.Connection getConnection()
Returns:
the database connection.

getInfo

protected java.util.Properties getInfo()
Returns:
the database info.

getStatement

public java.sql.Statement getStatement()
Returns:
the database statement.

setConnection

public void setConnection(java.sql.Connection dbConnection)
Sets the database connection.

Parameters:
dbConnection - database connection

setStatement

public void setStatement(java.sql.Statement dbStatement)
Sets the database statement.

Parameters:
dbStatement - database statement

setUserPassword

protected void setUserPassword(java.lang.String userName,
                               java.lang.String password)
Sets the username and password.

Parameters:
userName - user name
password - password for the above user name

executeQueryNow

protected final QueryJDBC executeQueryNow(java.lang.String sql,
                                          java.awt.Component parentComp,
                                          IstiDialogPopup popupObj)
                                   throws java.lang.ClassNotFoundException,
                                          java.lang.InstantiationException,
                                          java.lang.IllegalAccessException,
                                          java.sql.SQLException
Executes an SQL statement that returns a single ResultSet object.

Parameters:
sql - typically this is a static SQL SELECT statement
parentComp - parent component for password popup or null for no popup.
popupObj - popup object or null for no popup.
Returns:
query results or null if interrupted or callback is used. If a callback is used the return value ('retVal') will contain the query results. The connection will be initialized if needed.
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.
See Also:
initConnection

executeQuery

public final QueryJDBC executeQuery(java.lang.String sql,
                                    java.awt.Component parentComp,
                                    IstiDialogPopup popupObj)
                             throws java.lang.ClassNotFoundException,
                                    java.lang.InstantiationException,
                                    java.lang.IllegalAccessException,
                                    java.sql.SQLException
Executes an SQL statement that returns a single ResultSet object.

Parameters:
sql - typically this is a static SQL SELECT statement
parentComp - parent component for password popup or null for no popup.
popupObj - popup object or null for no popup.
Returns:
query results or null if interrupted or callback is used. If a callback is used the return value ('retVal') will contain the query results. The connection will be initialized if needed.
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.
See Also:
initConnection

executeQuery

public QueryJDBC executeQuery(java.lang.String sql)
                       throws java.lang.ClassNotFoundException,
                              java.lang.InstantiationException,
                              java.lang.IllegalAccessException,
                              java.sql.SQLException
Executes an SQL statement that returns a single ResultSet object.

Parameters:
sql - typically this is a static SQL SELECT statement
Returns:
query results or null if interrupted or callback is used. If a callback is used the return value ('retVal') will contain the query results. The connection will be initialized if needed.
Throws:
java.lang.ClassNotFoundException - if the driver's class could not be found.
java.lang.InstantiationException - if the driver could not be instantiated.
java.lang.IllegalAccessException - if the driver's class or initializer is not accessible.
java.sql.SQLException - if a database access error occurs.
See Also:
initConnection

processCallback

protected void processCallback(java.lang.Object retVal,
                               java.lang.String errorMsg)
Process a callback

Parameters:
retVal - return value object
errorMsg - error message or null if no error.

createPasswordPanel

protected IstiPasswordPanel createPasswordPanel()
Creates a password panel.

Returns:
password panel

createPasswordDialog

protected IstiDialogPopup createPasswordDialog(java.awt.Component parentComp,
                                               IstiPasswordPanel passwordPanel)
Creates a password dialog.

Parameters:
parentComp - parent component for popup.
passwordPanel - password panel
Returns:
the password dialog

promptForPassword

public void promptForPassword(java.awt.Component parentComp)
Bring up password popup.

Parameters:
parentComp - parent component for popup.

startBackgroundThread

protected void startBackgroundThread(java.lang.String sql)
Starts the background thread.

Parameters:
sql - typically this is a static SQL SELECT statement