org.opengts.dbtools
Class DBConnection

java.lang.Object
  extended by org.opengts.dbtools.DBConnection

public class DBConnection
extends java.lang.Object

DBConnection provides connections to the SQL server


Field Summary
static boolean ALWAYS_NEW_CONNECTION
           
protected static java.util.Collection<DBConnection> dbConnectionList
           
protected static java.util.Map<java.lang.String,DBConnection> dbConnectionMap
           
protected static java.util.Map<java.lang.String,java.util.ArrayList<DBConnection>> dbConnectionPool
           
 
Constructor Summary
protected DBConnection(java.lang.String uri, java.lang.String user, java.lang.String pass)
          Constructor
 
Method Summary
protected  java.sql.Statement _execute(java.lang.String sql, boolean rowByRow)
          Execute the specified SQL statement
protected  java.sql.ResultSet _executeQuery(java.lang.String sql)
          Execute the specified SQL query
protected static void _saveDBConnection(DBConnection dbc)
          Saves a named DBConnection
static void closeAllConnections()
          Close all open DBConnections
 void closeConnection()
          Closes the current database connection
 java.sql.Statement createStatement()
          Create a new Statement
 java.sql.Statement createStatement(boolean rowByRow)
          Create a new Statement
 java.sql.Statement execute(java.lang.String sql)
          Execute the specified SQL statement
 java.sql.Statement execute(java.lang.String sql, boolean rowByRow)
          Execute the specified SQL statement
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Execute the specified SQL query
 void executeUpdate(java.lang.String sql)
          Execute the specified SQL update
 long executeUpdate(java.lang.String sql, boolean rtnAutoIncrVal)
          Execute the specified SQL update
protected  void finalize()
           
 java.sql.Connection getConnection()
          Returns a database connection
static DBConnection getDBConnection(java.lang.String uri, java.lang.String user, java.lang.String pass)
          Gets a DBConnection based on the specified URI
static DBConnection getDefaultConnection()
          Gets the default DBConnection
 java.lang.String getPassword()
          Gets the connection user password
static boolean getShowExecutedSQL()
          Returns true if SQL statements should be displayed
 java.lang.String getUri()
          Gets the connection URI
 java.lang.String getUser()
          Gets the connection user
 boolean isConnectionClosed()
          Returns true if the current database connection is closed
 boolean isConnectionTimeout()
          Returns true if the last time this connection was used is beyond the timeout interval
static boolean isLocked(DBConnection dbc)
           
static void main(java.lang.String[] argv)
          DEBUG: Main entry point used for testing/debugging
static boolean parseCommunicationsException(java.sql.SQLException sqe)
          Analyze SQLException and return true if the exception refers to a communication error.
static void popShowExecutedSQL()
          Pops the display of SQL statements state
static void pushShowExecutedSQL()
          Pushes displaying SQL statement on a stack
static void release(DBConnection dbc)
          clear "In Use"
static void release(DBConnection dbc, java.sql.Statement stmt, java.sql.ResultSet rs)
          clear "In Use"
static void setShowExecutedSQL(boolean showSQL)
          Sets a flag indicating that all SQL statements should be displayed prior to execution
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALWAYS_NEW_CONNECTION

public static boolean ALWAYS_NEW_CONNECTION

dbConnectionMap

protected static java.util.Map<java.lang.String,DBConnection> dbConnectionMap

dbConnectionPool

protected static java.util.Map<java.lang.String,java.util.ArrayList<DBConnection>> dbConnectionPool

dbConnectionList

protected static java.util.Collection<DBConnection> dbConnectionList
Constructor Detail

DBConnection

protected DBConnection(java.lang.String uri,
                       java.lang.String user,
                       java.lang.String pass)
Constructor

Parameters:
uri - The connection URI
user - The user name
pass - The password
Method Detail

setShowExecutedSQL

public static void setShowExecutedSQL(boolean showSQL)
Sets a flag indicating that all SQL statements should be displayed prior to execution

Parameters:
showSQL - True to display SQL statements, false otherwise

getShowExecutedSQL

public static boolean getShowExecutedSQL()
Returns true if SQL statements should be displayed

Returns:
True if SQL statements should be displayed

pushShowExecutedSQL

public static void pushShowExecutedSQL()
Pushes displaying SQL statement on a stack


popShowExecutedSQL

public static void popShowExecutedSQL()
Pops the display of SQL statements state


parseCommunicationsException

public static boolean parseCommunicationsException(java.sql.SQLException sqe)
Analyze SQLException and return true if the exception refers to a communication error.

Parameters:
sqe - The SQLException
Returns:
True if the SQLException refers to a communication error, false otherwise.

_saveDBConnection

protected static void _saveDBConnection(DBConnection dbc)
Saves a named DBConnection

Parameters:
dbc - The DBConnection

closeAllConnections

public static void closeAllConnections()
Close all open DBConnections


getDBConnection

public static DBConnection getDBConnection(java.lang.String uri,
                                           java.lang.String user,
                                           java.lang.String pass)
Gets a DBConnection based on the specified URI

Parameters:
uri - The connection URI
user - The user name
pass - The password
Returns:
The returned DBConnection

getDefaultConnection

public static DBConnection getDefaultConnection()
Gets the default DBConnection


isLocked

public static boolean isLocked(DBConnection dbc)

release

public static void release(DBConnection dbc)
clear "In Use"


release

public static void release(DBConnection dbc,
                           java.sql.Statement stmt,
                           java.sql.ResultSet rs)
clear "In Use"


finalize

protected void finalize()
Overrides:
finalize in class java.lang.Object

getUri

public java.lang.String getUri()
Gets the connection URI

Returns:
The connection URI

getUser

public java.lang.String getUser()
Gets the connection user

Returns:
The connection user

getPassword

public java.lang.String getPassword()
Gets the connection user password

Returns:
The connection user password

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Returns a database connection

Returns:
A database connection
Throws:
java.sql.SQLException - If an SQL error occurs

closeConnection

public void closeConnection()
Closes the current database connection


isConnectionClosed

public boolean isConnectionClosed()
Returns true if the current database connection is closed

Returns:
True if the current database connection is closed

isConnectionTimeout

public boolean isConnectionTimeout()
Returns true if the last time this connection was used is beyond the timeout interval

Returns:
True if the last time this connection was used is beyond the timeout interval

createStatement

public java.sql.Statement createStatement()
                                   throws java.sql.SQLException
Create a new Statement

Returns:
A new SQL Statement
Throws:
java.sql.SQLException - If an SQL error occurs

createStatement

public java.sql.Statement createStatement(boolean rowByRow)
                                   throws java.sql.SQLException
Create a new Statement

Parameters:
rowByRow - True to create a new Statement in row-by-row mode
Returns:
A new SQL Statement
Throws:
java.sql.SQLException - If an SQL error occurs

execute

public java.sql.Statement execute(java.lang.String sql)
                           throws java.sql.SQLException,
                                  DBException
Execute the specified SQL statement

Parameters:
sql - The String SQL statement to execute
Returns:
The returned executed statement
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

execute

public java.sql.Statement execute(java.lang.String sql,
                                  boolean rowByRow)
                           throws java.sql.SQLException,
                                  DBException
Execute the specified SQL statement

Parameters:
sql - The String SQL statement to execute
rowByRow - True to create a new Statement in row-by-row mode
Returns:
The returned executed statement
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

_execute

protected java.sql.Statement _execute(java.lang.String sql,
                                      boolean rowByRow)
                               throws java.sql.SQLException
Execute the specified SQL statement

Parameters:
sql - The String SQL statement to execute
rowByRow - True to create a new Statement in row-by-row mode
Returns:
The returned executed statement
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
                                throws java.sql.SQLException,
                                       DBException
Execute the specified SQL query

Parameters:
sql - The String SQL statement to execute
Returns:
The returned ResultSet
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

_executeQuery

protected java.sql.ResultSet _executeQuery(java.lang.String sql)
                                    throws java.sql.SQLException
Execute the specified SQL query

Parameters:
sql - The String SQL statem
Returns:
The returned ResultSet
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

executeUpdate

public void executeUpdate(java.lang.String sql)
                   throws java.sql.SQLException,
                          DBException
Execute the specified SQL update

Parameters:
sql - The String SQL statement to execute
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

executeUpdate

public long executeUpdate(java.lang.String sql,
                          boolean rtnAutoIncrVal)
                   throws java.sql.SQLException,
                          DBException
Execute the specified SQL update

Parameters:
sql - The String SQL statement to execute
rtnAutoIncrVal - If auto-generated fields (ie. "auto_increment") should be returned
Returns:
The generated auto increment value or -1
Throws:
java.sql.SQLException - If an SQL error occurs
DBException - If a database error occurs

main

public static void main(java.lang.String[] argv)
DEBUG: Main entry point used for testing/debugging

Parameters:
argv - The command-line arguments