📄 jdbchelper.java.craig
字号:
* @return a value of type 'int' * @exception SQLException if an error occurs */ public int executeUpdate(String sqlString) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { Statement statement; this.validateConnection(); if (LOG.isDebugEnabled()) { LOG.debug("[" + sqlString + "]"); } if (i_statement == null) { i_statement = i_connection.createStatement(); } else if (!i_reuseStatement) { i_statement.close(); i_statement = i_connection.createStatement(); } int resultCount = i_statement.executeUpdate(sqlString); if (LOG.isDebugEnabled()) { LOG.debug(resultCount + " rows impacted by sql statement."); } return resultCount; } /** * 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, SQLException { Statement statement; this.validateConnection(); if (LOG.isDebugEnabled()) { LOG.debug("[" + stmt + "]"); } if (i_statement != null) { // The connection is being reused, but not the statement i_statement.close(); } i_statement = stmt; int resultCount = stmt.executeUpdate(); if (LOG.isDebugEnabled()) { LOG.debug(resultCount + " rows impacted by prepared sql statement."); } return resultCount; } /** * Make sure the connection exists and is open. */ private void validateConnection() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { if ((i_connection == null) || i_connection.isClosed()) { i_statement = null; i_resultSet = null; Class.forName (i_driverClass).newInstance(); i_connection = DriverManager.getConnection (i_url, i_properties); i_connection.setAutoCommit(this.getShouldAutoCommit()); } } /** * Assume we have a connection. If it is closed we don't want to open it * by calling validateConnection(). * * @return a value of type 'boolean' */ public boolean isConnectionClosed() throws SQLException { return i_connection.isClosed(); } /** * 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, 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 (i_isInsideTransaction) { LOG.debug("JDBCHelper.close() not executed--inside transaction."); return; } else { LOG.debug("JDBCHelper.close()"); } if ((i_connection != null) && !i_connection.isClosed() ) { try { if (this.getShouldCommitOnClose() ) { i_connection.commit(); } } finally { // 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(); } } } // close() /** * Commit the transaction. * * @exception SQLException if a database access error occurs */ public void commit() throws SQLException { if (i_isInsideTransaction) { LOG.debug("JDBCHelper.commit() not executed -- inside a transaction"); } else { LOG.debug("JDBCHelper.commit()"); i_connection.commit(); } } /** * Rollback the transaction. * * @exception SQLException if a database access error occurs */ public void rollback() throws SQLException { LOG.info("JDBCHelper.rollback()"); i_isInsideTransaction = false; 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, SQLException { LOG.debug("JDBCHelper.beginTransaction()"); this.validateConnection(); i_isInsideTransaction = true; } /** * This method turns off the isInsideTransaction flag and commits and * closes the database. * * @exception SQLException if an error occurs */ public void endTransaction() throws SQLException { LOG.debug("JDBCHelper.endTransaction()"); i_isInsideTransaction = false; this.commit(); this.close(); } /** * 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; } /** * Print out the column names returned in the result set. * This can only be done if the resultSet is not null. */ public void printColumnNames() { ResultSetMetaData metaData = null; 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) { System.out.println( "SQLException occurred in JDBCHelper.printMetaData(): " + e); } } // printColumnNames() /** * Return a copy of myself without a database connection. * * @return a value of type 'Object' */ public Object clone() throws CloneNotSupportedException { JDBCHelper clone = (JDBCHelper) super.clone(); clone.i_statement = null; clone.i_resultSet = null; clone.i_connection = null; return clone; } /* ============== Column Value Accessors ================= */ /** * Get whatever type of object is in the given column. * If the column has a null, this will return false. * @param column a value of type 'String' * @return a value of type 'Object' */ public Object getObject(String column) throws SQLException { i_columnName = column; Object returnValue = i_resultSet.getObject(column); i_columnName = EMPTY_STRING; return returnValue; } /** * Get whatever type of object is in the given column. * If the column has a null, this will return false. * @param column a value of type 'int' * @return a value of type 'Object' */ public Object getObject(int column) throws SQLException { i_columnIndex = column; Object returnValue = i_resultSet.getObject(column); i_columnIndex = 0; return returnValue; } /** * Calls the getBoolean() method on the ResultSet. * If the column has a null, this will return false. * @param column a value of type 'String' * @return a value of type 'boolean' * @exception SQLException if column is not found */ public boolean getboolean(String column) throws SQLException { i_columnName = column; boolean returnValue = i_resultSet.getBoolean(column); i_columnName = EMPTY_STRING; return returnValue; } /** * Calls the getboolean() method on the ResultSet. * If the column has a null, this will return false. * @param column a value of type 'int' * @return a value of type 'boolean' * @exception SQLException if column is not found */ public boolean getboolean(int column) throws SQLException { i_columnIndex = column; boolean returnValue = i_resultSet.getBoolean(column); i_columnIndex = 0; return returnValue; } /** * Calls the getBoolean() method on the ResultSet and wraps the boolean in * a Boolean. If the column has a null, this will return a null. * @param column a value of type 'String' * @return a value of type 'Boolean' * @exception SQLException if column is not found */ public Boolean getBoolean(String column) throws SQLException { i_columnName = column; Boolean returnValue = new Boolean(i_resultSet.getBoolean(column)); i_columnName = EMPTY_STRING; return returnValue; } /** * Calls the getBoolean() method on the ResultSet and wraps the boolean in * a Boolean. If the column has a null, this will return a null. * @param column a value of type 'int' * @return a value of type 'Boolean' * @exception SQLException if column is not found */ public Boolean getBoolean(int column) throws SQLException { i_columnIndex = column; Boolean returnValue = new Boolean(i_resultSet.getBoolean(column)); i_columnIndex = 0; return returnValue; } /** * Calls the getString() method on the ResultSet and trims the result. * If the column has a null, this will return a null. * @param column a value of type 'String' * @return a value of type 'String' * @exception SQLException if column is not found * @see #getRawString */ public String getString(String column) throws SQLException { i_columnName = column; String result = i_resultSet.getString(column); if (result != null) { result = result.trim(); } i_columnName = EMPTY_STRING; return result; } /** * Calls the getString() method on the ResultSet and trims the result. * If the column has a null, this will return a null. * @param column a value of type 'int' * @return a value of type 'String' * @exception SQLException if column is not found * @see #getRawString */ public String getString(int column) throws SQLException { i_columnIndex = column; String result = i_resultSet.getString(column); if (result != null) { result = result.trim(); } i_columnIndex = 0; return result; } /** * Calls the getString() method on the ResultSet. * If the column has a null, this will return a null. * @param column a value of type 'String' * @return a value of type 'String' * @exception SQLException if column is not found */ public String getRawString(String column) throws SQLException { i_columnName = column; String returnValue = i_resultSet.getString(column); i_columnName = EMPTY_STRING; return returnValue; } /** * Calls the getString() method on the ResultSet. * If the column has a null, this will return a null. * @param column a value of type 'int' * @return a value of type 'String' * @exception SQLException if column is not found */ public String getRawString(int column) throws SQLException { i_columnIndex = column; String returnValue = i_resultSet.getString(column); i_columnIndex = 0; return returnValue; } /** * Calls the getTimestamp() method on the ResultSet. * If the column has a null, this will return a null. * @param column a value of type 'String' * @return a value of type 'Date' * @exception SQLException if column is not found */ public Timestamp getTimestamp(String column) throws SQLException { i_columnName = column; Timestamp returnValue = i_resultSet.getTimestamp(column); i_columnName = EMPTY_STRING; return returnValue; } /** * Calls the getTimestamp() method on the ResultSet. * If the column has a null, this will return a null. * @param column a value of type 'int' * @return a value of type 'Date' * @exception SQLException if column is not found */ public Timestamp getTimestamp(int column) throws SQLException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -