📄 simpletextstatement.java
字号:
//----------------------------------------------------------------------------//// Module: SimpleTextStatement.java//// Description: Implementation of the JDBC Statement 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 Statement object is used for executing a static SQL statement// and obtaining the results produced by it.//// Only one ResultSet per Statement can be open at any point in// time. Therefore, if the reading of one ResultSet is interleaved with// the reading of another, each must have been generated by different// Statements.//----------------------------------------------------------------------------// NOTE - this is an implementation of the JDBC API version 1.20//---------------------------------------------------------------------------import java.sql.*;import java.util.Hashtable;import java.io.*;public class SimpleTextStatement extends SimpleTextObject implements SimpleTextIStatement{ //------------------------------------------------------------------------ // initialize //------------------------------------------------------------------------ public void initialize( SimpleTextIConnection con) throws SQLException { // Save the owning connection object ownerConnection = con; } //------------------------------------------------------------------------ // executeQuery - JDBC API // Execute a SQL statement that returns a single ResultSet. // // sql typically this is a static SQL SELECT statement // // Returns the table of data produced by the SQL statement //------------------------------------------------------------------------ public ResultSet executeQuery( String sql) throws SQLException { if (traceOn()) { trace("@executeQuery(" + sql + ")"); } java.sql.ResultSet rs = null; // Execute the query. If execute returns true, then a result set // exists if (execute(sql)) { rs = getResultSet(); } return rs; } //------------------------------------------------------------------------ // 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. // // sql a SQL INSERT, UPDATE or DELETE statement or a SQL // statement that returns nothing // // Returns either the row count for INSERT, UPDATE or DELETE; or 0 // for SQL statements that return nothing //------------------------------------------------------------------------ public int executeUpdate( String sql) throws SQLException { if (traceOn()) { trace("@executeUpdate(" + sql + ")"); } int count = -1; // Execute the query. If execute returns false, then an update // count exists. if (execute(sql) == false) { count = getUpdateCount(); } return count; } //------------------------------------------------------------------------ // close - JDBC API // In many cases, it is desirable to immediately release a // Statements's database and JDBC resources instead of waiting for // this to happen when it is automatically closed; the close // method provides this immediate release. // // Note: A Statement is automatically closed when it is // garbage collected. When a Statement is closed its current // ResultSet, if one exists, is also closed. //------------------------------------------------------------------------ public void close() throws SQLException { // If we have a current result set, close it if (currentResultSet != null) { currentResultSet.close(); currentResultSet = null; } } //------------------------------------------------------------------------ // getMaxFieldSize - JDBC API // The maxFieldSize limit (in bytes) is the maximum amount of data // returned for any column value; it only applies to BINARY, // VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and LONGVARCHAR // columns. If the limit is exceeded, the excess data is silently // discarded. // // Returns the current max column size limit; zero means unlimited //------------------------------------------------------------------------ public int getMaxFieldSize() throws SQLException { // The SimpleText driver does not have a limit on size return 0; } //------------------------------------------------------------------------ // setMaxFieldSize - JDBC API // The maxFieldSize limit (in bytes) is set to limit the size of // data that can be returned for any column value; it only applies // to BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR, and // LONGVARCHAR fields. If the limit is exceeded, the excess data // is silently discarded. // // max the new max column size limit; zero means unlimited //------------------------------------------------------------------------ public void setMaxFieldSize( int max) throws SQLException { // The SimpleText driver does not allow the maximum field size to // be set if (max != 0) { throw DriverNotCapable(); } } //------------------------------------------------------------------------ // getMaxRows - JDBC API // The maxRows limit is the maximum number of rows that a // ResultSet can contain. If the limit is exceeded, the excess // rows are silently dropped. // // Returns the current max row limit; zero means unlimited //------------------------------------------------------------------------ public int getMaxRows() throws SQLException { // The SimpleText driver does not have a limit on the number // of rows that can be returned return 0; } //------------------------------------------------------------------------ // setMaxRows - JDBC API // The maxRows limit is set to limit the number of rows that any // ResultSet can contain. If the limit is exceeded, the excess // rows are silently dropped. // // max the new max rows limit; zero means unlimited //------------------------------------------------------------------------ public void setMaxRows( int max) throws SQLException { // The SimpleText driver does not allow the maximum number of rows // to be set if (max != 0) { throw DriverNotCapable(); } } //------------------------------------------------------------------------ // setEscapeProcessing - JDBC API // If escape scanning is on (the default) the driver will do // escape substitution before sending the SQL to the database. // // enable true to enable; false to disable //------------------------------------------------------------------------ public void setEscapeProcessing( boolean enable) throws SQLException { // The SimpleText driver does not support escape sequence expansion if (enable) { throw DriverNotCapable(); } } //------------------------------------------------------------------------ // getQueryTimeout - JDBC API // The queryTimeout limit is the number of seconds the driver will // wait for a Statement to execute. If the limit is exceeded a // SQLException is thrown. // // Returns the current query timeout limit in seconds; zero means // unlimited //------------------------------------------------------------------------ public int getQueryTimeout() throws SQLException { // The SimpleText driver does not have a query timeout return 0; } //------------------------------------------------------------------------ // setQueryTimeout - JDBC API // The queryTimeout limit is the number of seconds the driver will // wait for a Statement to execute. If the limit is exceeded a // SQLException is thrown. // // seconds the new query timeout limit in seconds; zero means unlimited //------------------------------------------------------------------------ public void setQueryTimeout( int seconds) throws SQLException { // The SimpleText driver does not support query timeouts if (seconds != 0) { throw DriverNotCapable(); } } //------------------------------------------------------------------------ // cancel - JDBC API // Cancel can be used by one thread to cancel a statement that // is being executed by another thread. //------------------------------------------------------------------------ public void cancel() throws SQLException { // No-op for the SimpleText driver } //------------------------------------------------------------------------ // getWarnings - JDBC API // The first warning reported by calls on this Statement is // returned. A Statment's execute methods clear its SQLWarning // chain. Subsequent Statement warnings will be chained to this // SQLWarning. // // Note: The warning chain is automatically cleared each time // a statement is (re)executed. // // Note: If you are processing a ResultSet then any // warnings associated with ResultSet reads will be chained on the // ResultSet object. // // Returns the first SQLWarning or null //------------------------------------------------------------------------ public SQLWarning getWarnings() throws SQLException { return lastWarning; } //------------------------------------------------------------------------ // clearWarnings - JDBC API // After this call getWarnings returns null until a new warning is // reported for this Statement. //------------------------------------------------------------------------ public void clearWarnings() throws SQLException { setWarning(null); } //------------------------------------------------------------------------ // setWarning // Sets the given SQLWarning in the warning chain. If null, the // chain is reset //------------------------------------------------------------------------ protected void setWarning( SQLWarning warning) { if (warning == null) { lastWarning = null; } else { SQLWarning chain = lastWarning; // Find the end of the chain while (chain.getNextWarning() != null) { chain = chain.getNextWarning(); } // We're at the end of the chain. Add the new warning chain.setNextWarning(warning); } } //------------------------------------------------------------------------ // setCursorName - JDBC API // setCursorname defines the SQL cursor name that will be used by // subsequent Statement execute methods. This name can then be // used in SQL positioned update/delete statements to identify the // current row in the ResultSet generated by this statement. If // the database doesn't support positioned update/delete, this // method is a noop. // // Note: By definition, positioned update/delete // execution must be done by a different Statement than the one // which generated the ResultSet being used for positioning. Also, // cursor names must be unique within a Connection. // // name the new cursor name. //------------------------------------------------------------------------ public void setCursorName( String name) throws SQLException { // The SimpleText driver does not support positioned updates. Per // the spec, this is a no-op } //------------------------------------------------------------------------ // execute - JDBC API // Execute a SQL statement that may return multiple results. // Under some (uncommon) situations a single SQL statement may return // multiple result sets and/or update counts. Normally you can ignore // this, unless you're executing a stored procedure that you know may // return multiple results, or unless you're dynamically executing an // unknown SQL string. The "execute", "getMoreResults", "getResultSet" // and "getUpdateCount" methods let you navigate through multiple results. // // The "execute" method executes a SQL statement and indicates the // form of the first result. You can then use getResultSet or // getUpdateCount to retrieve the result, and getMoreResults to // move to any subsequent result(s). // // sql any SQL statement // Returns true if the first result is a ResultSet; false if it is an // integer //------------------------------------------------------------------------ public boolean execute( String sql) throws SQLException { resultSetColumns = null; // Convert the SQL statement into native syntax sql = ownerConnection.nativeSQL(sql); // Save the SQL statement sqlStatement = sql; // First, parse the sql statement into a String array parsedSQL = ownerConnection.parseSQL(sql); // Now validate the SQL statement and execute it. // Returns true if a result set exists. boolean rc = prepare(false); return rc; } //------------------------------------------------------------------------ // getResultSet - JDBC API // Returns the current result as a ResultSet. It // should only be called once per result. // // Returns the current result as a ResultSet; null if it is an integer //------------------------------------------------------------------------ public ResultSet getResultSet() throws SQLException { // If there are no column to be returned, return null if (resultSetColumns == null) { return null; } SimpleTextResultSet rs = new SimpleTextResultSet(); rs.initialize(this, resultSetCatalog, resultSetTable, resultSetColumns, resultSetFilter); // Save our current result set currentResultSet = rs; return rs; } //------------------------------------------------------------------------ // getUpdateCount - JDBC API // getUpdateCount returns the current result, which should be an // integer value. It should only be called once per result.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -