📄 simpletextpreparedstatement.java
字号:
//----------------------------------------------------------------------------//// Module: SimpleTextPreparedStatement.java//// Description: Implementation of the JDBC PreparedStatement interface//// Author: Karl Moss//// Copyright: (C) 1996,1997 Karl Moss. All rights reserved.// You may study, use, modify and distribute this example// for any purpose, provided that this copyright notice// appears in all copies. This example is provided WITHOUT// WARRANTY either expressed or implied.//----------------------------------------------------------------------------package jdbc.SimpleText;//----------------------------------------------------------------------------// A SQL statement is pre-compiled and stored in a// PreparedStatement object. This object can then be used to// efficiently execute this statement multiple times.//// Note: 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 setInt should be used.//// If arbitrary parameter type conversions are required then the// setObject method should be used with a target SQL type.//----------------------------------------------------------------------------// NOTE - this is an implementation of the JDBC API version 1.20//---------------------------------------------------------------------------import java.sql.*;import java.io.*;import java.util.Hashtable;import java.util.StringTokenizer;public class SimpleTextPreparedStatement extends SimpleTextStatement implements PreparedStatement{ //------------------------------------------------------------------------ // initialize //------------------------------------------------------------------------ public void initialize( SimpleTextIConnection con, String sql) throws SQLException { super.initialize(con); // Save the SQL statement sqlStatement = sql; // To prepare the statement, we must parse and validate. The act // of preparing does nothing more than validate the statement; it // does not save an execution plan. When the prepared statement is // executed, it is re-parsed and re-validated each time. parsedSQL = ownerConnection.parseSQL(sql); // Prepare the statement, indicating the we are only preparing prepare(true); // Create a new boundParams Hashtable boundParams = new Hashtable(); } //----------------------------------------------------------------------- // executeQuery - JDBC API // A prepared SQL query is executed and its ResultSet is returned. // // Returns a ResultSet that contains the data produced by the query //----------------------------------------------------------------------- public ResultSet executeQuery() throws SQLException { if (traceOn()) { trace("@executeQuery()"); } java.sql.ResultSet rs = null; // Execute the query. If execute returns true, then a result set // exists if (execute()) { rs = getResultSet(); } return rs; } // The overloaded executeQuery on the Statement object (which we // extend) is not valid for PreparedStatement or CallableStatement // objects. public ResultSet executeQuery( String sql) throws SQLException { throw new SQLException("Method is not valid"); } //----------------------------------------------------------------------- // executeUpdate - JDBC API // Execute a SQL INSERT, UPDATE or DELETE statement. In addition, // SQL statements that return nothing such as SQL DDL statements // can be executed. // // Returns either the row count for INSERT, UPDATE or DELETE; or 0 // for SQL statements that return nothing //----------------------------------------------------------------------- public int executeUpdate() throws SQLException { if (traceOn()) { trace("@executeUpdate()"); } int count = -1; // Execute the query. If execute returns false, then an update // count exists. if (execute() == false) { count = getUpdateCount(); } return count; } // The overloaded executeUpdate on the Statement object (which we // extend) is not valid for PreparedStatement or CallableStatement // objects. public int executeUpdate( String sql) throws SQLException { throw new SQLException("Method is not valid"); } //----------------------------------------------------------------------- // setNull - JDBC API // Set a parameter to SQL NULL. // // Note: You must specify the parameter's SQL type. // // @param parameterIndex the first parameter is 1, the second is 2, ... // @param sqlType SQL type code defined by java.sql.Types //----------------------------------------------------------------------- public void setNull( int parameterIndex, int sqlType) throws SQLException { // The SimpleText driver does not support null values throw DriverNotCapable(); } //----------------------------------------------------------------------- // setBoolean - JDBC API // Set a parameter to a Java boolean value. The driver converts this // to a SQL BIT value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setBoolean( int parameterIndex, boolean x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setByte - JDBC API // Set a parameter to a Java byte value. The driver converts this // to a SQL TINYINT value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setByte( int parameterIndex, byte x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setShort - JDBC API // Set a parameter to a Java short value. The driver converts this // to a SQL SMALLINT value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setShort( int parameterIndex, short x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setInt - JDBC API // Set a parameter to a Java int value. The driver converts this // to a SQL INTEGER value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setInt( int parameterIndex, int x) throws SQLException { // Validate the parameter index verify(parameterIndex); // Put the parameter into the boundParams Hashtable. Coerce the // data into a String boundParams.put(new Integer(parameterIndex), (new CommonValue(x)).getString()); } //----------------------------------------------------------------------- // setLong - JDBC API // Set a parameter to a Java long value. The driver converts this // to a SQL BIGINT value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setLong( int parameterIndex, long x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setFloat - JDBC API // Set a parameter to a Java float value. The driver converts this // to a SQL FLOAT value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setFloat( int parameterIndex, float x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setDouble - JDBC API // Set a parameter to a Java double value. The driver converts this // to a SQL DOUBLE value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setDouble( int parameterIndex, double x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setBigDecimal - JDBC API // Set a parameter to a java.math.BigDecimal value. The driver converts this // to a SQL NUMERIC value when it sends it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setBigDecimal( int parameterIndex, java.math.BigDecimal x) throws SQLException { // The SimpleText driver does not support this data type. We could // coerce the data, but an exception is will be thrown for now. throw DataTypeNotSupported(); } //----------------------------------------------------------------------- // setString - JDBC API // Set a parameter to a Java String value. The driver converts this // to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments // size relative to the driver's limits on VARCHARs) when it sends // it to the database. // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setString( int parameterIndex, String x) throws SQLException { // Validate the parameter index verify(parameterIndex); // Put the parameter into the boundParams Hashtable boundParams.put(new Integer(parameterIndex), x); } //----------------------------------------------------------------------- // setBytes - JDBC API // Set a parameter to a Java array of bytes. The driver converts this // to a SQL VARBINARY or LONGVARBINARY (depending on the arguments // size relative to the driver's limits on VARBINARYs) when it sends // // parameterIndex the first parameter is 1, the second is 2, ... // x the parameter value //----------------------------------------------------------------------- public void setBytes( int parameterIndex, byte x[]) throws SQLException { // Validate the parameter index verify(parameterIndex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -