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

📄 jdbchelper.java

📁 把java对象映射成数据库表中的一条记录
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getLong() method on the ResultSet.
   * If the column has a null, this will return a zero.
   * @param column a value of type 'String'
   * @return a value of type 'long'
   * @exception SQLException if column is not found
   */
  public long getlong(String column)
          throws SQLException
    {
    i_columnName = column;
    long returnValue = i_resultSet.getLong(column);
    i_columnName = "";
    return returnValue;
    }

  /**
   * Calls the getLong() method on the ResultSet.
   * If the column has a null, this will return a zero.
   * @param column a value of type 'int'
   * @return a value of type 'long'
   * @exception SQLException if column is not found
   */
  public long getlong(int column)
          throws SQLException
    {
    i_columnIndex = column;
    long returnValue = i_resultSet.getLong(column);
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getShort() method on the ResultSet and wraps the result
   * in a Short.  If database has a null, null is returned.
   * @param column a value of type 'String'
   * @return a value of type 'Short'
   * @exception SQLException if column is not found
   */
  public Short getShort(String column)
          throws SQLException
    {
    i_columnName = column;
    Short returnValue = new Short(i_resultSet.getShort(column));
    i_columnName = "";
    if (i_resultSet.wasNull())
        {
        returnValue = null;
        }
    return returnValue;
    }


  /**
   * Calls the getShort() method on the ResultSet and wraps the resulting
   * in a Short.  If database has a null, null is returned.
   * @param column a value of type 'int'
   * @return a value of type 'Short'
   * @exception SQLException if column is not found
   */
  public Short getShort(int column)
          throws SQLException
    {
    i_columnIndex = column;
    Short returnValue = new Short(i_resultSet.getShort(column));
    i_columnIndex = 0;
    if (i_resultSet.wasNull())
        {
        returnValue = null;
        }
    return returnValue;
    }


  /**
   * Calls the getFloat() method on the ResultSet and wraps the resulting
   * float in a Float.  If database has a null, null is returned.
   * @param column a value of type 'String'
   * @return a value of type 'Float'
   * @exception SQLException if column is not found
   */
  public Float getFloat(String column)
          throws SQLException
    {
    i_columnName = column;
    Float returnValue = new Float(i_resultSet.getFloat(column));
    i_columnName = "";
    if (i_resultSet.wasNull())
        {
        returnValue = null;
        }
    return returnValue;
    }


  /**
   * Calls the getFloat() method on the ResultSet and wraps the resulting
   * float in a Float.  If database has a null, null is returned.
   * @param column a value of type 'int'
   * @return a value of type 'Float'
   * @exception SQLException if column is not found
   */
  public Float getFloat(int column)
          throws SQLException
    {
    i_columnIndex = column;
    Float returnValue = new Float(i_resultSet.getFloat(column));
    i_columnIndex = 0;
    if (i_resultSet.wasNull())
        {
        returnValue = null;
        }
    return returnValue;
    }


  /**
   * Calls the getFloat() method on the ResultSet.
   * If the column has a null, this will return a zero.
   * @param column a value of type 'String'
   * @return a value of type 'float'
   * @exception SQLException if column is not found
   */
  public float getfloat(String column)
          throws SQLException
    {
    i_columnName = column;
    float returnValue = i_resultSet.getFloat(column);
    i_columnName = "";
    return returnValue;
    }


  /**
   * Calls the getFloat() method on the ResultSet.
   * If the column has a null, this will return a zero.
   * @param column a value of type 'int'
   * @return a value of type 'float'
   * @exception SQLException if column is not found
   */
  public float getfloat(int column)
          throws SQLException
    {
    i_columnIndex = column;
    float returnValue = i_resultSet.getFloat(column);
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getDouble() method on the ResultSet and wraps the resulting
   * double in a Double.  If database has a null, null is returned.
   * @param column a value of type 'String'
   * @return a value of type 'Double'
   * @exception SQLException if column is not found
   */
  public Double getDouble(String column)
          throws SQLException
    {
    i_columnName = column;
    Double returnValue = new Double(i_resultSet.getDouble(column));
    i_columnName = "";
    if (i_resultSet.wasNull())
        {
        returnValue = null;
        }
    return returnValue;
    }


  /**
   * Calls the getDouble() method on the ResultSet and wraps the resulting
   * double in a Double.  If database has a null, null is returned.
   * @param column a value of type 'int'
   * @return a value of type 'Double'
   * @exception SQLException if column is not found
   */
  public Double getDouble(int column)
          throws SQLException
    {
    i_columnIndex = column;
    Double returnValue = new Double(i_resultSet.getDouble(column));
    i_columnIndex = 0;
    if (i_resultSet.wasNull())
        {
        returnValue = null;
        }
    return returnValue;
    }


  /**
   * Calls the getDouble() method on the ResultSet.
   * If the column has a null, this will return a zero.
   * @param column a value of type 'String'
   * @return a value of type 'double'
   * @exception SQLException if column is not found
   */
  public double getdouble(String column)
          throws SQLException
    {
    i_columnName = column;
    double returnValue = i_resultSet.getDouble(column);
    i_columnName = "";
    return returnValue;
    }


  /**
   * Calls the getDouble() method on the ResultSet.
   * If the column has a null, this will return a zero.
   * @param column a value of type 'int'
   * @return a value of type 'double'
   * @exception SQLException if column is not found
   */
  public double getdouble(int column)
          throws SQLException
    {
    i_columnIndex = column;
    double returnValue = i_resultSet.getDouble(column);
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getBigDecimal() method on the ResultSet.
   * If the column has a null, this will return null.
   * @param column a value of type String
   * @return a value of type BigDecimal
   * @exception SQLException if column is not found
   */
  public BigDecimal getBigDecimal(String column)
          throws SQLException
    {
    i_columnName = column;

    // WORKAROUND ****
    // Weblogic hasn't implemented ResultSet.getBigDecimal yet.
    // For now get it as a String and turn into a BigDecimal.
    String resultString = i_resultSet.getString(column);
    if (resultString == null)
        {
        return (BigDecimal) null;
        }
    BigDecimal returnValue = new BigDecimal(resultString);
    i_columnName = "";
    return returnValue;
    }


  /**
   * Calls the getBigDecimal() method on the ResultSet.
   * If the column has a null, this will return null.
   * @param column a value of type 'int'
   * @return a value of type 'BigDecimal'
   * @exception SQLException if column is not found
   */
  public BigDecimal getBigDecimal(int column)
          throws SQLException
    {
    i_columnIndex = column;

    // WORKAROUND ****
    // Weblogic hasn't implemented ResultSet.getBigDecimal yet.
    // For now get it as a String and turn into a BigDecimal.
    String resultString = i_resultSet.getString(column);
    if (resultString == null)
        {
        return (BigDecimal) null;
        }
    BigDecimal returnValue = new BigDecimal(resultString);
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getBytes() 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 'byte[]'
   * @exception SQLException if column is not found
   */
  public byte[] getBytes(int column)
          throws SQLException
    {
    i_columnIndex = column;
    byte[] returnValue = i_resultSet.getBytes(column);
    i_columnIndex = 0;
    return returnValue;
    }


  /**
   * Calls the getBytes() 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 'byte[]'
   * @exception SQLException if column is not found
   */
  public byte[] getBytes(String column)
          throws SQLException
    {
    i_columnName = column;
    byte[] returnValue = i_resultSet.getBytes(column);
    i_columnName = "";
    return returnValue;
    }


  /* ==========  Convenience Helper Methods  ========== */


  /**
   * Formats a String for use as a String in a DB query
   *   where a single quote is the delimiter.
   *
   * @param p_val the String to format
   * @return String with quotes escaped, and enclosing.
   * @see #delimitString
   */
  public static String delimitSingleQuote(String p_val)
    {
    return JDBCHelper.delimitString(p_val, SINGLE_QUOTE);
    }


  /**
   * Formats a String for use as a String in a DB query
   *   where a double quote is the delimiter.
   *
   * @param p_val the String to format
   * @return String with quotes escaped, and enclosing.
   * @see #delimitString
   */
  public static String delimitDoubleQuote(String p_val)
    {
    return JDBCHelper.delimitString(p_val, DOUBLE_QUOTE);
    }


  /**
   * STATIC CLASS METHOD:
   * Formats a String for use as a String in a DB query
   *   where the delimiter is as provided.
   *
   * @param p_val the String to format
   * @param p_delimiter the delimiter for formatting
   * @return String with quotes escaped, and enclosing.
   */
  public static String delimitString(String p_val, String p_delimiter)
    {
    String result = null;

    // replace occurrances of the delimiter with "double delimiters"
    // to "escape" the delimiter character within the string.
    result = JDBCHelper.replace(p_val,
                                p_delimiter,
                                (p_delimiter + p_delimiter));

    // pre-pend and post-pend the delimiter
    result = p_delimiter + result + p_delimiter;

    return result;
    }


  /**
   * This method should really be in a StringUtil class.
   * Replace occurrences of oldString with newString within the content text.
   * @param content The text String that will be acted upon (strings
   *                replaced).
   * @param oldString  The string that will be replaced.
   * @param newString  The string that will replace oldString.
   * @return returns the content string with the replaced values, as a
   *         String, or original content if bad parms.
   */
  public static String replace(String content,
                               String oldString,
                               String newString)
    {
    // if any parms null or too small, no point in doing anything...return
    // content
    if ((content == null) ||
        (oldString == null) ||
        (oldString.length() < 1) ||
        (newString == null))
        {
        return content;
        }
    // if content too small to contain oldString, no point in doing
    // anything...return content
    if (content.length() < oldString.length())
        {
        return content;
        }

    // Perform String replacement
    String newContent = content;
    int foundIndex = content.indexOf(oldString);

    // Recurse through the string, replacing ALL occurrences recurse rather
    // than loop to allow a replace that includes itself (eg "'" with "''" -
    // SQL escaping)
    if (foundIndex != -1)
        {
        try
            {
            newContent = content.substring(0, foundIndex)
                    + newString
                    + replace(
                        content.substring(foundIndex + oldString.length(),
                                          content.length()),
                        oldString,
                        newString);
            }
        catch (StringIndexOutOfBoundsException e)
            {
            Category.getInstance("JDBCHelper").error(
                "Exception occured in JDBCHelper.replace()... Ignoring.", e);
            }
        }
    return newContent;
    } // replace(...)


  /**
   * This method should really be in a StringUtil class.
   * For a given List, this method creates a comma-separated string list and
   * surrounds it with parenthesis.  It is suitable for use in any SQL clauses
   * where that formatting is required.<br>
   * e.g. WHERE IN (foo,bar,blah)
   * e.g. VALUES (tom,dick,harry)<br>
   *
   * This calls the String.valueOf() method on each List element to produce the values.
   *
   * @param objects a value of type 'List'
   * @return a value of type 'String'
   */
  public static String toSQLList(List objects)
    {
    if (objects == null || objects.size() < 1)
        {
        return "()";
        }

    // Create a comma delimited, paren enclosed string of all the objects in
    // the List.  All concrete implementations of List will inherit from
    // AbstractCollection where the #toString() method does this formatting
    // but uses [] instead of (), so we have to replace it. Elements are
    // converted to strings as by String.valueOf(Object).  So if Your list
    // contains:
    //
    // 1234
    // 5678
    // 9012
    //
    // ... the resulting String will be:
    // (1234, 5678, 9012)

    return objects.toString().replace('[', '(').replace(']', ')');
    }

    public String toString()
    {
       return "JDBCHelper #" + i_serialNum;
    }

  } // JDBCHelper

⌨️ 快捷键说明

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