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

📄 jdbcpreparedstatement.java

📁 一个可以在applet窗体上持行sql语句并显示返回结果的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * jdbcPreparedStatement.java
 */

package org.hsql;
import java.sql.*;
import java.math.BigDecimal;
import java.io.*;
import java.util.*;

/**
 * <P><font color="#009900">
 * In Hypersonic SQL, the class jdbcPreparedStatement implements both the
 * PreparedStatement and the CallableStatement interfaces.
 * <P>
 * CallableStatement is used to execute SQL stored procedures,<BR>
 * PreparedStatement is used to execute SQL statement with parameters.
 * <P>
 * There are currently no stored procedures in Hypersonic SQL.
 * The reason is that stored procedures are currently extremely
 * proprietary. If a stored procedure was made for one database type,
 * it will not work in a database of another vendor.
 * <P>
 * The functions for working with OUT parameters are not implemented
 * because there are no statements that returns OUT parameters. All
 * statements in Hypersonic SQL returns a ResultSet or a Update Count.
 * <P>
 * For other databases it may be required to use OUT parameters
 * when calling stored procedures or vendor specific statements.
 * </font><P>
 * A PreparedStatement represents a precompiled SQL statement.
 * <P>A SQL statement is pre-compiled and stored in a
 * PreparedStatement object. This object can then be used to
 * execute this statement multiple times.
 *
 * <P><B>Note:</B> The setXXX methods for setting IN parameter values
 * must specify types that are compatible with the defined SQL type of
 * the input parameter. For instance, if the IN parameter has SQL type
 * Integer, then the method <code>setInt</code> should be used.
 *
 * <p>If arbitrary parameter type conversions are required, the method
 * <code>setObject</code> should be used with a target SQL type.
 * <br>
 * Example of setting a parameter; <code>con</code> is an active connection
 * <pre><code>
 *   PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
 *                                     SET SALARY = ? WHERE ID = ?");
 *   pstmt.setBigDecimal(1, 153833.00)
 *   pstmt.setInt(2, 110592)
 * </code></pre>
 *
 * <P>
 * The interface CallableStatement is used to execute SQL
 * stored procedures.  JDBC provides a stored procedure
 * SQL escape that allows stored procedures to be called in a standard
 * way for all RDBMSs. This escape syntax has one form that includes
 * a result parameter and one that does not. If used, the result
 * parameter must be registered as an OUT parameter. The other parameters
 * can be used for input, output or both. Parameters are referred to
 * sequentially, by number. The first parameter is 1.
 * <P>
 * <blockquote><pre>
 *   {?= call &lt;procedure-name&gt;[&lt;arg1&gt;,&lt;arg2&gt;, ...]}
 *   {call &lt;procedure-name&gt;[&lt;arg1&gt;,&lt;arg2&gt;, ...]}
 * </pre></blockquote>
 * <P>
 * IN parameter values are set using the set methods inherited from
 * {@link PreparedStatement}.  The type of all OUT parameters must be
 * registered prior to executing the stored procedure; their values
 * are retrieved after execution via the <code>get</code> methods provided here.
 * <P>
 * A <code>CallableStatement</code> can return one {@link ResultSet} or
 * multiple <code>ResultSet</code> objets.  Multiple
 * <code>ResultSet</code> objects are handled using operations
 * inherited from {@link Statement}.
 * <P>
 * For maximum portability, a call's <code>ResultSet</code> objects and
 * update counts should be processed prior to getting the values of output
 * parameters.
 *
 * @see jdbcResultSet
 */

public class jdbcPreparedStatement extends jdbcStatement
implements PreparedStatement,CallableStatement {
  private String sSql;
  private Vector vParameter;

  /**
	 * Executes the SQL query in this <code>PreparedStatement</code> object
	 * and returns the result set generated by the query.
   *
   * @return a ResultSet that contains the data produced by the
   * query; never null
   * @exception SQLException if a database access error occurs
   */
  public ResultSet executeQuery() throws SQLException {
    if(Trace.TRACE) Trace.trace();
    return super.executeQuery(build());
  }
  /**
   * Executes the SQL INSERT, UPDATE or DELETE statement
   * in this <code>PreparedStatement</code> object.
   * In addition,
   * SQL statements that return nothing, such as SQL DDL statements,
   * can be executed.
   *
   * @return either the row count for INSERT, UPDATE or DELETE statements;
   * or 0 for SQL statements that return nothing
   * @exception SQLException if a database access error occurs
   */
  public int executeUpdate() throws SQLException {
    if(Trace.TRACE) Trace.trace();
    return super.executeUpdate(build());
  }
  /**
   * Sets the designated parameter to SQL NULL.
   *
   * <P><B>Note:</B> You must specify the parameter's SQL type.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param sqlType the SQL type code defined in java.sql.Types
   * @exception SQLException if a database access error occurs
   */
  public void setNull(int parameterIndex,int sqlType) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setNull(parameterIndex);
  }
  /**
   * Sets the designated parameter to a Java boolean value.  The driver
   * converts this to an SQL BIT value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setBoolean(int parameterIndex,boolean x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,x ? "TRUE" : "FALSE");
  }
  /**
   * Sets the designated parameter to a Java byte value.  The driver
   * converts this to an SQL TINYINT value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setByte(int parameterIndex,byte x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,""+x);
  }
  /**
   * Sets the designated parameter to a Java short value.  The driver
   * converts this to an SQL SMALLINT value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setShort(int parameterIndex,short x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,""+x);
  }
  /**
   * Sets the designated parameter to a Java int value.  The driver
   * converts this to an SQL INTEGER value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setInt(int parameterIndex,int x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,""+x);
  }
  /**
   * Sets the designated parameter to a Java long value.  The driver
   * converts this to an SQL BIGINT value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setLong(int parameterIndex,long x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,""+x);
  }
  /**
   * Sets the designated parameter to a Java float value.  The driver
   * converts this to an SQL FLOAT value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setFloat(int parameterIndex,float x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,""+x);
  }
  /**
   * Sets the designated parameter to a Java double value.  The driver
   * converts this to an SQL DOUBLE value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setDouble(int parameterIndex,double x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    setParameter(parameterIndex,""+x);
  }
  /**
   * Sets the designated parameter to a java.lang.BigDecimal value.
   * The driver converts this to an SQL NUMERIC value when
   * it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setBigDecimal(int parameterIndex,BigDecimal x)
  throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setParameter(parameterIndex,x.toString());
    }
  }
  /**
   * Sets the designated parameter to a Java String value.  The driver
   * converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the
   * argument's size relative to the driver's limits on VARCHARs) when it sends
   * it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setString(int parameterIndex,String x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setParameter(parameterIndex,Column.createString(x));
    }
  }
  /**
   * Sets the designated parameter to a Java array of bytes.  The driver
   * converts this to an SQL VARBINARY or LONGVARBINARY (depending on the
   * argument's size relative to the driver's limits on VARBINARYs)
   * when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setBytes(int parameterIndex,byte[] x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setParameter(parameterIndex,"'"+ByteArray.createString(x)+"'");
    }
  }
  /**
   * Sets the designated parameter to a java.sql.Date value.  The driver
   * converts this to an SQL DATE value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setDate(int parameterIndex,java.sql.Date x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setParameter(parameterIndex,"'"+x.toString()+"'");
    }
  }
  /**
   * Sets the designated parameter to a java.sql.Time value.  The driver
   * converts this to an SQL TIME value when it sends it to the database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setTime(int parameterIndex,Time x) throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setParameter(parameterIndex,"'"+x.toString()+"'");
    }
  }
  /**
   * Sets the designated parameter to a java.sql.Timestamp value.  The driver
   * converts this to an SQL TIMESTAMP value when it sends it to the
   * database.
   *
   * @param parameterIndex the first parameter is 1, the second is 2, ...
   * @param x the parameter value
   * @exception SQLException if a database access error occurs
   */
  public void setTimestamp(int parameterIndex,Timestamp x)
  throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setParameter(parameterIndex,"'"+x.toString()+"'");
    }
  }
  /**
   * Sets the designated parameter to the given input stream, which will have
   * the specified number of bytes.
   * When a very large ASCII value is input to a LONGVARCHAR
   * 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.  The JDBC driver will
   * do any necessary conversion from ASCII to the database char format.
   *
   * <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 that contains the ASCII parameter value
   * @param length the number of bytes in the stream
   * @exception SQLException if a database access error occurs
   */
  public void setAsciiStream(int parameterIndex,InputStream x,int length)
  throws SQLException {
    if(Trace.TRACE) Trace.trace();
    if(x==null) {
      setNull(parameterIndex);
    } else {
      setString(parameterIndex,StringConverter.InputStreamToString(x));
    }
  }
  /**
   * Sets the designated parameter to the given input stream, which will have
   * the specified number of bytes.
   * When a very large UNICODE value is input to a LONGVARCHAR
   * 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.  The JDBC driver will
   * do any necessary conversion from UNICODE to the database char format.
   * The byte format of the Unicode stream must be Java UTF-8, as
   * defined in the Java Virtual Machine Specification.
   *
   * <P><B>Note:</B> This stream object can either be a standard
   * Java stream object or your own subclass that implements the

⌨️ 快捷键说明

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