Class ServerPreparedStatement

  • All Implemented Interfaces:
    java.lang.AutoCloseable, java.sql.PreparedStatement, java.sql.Statement, java.sql.Wrapper
    Direct Known Subclasses:
    BaseCallableStatement

    public class ServerPreparedStatement
    extends BasePreparedStatement
    • Field Detail

      • PREPARABLE_STATEMENT_PATTERN

        private static final java.util.regex.Pattern PREPARABLE_STATEMENT_PATTERN
    • Constructor Detail

      • ServerPreparedStatement

        public ServerPreparedStatement​(java.lang.String sql,
                                       Connection con,
                                       java.util.concurrent.locks.ReentrantLock lock,
                                       boolean canUseServerTimeout,
                                       boolean canUseServerMaxRows,
                                       int autoGeneratedKeys,
                                       int resultSetType,
                                       int resultSetConcurrency,
                                       int defaultFetchSize)
                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
    • Method Detail

      • prepareIfNotAlready

        private void prepareIfNotAlready​(java.lang.String cmd)
                                  throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • executeInternal

        protected void executeInternal()
                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • executePipeline

        private void executePipeline​(java.lang.String cmd)
                              throws java.sql.SQLException
        Send COM_STMT_PREPARE + COM_STMT_EXECUTE, then read for the 2 answers
        Parameters:
        cmd - command
        Throws:
        java.sql.SQLException - if IOException / Command error
      • executeStandard

        private void executeStandard​(java.lang.String cmd)
                              throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • executeInternalPreparedBatch

        private java.util.List<Completion> executeInternalPreparedBatch()
                                                                 throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • executeBatchBulk

        private java.util.List<Completion> executeBatchBulk​(java.lang.String cmd)
                                                     throws java.sql.SQLException
        Send COM_STMT_PREPARE + X * COM_STMT_BULK_EXECUTE, then read for the all answers
        Parameters:
        cmd - command
        Throws:
        java.sql.SQLException - if IOException / Command error
      • executeBatchPipeline

        private java.util.List<Completion> executeBatchPipeline​(java.lang.String cmd)
                                                         throws java.sql.SQLException
        Send COM_STMT_PREPARE + X * COM_STMT_EXECUTE, then read for the all answers
        Parameters:
        cmd - command
        Throws:
        java.sql.SQLException - if Command error
      • executeBunch

        private java.util.List<Completion> executeBunch​(java.lang.String cmd,
                                                        int index,
                                                        int maxCmd)
                                                 throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • executeBunchPrepare

        private java.util.List<Completion> executeBunchPrepare​(java.lang.String cmd,
                                                               int index,
                                                               int maxCmd)
                                                        throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • executeBatchStandard

        private java.util.List<Completion> executeBatchStandard​(java.lang.String cmd)
                                                         throws java.sql.SQLException
        Send COM_STMT_PREPARE + read answer, then Send a COM_STMT_EXECUTE + read answer * n time
        Parameters:
        cmd - command
        Throws:
        java.sql.SQLException - if IOException / Command error
      • execute

        public boolean execute()
                        throws java.sql.SQLException
        Executes the SQL statement in this PreparedStatement object, which may be 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 the methods executeQuery and executeUpdate.

        The execute method returns a boolean to indicate the form of the first result. You must call either the method getResultSet or getUpdateCount to retrieve the result; you must call getMoreResults to move to any subsequent result(s).

        Specified by:
        execute in interface java.sql.PreparedStatement
        Specified by:
        execute in class BasePreparedStatement
        Returns:
        true if the first result is a ResultSet object; false if the first result is an update count or there is no result
        Throws:
        java.sql.SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or an argument is supplied to this method
        java.sql.SQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement
        See Also:
        Statement.execute(java.lang.String), Statement.getResultSet(), Statement.getUpdateCount(), Statement.getMoreResults()
      • setMaxRows

        public void setMaxRows​(int max)
                        throws java.sql.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:
        setMaxRows in interface java.sql.Statement
        Overrides:
        setMaxRows in class Statement
        Parameters:
        max - the new max rows limit; zero means there is no limit
        Throws:
        java.sql.SQLException - if a database access error occurs, this method is called on a closed Statement or the condition max >= 0 is not satisfied
        See Also:
        Statement.getMaxRows()
      • setLargeMaxRows

        public void setLargeMaxRows​(long max)
                             throws java.sql.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 interface java.sql.Statement
        Overrides:
        setLargeMaxRows in class Statement
        Parameters:
        max - the new max rows limit; zero means there is no limit
        Throws:
        java.sql.SQLException - if the condition max >= 0 is not satisfied
      • setQueryTimeout

        public void setQueryTimeout​(int seconds)
                             throws java.sql.SQLException
        Description copied from class: Statement
        Sets the number of seconds the driver will wait for a Statement 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, an SQLTimeoutException is thrown. A JDBC driver must apply this limit to the execute, executeQuery and executeUpdate 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 the addBatch method or to the entire batch of SQL commands invoked by the executeBatch method (consult your driver vendor documentation for details).

        Specified by:
        setQueryTimeout in interface java.sql.Statement
        Overrides:
        setQueryTimeout in class Statement
        Parameters:
        seconds - the new query timeout limit in seconds; zero means there is no limit
        Throws:
        java.sql.SQLException - if a database access error occurs, this method is called on a closed Statement or the condition seconds >= 0 is not satisfied
        See Also:
        Statement.getQueryTimeout()
      • executeQuery

        public java.sql.ResultSet executeQuery()
                                        throws java.sql.SQLException
        Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
        Specified by:
        executeQuery in interface java.sql.PreparedStatement
        Specified by:
        executeQuery in class BasePreparedStatement
        Returns:
        a ResultSet object that contains the data produced by the query; never null
        Throws:
        java.sql.SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object
        java.sql.SQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement
      • executeUpdate

        public int executeUpdate()
                          throws java.sql.SQLException
        Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.
        Specified by:
        executeUpdate in interface java.sql.PreparedStatement
        Specified by:
        executeUpdate in class BasePreparedStatement
        Returns:
        either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
        Throws:
        java.sql.SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet object
        java.sql.SQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement
      • executeLargeUpdate

        public long executeLargeUpdate()
                                throws java.sql.SQLException
        Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; 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 interface java.sql.PreparedStatement
        Specified by:
        executeLargeUpdate in class BasePreparedStatement
        Returns:
        either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
        Throws:
        java.sql.SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet object
        java.sql.SQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement
        Since:
        1.8
      • handleParameterOutput

        protected void handleParameterOutput()
                                      throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • addBatch

        public void addBatch()
                      throws java.sql.SQLException
        Adds a set of parameters to this PreparedStatement object's batch of commands.
        Specified by:
        addBatch in interface java.sql.PreparedStatement
        Specified by:
        addBatch in class BasePreparedStatement
        Throws:
        java.sql.SQLException - if a database access error occurs or this method is called on a closed PreparedStatement
        Since:
        1.2
        See Also:
        Statement.addBatch(java.lang.String)
      • validParameters

        protected void validParameters()
                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getMetaData

        public java.sql.ResultSetMetaData getMetaData()
                                               throws java.sql.SQLException
        Retrieves a ResultSetMetaData object that contains information about the columns of the ResultSet object that will be returned when this PreparedStatement object is executed.

        Because a PreparedStatement object is precompiled, it is possible to know about the ResultSet object that it will return without having to execute it. Consequently, it is possible to invoke the method getMetaData on a PreparedStatement object rather than waiting to execute it and then invoking the ResultSet.getMetaData method on the ResultSet 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 interface java.sql.PreparedStatement
        Specified by:
        getMetaData in class BasePreparedStatement
        Returns:
        the description of a ResultSet object's columns or null if the driver cannot return a ResultSetMetaData object
        Throws:
        java.sql.SQLException - if a database access error occurs or this method is called on a closed PreparedStatement
        java.sql.SQLFeatureNotSupportedException - if the JDBC driver does not support this method
        Since:
        1.2
      • getParameterMetaData

        public java.sql.ParameterMetaData getParameterMetaData()
                                                        throws java.sql.SQLException
        Retrieves the number, types and properties of this PreparedStatement object's parameters.
        Specified by:
        getParameterMetaData in interface java.sql.PreparedStatement
        Specified by:
        getParameterMetaData in class BasePreparedStatement
        Returns:
        a ParameterMetaData object that contains information about the number, types and properties for each parameter marker of this PreparedStatement object
        Throws:
        java.sql.SQLException - if a database access error occurs or this method is called on a closed PreparedStatement
        Since:
        1.4
        See Also:
        ParameterMetaData
      • executeBatch

        public int[] executeBatch()
                           throws java.sql.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. The int 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 method executeBatch may be one of the following:
        1. 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
        2. A value of SUCCESS_NO_INFO -- indicates that the command was processed successfully but that the number of rows affected is unknown

          If 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 method BatchUpdateException.getUpdateCounts will contain as many elements as there are commands in the batch, and at least one of the elements will be the following:

        3. 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 interface java.sql.Statement
        Specified by:
        executeBatch in class BasePreparedStatement
        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:
        java.sql.SQLException - if a database access error occurs, this method is called on a closed Statement or the driver does not support batch statements. Throws BatchUpdateException (a subclass of SQLException) if one of the commands sent to the database fails to execute properly or attempts to return a result set.
        java.sql.SQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement
        See Also:
        Statement.addBatch(java.lang.String), DatabaseMetaData.supportsBatchUpdates()
      • executeLargeBatch

        public long[] executeLargeBatch()
                                 throws java.sql.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 interface java.sql.Statement
        Specified by:
        executeLargeBatch in class BasePreparedStatement
        Returns:
        an array of update counts (one element for each command in the batch)
        Throws:
        java.sql.SQLException - if a database error occur.
      • close

        public void close()
                   throws java.sql.SQLException
        Description copied from class: Statement
        Releases this Statement 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 a Statement object that is already closed has no effect.

        Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.sql.Statement
        Overrides:
        close in class Statement
        Throws:
        java.sql.SQLException - if a database access error occurs
      • reset

        public void reset()