📄 jdbchelper.java.pretal
字号:
i_statement = stmt;
i_resultSet = stmt.executeQuery();
}
/**
* Execute an update/insert/delete.
*
* @param sqlString a value of type 'String'
* @return a value of type 'int'
* @exception SQLException if an error occurs
*/
public int executeUpdate(String sqlString)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
NamingException,
SQLException
{
Statement statement;
this.validateConnection();
if (SQLLOG.isDebugEnabled())
{ // log the SQL
SQLLOG.debug(
"[" + sqlString + "] " + this);
}
this.initStatement();
return i_statement.executeUpdate(sqlString);
}
/**
* Execute an update/insert/delete on a PreparedStatement
*
* @param sqlString a value of type 'String'
* @return either the row count for INSERT, UPDATE or DELETE statements;
* or 0 for SQL statements that return nothing
* @exception SQLException if an error occurs
*/
public int executeUpdate(PreparedStatement stmt)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
NamingException,
SQLException
{
Statement statement;
this.validateConnection();
if (SQLLOG.isDebugEnabled())
{ // log the SQL
SQLLOG.debug(
"[" + i_sqlString + "] " + this);
}
this.closeStatement();
i_statement = stmt;
return stmt.executeUpdate();
}
/**
* Make sure the connection exists and is open.
*/
private final void validateConnection()
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
NamingException,
SQLException
{
if (i_inPool)
{
throw new SQLException(
"This JDBCHelper instance (" + this + ") has been returned to "
+ "the pool. You must retrieve and use another one.");
}
if (LOG.isDebugEnabled())
{
LOG.debug(
"Checking to see if connection is valid: " + i_connection);
}
if (i_connection == null || i_connection.isClosed())
{
// assumes standard close methods are used.
i_statement = null;
i_resultSet = null;
if (i_dataSourceName != null && i_dataSource == null)
{
// look up DataSource via JNDI
Context context = new InitialContext();
i_dataSource = (DataSource) context.lookup(i_dataSourceName);
context.close();
}
LOG.debug("About to get a new connection.");
if ( i_dataSource != null )
{
// get connection from datasource
i_connection = i_dataSource.getConnection();
}
else
{
// no datasource, create new connection from driver info.
if (LOG.isDebugEnabled())
{
LOG.debug("Driver Class: " + i_driverClass);
}
Class.forName (i_driverClass).newInstance();
i_connection = DriverManager.getConnection (i_url, i_properties);
}
// configure the connection properties.
i_connection.setAutoCommit(this.getShouldAutoCommit());
if (LOG.isDebugEnabled())
{
LOG.debug("Connection was created: " + i_connection);
}
}
}
/**
* Answer true if the connection is closed.
*
* @return a value of type 'boolean'
*/
public boolean isConnectionClosed()
throws SQLException
{
if (i_connection != null)
return i_connection.isClosed();
else
return true;
}
/**
* Return the current Connection. This was added to allow users to
* bypass the executeQuery() method. i.e. to use a PreparedStatement
* instead of a Statement, etc.
*
* @return a value of type 'Connection'
*/
public Connection getConnection()
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
NamingException,
SQLException
{
this.validateConnection();
return i_connection;
}
/**
* Return the current ResultSet
*
* @return a value of type 'ResultSet'
*/
public ResultSet getResultSet()
{
return i_resultSet;
}
/**
* Move the cursor to the next row in the result set.
*
* @return true if the new current row is valid; false if there are no
* more rows.
* @exception SQLException if a database access error occurs
*/
public boolean next()
throws SQLException
{
return i_resultSet.next();
}
/**
* Close the result set, statement, and connection.
* If the connection is from a pool driver, it is returned to the pool.
*
* @exception SQLException if a database access error occurs
*/
public void close()
throws SQLException
{
if (LOG.isDebugEnabled())
{
LOG.debug("at top of JDBCHelper.close(): " + this);
}
if (i_isInsideTransaction)
{
LOG.debug(
"Connection closing postponed (still inside a transaction)");
return;
}
if (i_connection != null &&
!i_connection.isClosed() )
{
try
{
// check if should commit before closing.
if (i_shouldCommitOnClose || i_commitButDontClose)
{
this.commit();
}
}
finally
{
if (i_commitButDontClose)
{
if (LOG.isDebugEnabled())
{
LOG.debug(
"Connection closing postponed (most likely because "
+ "it is in a JDBCHelperPool).");
}
}
else
{
// perform separately for the additional logging.
LOG.debug("close() is closing the ResultSet.");
this.closeResultSet();
LOG.debug("close() is closing the Statement.");
this.closeStatement();
LOG.debug("Connection closing.");
// If this connection came from a driver that is a pool,
// then this will return the connection to the pool.
// Else it will be freeing the connection (as it should).
i_connection.close();
i_connection = null;
LOG.debug("Connection closed.");
}
}
}
// If I came from a pool, I should return to it
// so I can be reused.
this.returnToPool();
} // close()
/**
* Close the SQL Statement.
* This is called when closing or changing the current SQL;
* and enables the connection to be reused.
*/
private void closeStatement()
throws SQLException
{
if (i_statement != null)
{
// if attempting to close the Statement,
// be sure to close any ResultSet first.
// assuming these methods used, and can't have RS without Stmt.
this.closeResultSet();
// The connection is being reused, but not the statement
i_statement.close();
i_statement = null;
}
}
/**
* Close the SQL ResultSet.
* This is called when closing or changing the current SQL;
* and enables the connection to be reused.
*/
private void closeResultSet()
throws SQLException
{
if (i_resultSet != null)
{
i_resultSet.close();
i_resultSet = null;
}
}
/**
* Initialize a Statement. If none exists, a new one is created.
* If reusing Statements, nothing is done;
* else the Statement is closed and a new one is created.
*/
private void initStatement()
throws SQLException
{
// if not reusing the Statement, close it. Then will get new one
// always checking result set just in case a select was previously done.
if ( !i_reuseStatement)
{
this.closeStatement();
}
else
{
// just in case select was done on this Statement (which is being reused).
this.closeResultSet();
}
// if statement exists, reuse it; else get a new one.
if (i_statement == null)
{
i_statement = i_connection.createStatement();
}
}
/**
* Commit the transaction.
*
* @exception SQLException if a database access error occurs
*/
public void commit()
throws SQLException
{
if (i_isInsideTransaction)
{
if (LOG.isDebugEnabled())
{
LOG.debug(
"Connection commit postponed (inside a transaction): " + this);
}
}
else if (i_closeButDontCommit)
{
if (LOG.isDebugEnabled())
{
LOG.debug(
"Connection commit turned off: " + this);
}
}
else
{
if (LOG.isDebugEnabled())
{
LOG.debug("Connection about to be committed: " + this);
}
i_connection.commit();
}
}
/**
* Rollback the transaction.
*
* @exception SQLException if a database access error occurs
*/
public void rollback()
throws SQLException
{
i_isInsideTransaction = false;
if (LOG.isDebugEnabled())
{
LOG.debug("Connection about to be rolled back: " + this);
}
i_connection.rollback();
}
/**
* Calling this method tells JDBCHelper to ignore commit() messages and
* close() messages until endTransaction() is called. rollback() messages
* are *not* ignored.
*/
public void beginTransaction()
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
NamingException,
SQLException
{
if (LOG.isDebugEnabled())
{
LOG.debug("Beginning a transaction: " + this);
}
this.validateConnection();
i_isInsideTransaction = true;
}
/**
* This method turns off the isInsideTransaction flag and commits the
* database changes. It is up to the user to close the JDBCHelper when
* done with it.
*
* @exception SQLException if an error occurs
*/
public void endTransaction()
throws SQLException
{
if (LOG.isDebugEnabled())
{
LOG.debug("Ending a transaction: " + this);
}
i_isInsideTransaction = false;
this.commit();
}
/**
* Return the name of the column that was unsuccessfully accessed.
*
* @return a value of type 'String'
*/
public String getColumnName()
{
return i_columnName;
}
/**
* Return the SQL string that was last executed.
*
* @return a value of type 'String'
*/
public String getSQLString()
{
return i_sqlString;
}
/**
* After this method is called, this JDBCHelper cannot be used until
* markOutOfPool() is called.
*/
public void markInPool()
{
i_inPool = true;
}
public void markOutOfPool()
{
i_inPool = false;
}
public boolean isInPool()
{
return i_inPool;
}
/**
* Print the column names returned in the result set out to System.out.
* This is only done if the ResultSet is not null.
* This is a debugging method; and has no practical application in production.
*/
public void printColumnNames()
{
ResultSetMetaData metaData = null;
if (i_resultSet == null)
{
return;
}
try
{
metaData = i_resultSet.getMetaData();
System.out.print("Column Names: ");
for (int i=1; i<=metaData.getColumnCount(); i++)
{
System.out.print(metaData.getColumnName(i));
if (i != metaData.getColumnCount())
{
System.out.print(", ");
} // if
} // for
System.out.println(); // add a carriage return
}
catch (SQLException e)
{
LOG.error(
"SQLException occurred in JDBCHelper#printColumnNames()", e);
}
} // printColumnNames()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -