Class ServerPreparedStatement
- java.lang.Object
-
- org.mariadb.jdbc.Statement
-
- org.mariadb.jdbc.BasePreparedStatement
-
- org.mariadb.jdbc.ServerPreparedStatement
-
- All Implemented Interfaces:
AutoCloseable
,PreparedStatement
,Statement
,Wrapper
- Direct Known Subclasses:
BaseCallableStatement
public class ServerPreparedStatement extends BasePreparedStatement
Server prepare statement. command will generate COM_STMT_PREPARE + COM_STMT_EXECUTE (+ COM_STMT_CLOSE)
-
-
Field Summary
-
Fields inherited from class org.mariadb.jdbc.BasePreparedStatement
batchParameters, isCommandInsert, parameters, prepareResult, sql
-
Fields inherited from class org.mariadb.jdbc.Statement
autoGeneratedKeys, canUseServerMaxRows, canUseServerTimeout, closed, closeOnCompletion, con, currResult, escape, fetchSize, isInsertDuplicate, lastSql, localInfileInputStream, lock, maxRows, queryTimeout, results, resultSetConcurrency, resultSetType
-
Fields inherited from interface java.sql.Statement
CLOSE_ALL_RESULTS, CLOSE_CURRENT_RESULT, EXECUTE_FAILED, KEEP_CURRENT_RESULT, NO_GENERATED_KEYS, RETURN_GENERATED_KEYS, SUCCESS_NO_INFO
-
-
Constructor Summary
Constructors Constructor Description ServerPreparedStatement(String sql, Connection con, ReentrantLock lock, boolean canUseServerTimeout, boolean canUseServerMaxRows, boolean canCachePrepStmts, int autoGeneratedKeys, int resultSetType, int resultSetConcurrency, int defaultFetchSize)
Server prepare statement constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBatch()
Adds a set of parameters to thisPreparedStatement
object's batch of commands.void
close()
Releases thisStatement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.boolean
execute()
Executes the SQL statement in thisPreparedStatement
object, which may be any kind of SQL statement.int[]
executeBatch()
Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.protected void
executeInternal()
Execute command with parameterslong[]
executeLargeBatch()
Execute batch, like executeBatch(), with returning results with long[].long
executeLargeUpdate()
Executes the SQL statement in thisPreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
orDELETE
; or an SQL statement that returns nothing, such as a DDL statement.ResultSet
executeQuery()
Executes the SQL query in thisPreparedStatement
object and returns theResultSet
object generated by the query.int
executeUpdate()
Executes the SQL statement in thisPreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
orDELETE
; or an SQL statement that returns nothing, such as a DDL statement.ResultSetMetaData
getMetaData()
Retrieves aResultSetMetaData
object that contains information about the columns of theResultSet
object that will be returned when thisPreparedStatement
object is executed.ParameterMetaData
getParameterMetaData()
Retrieves the number, types and properties of thisPreparedStatement
object's parameters.protected void
handleParameterOutput()
Handle output parameter result-set (only for CallableStatement)void
reset()
reset prepare statement in case of a failover.void
setLargeMaxRows(long max)
Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number.void
setMaxRows(int max)
Sets the limit for the maximum number of rows that anyResultSet
object generated by thisStatement
object can contain to the given number.void
setQueryTimeout(int seconds)
Sets the number of seconds the driver will wait for aStatement
object to execute to the given number of seconds.String
toString()
protected void
validParameters()
Validated that all parameters have been set.-
Methods inherited from class org.mariadb.jdbc.BasePreparedStatement
addBatch, checkIfInsertCommand, clearBatch, clearParameters, execute, execute, execute, execute, executeLargeUpdate, executeLargeUpdate, executeLargeUpdate, executeLargeUpdate, executeQuery, executeUpdate, executeUpdate, executeUpdate, executeUpdate, getLastSql, getMeta, setArray, setAsciiStream, setAsciiStream, setAsciiStream, setBigDecimal, setBinaryStream, setBinaryStream, setBinaryStream, setBlob, setBlob, setBlob, setBoolean, setByte, setBytes, setCharacterStream, setCharacterStream, setCharacterStream, setClob, setClob, setClob, setDate, setDate, setDouble, setFloat, setInt, setLong, setNCharacterStream, setNCharacterStream, setNClob, setNClob, setNClob, setNString, setNull, setNull, setObject, setObject, setObject, setObject, setObject, setParameter, setParameters, setPrepareResult, setRef, setRowId, setShort, setSQLXML, setString, setTime, setTime, setTimestamp, setTimestamp, setUnicodeStream, setURL, updateMeta
-
Methods inherited from class org.mariadb.jdbc.Statement
abort, cancel, checkIfInsertDuplicateCommand, checkNotClosed, clearWarnings, closeOnCompletion, enquoteIdentifier, enquoteLiteral, enquoteNCharLiteral, escapeTimeout, executeInternalBatchPipeline, executeInternalBatchStandard, fetchRemaining, getConnection, getFetchDirection, getFetchSize, getGeneratedKeys, getLargeMaxRows, getLargeUpdateCount, getMaxFieldSize, getMaxRows, getMoreResults, getMoreResults, getQueryTimeout, getResultSet, getResultSetConcurrency, getResultSetHoldability, getResultSetType, getUpdateCount, getWarnings, isClosed, isCloseOnCompletion, isPoolable, isSimpleIdentifier, isWrapperFor, setCursorName, setEscapeProcessing, setFetchDirection, setFetchSize, setLocalInfileInputStream, setMaxFieldSize, setPoolable, unwrap
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.sql.Statement
cancel, clearWarnings, closeOnCompletion, enquoteIdentifier, enquoteLiteral, enquoteNCharLiteral, getConnection, getFetchDirection, getFetchSize, getGeneratedKeys, getLargeMaxRows, getLargeUpdateCount, getMaxFieldSize, getMaxRows, getMoreResults, getMoreResults, getQueryTimeout, getResultSet, getResultSetConcurrency, getResultSetHoldability, getResultSetType, getUpdateCount, getWarnings, isClosed, isCloseOnCompletion, isPoolable, isSimpleIdentifier, setCursorName, setEscapeProcessing, setFetchDirection, setFetchSize, setMaxFieldSize, setPoolable
-
Methods inherited from interface java.sql.Wrapper
isWrapperFor, unwrap
-
-
-
-
Constructor Detail
-
ServerPreparedStatement
public ServerPreparedStatement(String sql, Connection con, ReentrantLock lock, boolean canUseServerTimeout, boolean canUseServerMaxRows, boolean canCachePrepStmts, int autoGeneratedKeys, int resultSetType, int resultSetConcurrency, int defaultFetchSize) throws SQLException
Server prepare statement constructor- Parameters:
sql
- commandcon
- connectionlock
- thread safe lockcanUseServerTimeout
- can server use timeoutcanUseServerMaxRows
- can server use max rowscanCachePrepStmts
- can server cache prepared statementautoGeneratedKeys
- must command return automatically generated keysresultSetType
- resultset typeresultSetConcurrency
- resultset concurrencydefaultFetchSize
- default fetch size- Throws:
SQLException
- if prepare fails
-
-
Method Detail
-
executeInternal
protected void executeInternal() throws SQLException
Execute command with parameters- Throws:
SQLException
- if any error occurs
-
execute
public boolean execute() throws SQLException
Executes the SQL statement in thisPreparedStatement
object, which may be any kind of SQL statement. Some prepared statements return multiple results; theexecute
method handles these complex statements as well as the simpler form of statements handled by the methodsexecuteQuery
andexecuteUpdate
.The
execute
method returns aboolean
to indicate the form of the first result. You must call either the methodgetResultSet
orgetUpdateCount
to retrieve the result; you must callgetMoreResults
to move to any subsequent result(s).- Specified by:
execute
in interfacePreparedStatement
- Specified by:
execute
in classBasePreparedStatement
- Returns:
true
if the first result is aResultSet
object;false
if the first result is an update count or there is no result- Throws:
SQLException
- if a database access error occurs; this method is called on a closedPreparedStatement
or an argument is supplied to this methodSQLTimeoutException
- when the driver has determined that the timeout value that was specified by thesetQueryTimeout
method has been exceeded and has at least attempted to cancel the currently runningStatement
- See Also:
Statement.execute(java.lang.String)
,Statement.getResultSet()
,Statement.getUpdateCount()
,Statement.getMoreResults()
-
setMaxRows
public void setMaxRows(int max) throws SQLException
Description copied from class:Statement
Sets the limit for the maximum number of rows that anyResultSet
object generated by thisStatement
object can contain to the given number. If the limit is exceeded, the excess rows are silently dropped.- Specified by:
setMaxRows
in interfaceStatement
- Overrides:
setMaxRows
in classStatement
- Parameters:
max
- the new max rows limit; zero means there is no limit- Throws:
SQLException
- if a database access error occurs, this method is called on a closedStatement
or the conditionmax >= 0
is not satisfied- See Also:
Statement.getMaxRows()
-
setLargeMaxRows
public void setLargeMaxRows(long max) throws SQLException
Description copied from class:Statement
Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number. If the limit is exceeded, the excess rows are silently dropped.- Specified by:
setLargeMaxRows
in interfaceStatement
- Overrides:
setLargeMaxRows
in classStatement
- Parameters:
max
- the new max rows limit; zero means there is no limit- Throws:
SQLException
- if the condition max >= 0 is not satisfied
-
setQueryTimeout
public void setQueryTimeout(int seconds) throws SQLException
Description copied from class:Statement
Sets the number of seconds the driver will wait for aStatement
object to execute to the given number of seconds. By default, there is no limit on the amount of time allowed for a running statement to complete. If the limit is exceeded, anSQLTimeoutException
is thrown. A JDBC driver must apply this limit to theexecute
,executeQuery
andexecuteUpdate
methods.Note: JDBC driver implementations may also apply this limit to
ResultSet
methods (consult your driver vendor documentation for details).Note: In the case of
Statement
batching, it is implementation defined whether the time-out is applied to individual SQL commands added via theaddBatch
method or to the entire batch of SQL commands invoked by theexecuteBatch
method (consult your driver vendor documentation for details).- Specified by:
setQueryTimeout
in interfaceStatement
- Overrides:
setQueryTimeout
in classStatement
- Parameters:
seconds
- the new query timeout limit in seconds; zero means there is no limit- Throws:
SQLException
- if a database access error occurs, this method is called on a closedStatement
or the conditionseconds >= 0
is not satisfied- See Also:
Statement.getQueryTimeout()
-
executeQuery
public ResultSet executeQuery() throws SQLException
Executes the SQL query in thisPreparedStatement
object and returns theResultSet
object generated by the query.- Specified by:
executeQuery
in interfacePreparedStatement
- Specified by:
executeQuery
in classBasePreparedStatement
- Returns:
- a
ResultSet
object that contains the data produced by the query; nevernull
- Throws:
SQLException
- if a database access error occurs; this method is called on a closedPreparedStatement
or the SQL statement does not return aResultSet
objectSQLTimeoutException
- when the driver has determined that the timeout value that was specified by thesetQueryTimeout
method has been exceeded and has at least attempted to cancel the currently runningStatement
-
executeUpdate
public int executeUpdate() throws SQLException
Executes the SQL statement in thisPreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
orDELETE
; or an SQL statement that returns nothing, such as a DDL statement.- Specified by:
executeUpdate
in interfacePreparedStatement
- Specified by:
executeUpdate
in classBasePreparedStatement
- Returns:
- either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
- Throws:
SQLException
- if a database access error occurs; this method is called on a closedPreparedStatement
or the SQL statement returns aResultSet
objectSQLTimeoutException
- when the driver has determined that the timeout value that was specified by thesetQueryTimeout
method has been exceeded and has at least attempted to cancel the currently runningStatement
-
executeLargeUpdate
public long executeLargeUpdate() throws SQLException
Executes the SQL statement in thisPreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
orDELETE
; or an SQL statement that returns nothing, such as a DDL statement.This method should be used when the returned row count may exceed
Integer.MAX_VALUE
.The default implementation will throw
UnsupportedOperationException
- Specified by:
executeLargeUpdate
in interfacePreparedStatement
- Specified by:
executeLargeUpdate
in classBasePreparedStatement
- Returns:
- either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
- Throws:
SQLException
- if a database access error occurs; this method is called on a closedPreparedStatement
or the SQL statement returns aResultSet
objectSQLTimeoutException
- when the driver has determined that the timeout value that was specified by thesetQueryTimeout
method has been exceeded and has at least attempted to cancel the currently runningStatement
- Since:
- 1.8
-
handleParameterOutput
protected void handleParameterOutput() throws SQLException
Handle output parameter result-set (only for CallableStatement)- Throws:
SQLException
- if any error occurs
-
addBatch
public void addBatch() throws SQLException
Adds a set of parameters to thisPreparedStatement
object's batch of commands.- Specified by:
addBatch
in interfacePreparedStatement
- Specified by:
addBatch
in classBasePreparedStatement
- Throws:
SQLException
- if a database access error occurs or this method is called on a closedPreparedStatement
- Since:
- 1.2
- See Also:
Statement.addBatch(java.lang.String)
-
validParameters
protected void validParameters() throws SQLException
Validated that all parameters have been set.- Throws:
SQLException
- if number of parameters doesn't correspond to expected number
-
getMetaData
public ResultSetMetaData getMetaData() throws SQLException
Retrieves aResultSetMetaData
object that contains information about the columns of theResultSet
object that will be returned when thisPreparedStatement
object is executed.Because a
PreparedStatement
object is precompiled, it is possible to know about theResultSet
object that it will return without having to execute it. Consequently, it is possible to invoke the methodgetMetaData
on aPreparedStatement
object rather than waiting to execute it and then invoking theResultSet.getMetaData
method on theResultSet
object that is returned.NOTE: Using this method may be expensive for some drivers due to the lack of underlying DBMS support.
- Specified by:
getMetaData
in interfacePreparedStatement
- Specified by:
getMetaData
in classBasePreparedStatement
- Returns:
- the description of a
ResultSet
object's columns ornull
if the driver cannot return aResultSetMetaData
object - Throws:
SQLException
- if a database access error occurs or this method is called on a closedPreparedStatement
SQLFeatureNotSupportedException
- if the JDBC driver does not support this method- Since:
- 1.2
-
getParameterMetaData
public ParameterMetaData getParameterMetaData() throws SQLException
Retrieves the number, types and properties of thisPreparedStatement
object's parameters.- Specified by:
getParameterMetaData
in interfacePreparedStatement
- Specified by:
getParameterMetaData
in classBasePreparedStatement
- Returns:
- a
ParameterMetaData
object that contains information about the number, types and properties for each parameter marker of thisPreparedStatement
object - Throws:
SQLException
- if a database access error occurs or this method is called on a closedPreparedStatement
- Since:
- 1.4
- See Also:
ParameterMetaData
-
executeBatch
public int[] executeBatch() throws SQLException
Description copied from class:Statement
Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. Theint
elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch. The elements in the array returned by the methodexecuteBatch
may be one of the following:- A number greater than or equal to zero -- indicates that the command was processed successfully and is an update count giving the number of rows in the database that were affected by the command's execution
- A value of
SUCCESS_NO_INFO
-- indicates that the command was processed successfully but that the number of rows affected is unknownIf one of the commands in a batch update fails to execute properly, this method throws a
BatchUpdateException
, and a JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to process commands or never continuing to process commands. If the driver continues processing after a failure, the array returned by the methodBatchUpdateException.getUpdateCounts
will contain as many elements as there are commands in the batch, and at least one of the elements will be the following: - A value of
EXECUTE_FAILED
-- indicates that the command failed to execute successfully and occurs only if a driver continues to process commands after a command fails
The possible implementations and return values have been modified in the Java 2 SDK, Standard Edition, version 1.3 to accommodate the option of continuing to process commands in a batch update after a
BatchUpdateException
object has been thrown.- Specified by:
executeBatch
in interfaceStatement
- Specified by:
executeBatch
in classBasePreparedStatement
- Returns:
- an array of update counts containing one element for each command in the batch. The elements of the array are ordered according to the order in which commands were added to the batch.
- Throws:
SQLException
- if a database access error occurs, this method is called on a closedStatement
or the driver does not support batch statements. ThrowsBatchUpdateException
(a subclass ofSQLException
) if one of the commands sent to the database fails to execute properly or attempts to return a result set.SQLTimeoutException
- when the driver has determined that the timeout value that was specified by thesetQueryTimeout
method has been exceeded and has at least attempted to cancel the currently runningStatement
- See Also:
Statement.addBatch(java.lang.String)
,DatabaseMetaData.supportsBatchUpdates()
-
executeLargeBatch
public long[] executeLargeBatch() throws SQLException
Description copied from class:Statement
Execute batch, like executeBatch(), with returning results with long[]. For when row count may exceed Integer.MAX_VALUE.- Specified by:
executeLargeBatch
in interfaceStatement
- Specified by:
executeLargeBatch
in classBasePreparedStatement
- Returns:
- an array of update counts (one element for each command in the batch)
- Throws:
SQLException
- if a database error occur.
-
close
public void close() throws SQLException
Description copied from class:Statement
Releases thisStatement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.Calling the method
close
on aStatement
object that is already closed has no effect.Note:When a
Statement
object is closed, its currentResultSet
object, if one exists, is also closed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceStatement
- Overrides:
close
in classStatement
- Throws:
SQLException
- if a database access error occurs
-
reset
public void reset()
reset prepare statement in case of a failover. (Command need then to be re-prepared on server)
-
toString
public String toString()
- Overrides:
toString
in classBasePreparedStatement
-
-