⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbchelper.java.robmoore.done

📁 把java对象映射成数据库表中的一条记录
💻 DONE
📖 第 1 页 / 共 3 页
字号:
        }
    }


  /**
   * 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,
          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
    {
    i_category.debug("at top of JDBCHelper.close(): " + this);
    if (i_isInsideTransaction)
        {
        i_category.debug(
            "Connection closing postponed (still inside a transaction)");
        return;
        }
    if (i_connection != null &&
        !i_connection.isClosed() )
        {
        try
            {
            if ( this.getShouldCommitOnClose() )
                {
                this.commit();
                }
            }
        finally
            {
            if (i_shouldClose)
                {
                if (i_resultSet != null)
                    {
                    i_category.debug("ResultSet closing.");
                    i_resultSet.close();
                    }
                if (i_statement != null)
                    {
                    i_category.debug("Statement closing.");
                    i_statement.close();
                    }

                // 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_category.debug("Connection closing.");
                i_connection.close();
                i_category.debug("Connection closed.");
                }
            else
                {
                i_category.debug(
                    "Connection closing postponed (most likely because it is "
                    + "in a JDBCHelperPool).");
                }
            // If I came from a pool, I should return to it
            // so I can be reused.
            this.returnToPool();
            }
        }
    } // close()


  /**
   * Commit the transaction.
   *
   * @exception SQLException if a database access error occurs
   */
  public void commit()
          throws SQLException
    {
    if (i_isInsideTransaction)
        {
        i_category.debug(
            "Connection commit postponed (inside a transaction): " + this);
        }
    else
        {
        i_category.debug("Connection about to be committed: " + this);
        i_connection.commit();
        i_category.debug("Connection committed.");
        }
    }


  /**
   * Rollback the transaction.
   *
   * @exception SQLException if a database access error occurs
   */
  public void rollback()
          throws SQLException
    {
    i_isInsideTransaction = false;
    i_category.debug("Connection about to be rolled back.");
    i_connection.rollback();
    i_category.debug("Connection rolled back: " + this);
    }


  /**
   * 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
    {
    i_category.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
    {
    i_category.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 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)
        {
        i_category.error(
            "SQLException occurred in JDBCHelper.printColumnNames()", 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 = "";
    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 = "";
    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 = "";
    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
    {
    String result = this.getRawString(column);
    if (result != null)
        {
        result = result.trim();
        }
    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
    {
    String result = this.getRawString(column);
    if (result != null)
        {
        result = result.trim();
        }
    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 = "";
    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 = "";
    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
    {
    i_columnIndex = column;
    Timestamp returnValue =  i_resultSet.getTimestamp(column);
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getDate() 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 Date getDate(String column)
          throws SQLException
    {
    i_columnName = column;
    Date returnValue =  i_resultSet.getDate(column);
    i_columnName = "";
    return returnValue;
    }


  /**
   * Calls the getDate() 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 Date getDate(int column)
          throws SQLException

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -