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

📄 jdbcpreparedstatement.java

📁 一个可以在applet窗体上持行sql语句并显示返回结果的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   * standard interface.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the java input stream which contains the
   * UNICODE parameter value
   * @param length the number of bytes in the stream
   * @exception SQLException if a database access error occurs
   * @deprecated
   */
  public void setUnicodeStream(int paramIndex,InputStream x,int length)
  throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setAsciiStream(paramIndex,x,length);
  }
  /**
   * Sets the designated parameter to the given input stream, which will have
   * the specified number of bytes.
   * When a very large binary value is input to a LONGVARBINARY
   * parameter, it may be more practical to send it via a
   * java.io.InputStream. JDBC will read the data from the stream
   * as needed, until it reaches end-of-file.
   *
   * <P><B>Note:</B> This stream object can either be a standard
   * Java stream object or your own subclass that implements the
   * standard interface.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the java input stream which contains the binary parameter value
   * @param length the number of bytes in the stream
   * @exception SQLException if a database access error occurs
   */
  public void setBinaryStream(int parameterIndex,InputStream x,int length)
  throws SQLException {
    if(Trace.TRACE) Trace.trace();
    byte b[]=new byte[length];  // todo: is this correct? what if length=0?
    try {
      x.read(b,0,length);
      x.close();
    } catch(IOException e) {
      throw Trace.error(Trace.INPUTSTREAM_ERROR,e.getMessage());
    }
    setBytes(parameterIndex,b);
  }
  /**
   * Clears the current parameter values immediately.
   * <P>In general, parameter values remain in force for repeated use of a
   * Statement. Setting a parameter value automatically clears its
   * previous value.  However, in some cases it is useful to immediately
   * release the resources used by the current parameter values; this can
   * be done by calling clearParameters.
   */
  public void clearParameters() {
    if(Trace.TRACE) Trace.trace();
    vParameter.removeAllElements();
  }
  /**
   * <p>Sets the value of a parameter using an object. The second
   * argument must be an object type; for integral values, the
   * java.lang equivalent objects should be used.
   *
   * <p>The given Java object will be converted to the targetSqlType
   * before being sent to the database.
   *
   * If the object has a custom mapping (is of a class implementing SQLData),
   * the JDBC driver should call its method <code>writeSQL</code> to write it
   * to the SQL data stream.
   * If, on the other hand, the object is of a class implementing
   * Ref, Blob, Clob, Struct,
   * or Array, the driver should pass it to the database as a value of the
   * corresponding SQL type.
   *
   * <p>Note that this method may be used to pass datatabase-
   * specific abstract data types.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the object containing the input parameter value
   * @param targetSqlType the SQL type (as defined in java.sql.Types) to be
   * sent to the database. The scale argument may further qualify this type.
   * @param scale for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types,
   * this is the number of digits after the decimal point.  For all other
   * types, this value will be ignored.
   * @exception SQLException if a database access error occurs
   */
  public void setObject(int i,Object x,int type,int scale) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setObject(i,x,type);
  }
 /**
   * Sets the value of the designated parameter with the given object.
   * This method is like setObject above, except that it assumes a scale
   * of zero.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the object containing the input parameter value
   * @param targetSqlType the SQL type (as defined in java.sql.Types) to be
   *                      sent to the database
   * @exception SQLException if a database access error occurs
   */
  public void setObject(int parameterIndex,Object x,int type)
  throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
      return;
    }
    x=Column.convertObject(x,type);
    setObjectInType(parameterIndex,x,type);
  }
  /**
   * <p>Sets the value of a parameter using an object; use the
   * java.lang equivalent objects for integral values.
   *
   * <p>The JDBC specification specifies a standard mapping from
   * Java Object types to SQL types.  The given argument java object
   * will be converted to the corresponding SQL type before being
   * sent to the database.
   *
   * <p>Note that this method may be used to pass datatabase-
   * specific abstract data types, by using a Driver-specific Java
   * type.
   *
   * If the object is of a class implementing SQLData,
   * the JDBC driver should call its method <code>writeSQL</code> to write it
   * to the SQL data stream.
   * If, on the other hand, the object is of a class implementing
   * Ref, Blob, Clob, Struct,
   * or Array, then the driver should pass it to the database as a value of the
   * corresponding SQL type.
   *
   * This method throws an exception if there is an ambiguity, for example, if
   * the object is of a class implementing more than one of those interfaces.
   *
   * <P><font color="#009900">
   * In Hypersonic SQL, this function checks if the object is of one of the
   * Java SQL Types (Integer, String, Double,...) and if yes, the function will
   * call the apropriate setXXX function. If an object should be inserted
   * with data type 'Object' (OTHER), then the function
   * setObject(i,x,Types.OTHER) should be called.
   * </font><P>
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the object containing the input parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setObject(int parameterIndex,Object x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
      return;
    }
    int type=Types.OTHER;
    if(x instanceof String) {
      type=Types.VARCHAR;
    } else if (x instanceof BigDecimal) {
      type=Types.NUMERIC;
    } else if(x instanceof Integer) {
      type=Types.INTEGER;
    } else if(x instanceof Long) {
      type=Types.BIGINT;
    } else if(x instanceof Float) {
      type=Types.REAL;
    } else if(x instanceof Double) {
      type=Types.DOUBLE;
    } else if(x instanceof byte[]) {
      type=Types.BINARY;
    } else if(x instanceof java.sql.Date) {
      type=Types.DATE;
    } else if(x instanceof Time) {
      type=Types.TIME;
    } else if(x instanceof Timestamp) {
      type=Types.TIMESTAMP;
    } else if(x instanceof Boolean) {
      type=Types.BIT;
    } else if(x instanceof Byte) {
      type=Types.TINYINT;
    } else if(x instanceof Short) {
      type=Types.SMALLINT;
    }
    setObjectInType(parameterIndex,x,type);
  }
  /**
   * Executes any kind of SQL statement.
   * Some prepared statements return multiple results; the execute
   * method handles these complex statements as well as the simpler
   * form of statements handled by executeQuery and executeUpdate.
   *
   * @exception SQLException if a database access error occurs
   * @see Statement#execute
   */
  public boolean execute() throws SQLException {
    if(Trace.TRACE) Trace.trace();
    return super.execute(build());
  }

  private void setParameter(int i,String s) {
    if(Trace.TRACE) Trace.trace();
    if(vParameter.size()<i) {
      vParameter.setSize(i);
    }
    vParameter.setElementAt(s,--i);
  }
  private String getParameter(int i) {
    if(i>=vParameter.size()) {
      return null;
    }
    return (String)vParameter.elementAt(i);
  }
  private String build() {
    if(vParameter.isEmpty()) {
      return sSql;
    }
    StringBuffer s=new StringBuffer();
    int i=0,l=sSql.length();
    boolean bSkip=false,bSkip2=false;
    for(int j=0;j<l;j++) {
      char c=sSql.charAt(j);
      if(c=='?' && !bSkip && !bSkip2) {
        s.append(getParameter(i++));
      } else {
        if(c=='\'' && !bSkip2) {
          bSkip=!bSkip;
        } else if(c=='"' && !bSkip) {
          bSkip2=!bSkip2;
        }
        s.append(c);
      }
    }
    return s.toString();
  }
  private void setNull(int parameterIndex) {
    setParameter(parameterIndex,"NULL");
  }

  // implementation of CallableStatement
  /**
   * Registers the OUT parameter in ordinal position
   * <code>parameterIndex</code> to the JDBC type 
   * <code>sqlType</code>.  All OUT parameters must be registered
   * before a stored procedure is executed.
   * <p>
   * The JDBC type specified by <code>sqlType</code> for an OUT
   * parameter determines the Java type that must be used
   * in the <code>get</code> method to read the value of that parameter.
   * <p>
   * If the JDBC type expected to be returned to this output parameter
   * is specific to this particular database, <code>sqlType</code>
   * should be <code>java.sql.Types.OTHER</code>.  The method
   * {@link #getObject} retrieves the value.
   * @param parameterIndex the first parameter is 1, the second is 2, 
   * and so on
   * @param sqlType the JDBC type code defined by <code>java.sql.Types</code>.
   * If the parameter is of type Numeric or Decimal, the version of
   * <code>registerOutParameter</code> that accepts a scale value 
   * should be used.
   * @exception SQLException if a database access error occurs
   */
  public void registerOutParameter(int parameterIndex,int sqlType)
  throws SQLException {
    throw getNotSupported();
  }
  /**
   * Registers the parameter in ordinal position
   * <code>parameterIndex</code> to be of JDBC type
   * <code>sqlType</code>.  This method must be called
   * before a stored procedure is executed.
   * <p>
   * The JDBC type specified by <code>sqlType</code> for an OUT
   * parameter determines the Java type that must be used
   * in the <code>get</code> method to read the value of that parameter.
   * <p>
   * This version of <code>registerOutParameter</code> should be
   * used when the parameter is of JDBC type <code>NUMERIC</code>
   * or <code>DECIMAL</code>.
   * @param parameterIndex the first parameter is 1, the second is 2,
   * and so on
   * @param sqlType SQL type code defined by <code>java.sql.Types</code>.
   * @param scale the desired number of digits to the right of the
   * decimal point.  It must be greater than or equal to zero.
   * @exception SQLException if a database access error occurs
   */
  public void registerOutParameter(int parameterIndex,int sqlType,int scale)
  throws SQLException {
    throw getNotSupported();
  }
  /**
   * Indicates whether or not the last OUT parameter read had the value of
   * SQL NULL.  Note that this method should be called only after
   * calling the <code>get</code> method; otherwise, there is no value to use
   * in determining whether it is <code>null</code> or not.
   * @return <code>true</code> if the last parameter read was SQL NULL;
   * <code>false</code> otherwise.
   * @exception SQLException if a database access error occurs
   */
  public boolean wasNull() throws SQLException {
    throw getNotSupported();
  }
  /**
   * Retrieves the value of a JDBC <code>CHAR</code>, <code>VARCHAR</code>,
   * or <code>LONGVARCHAR</code> parameter as a <code>String</code> in
   * the Java programming language.
   * <p>
   * For the fixed-length type JDBC CHAR, the <code>String</code> object
   * returned has exactly the same value the JDBC CHAR value had in the
   * database, including any padding added by the database.
   * @param parameterIndex the first parameter is 1, the second is 2,
   * and so on
   * @return the parameter value. If the value is SQL NULL, the result
   * is <code>null</code>.
   * @exception SQLException if a database access error occurs
   */
  public String getString(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  /**
   * Gets the value of a JDBC BIT parameter as a <code>boolean</code>
   * in the Java programming language.
   * @param parameterIndex the first parameter is 1, the second is 2,
   * and so on
   * @return the parameter value.  If the value is SQL NULL, the result
   * is <code>false</code>.
   * @exception SQLException if a database access error occurs
   */
  public boolean getBoolean(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public byte getByte(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public short getShort(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public int getInt(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public long getLong(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public float getFloat(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public double getDouble(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public BigDecimal getBigDecimal(int parameterIndex,int scale)
  throws SQLException {
    throw getNotSupported();
  }
  public byte[] getBytes(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public java.sql.Date getDate(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }
  public Time getTime(int parameterIndex) throws SQLException {
    throw getNotSupported();
  }

⌨️ 快捷键说明

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