欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何獲取PreparedStatement參數(shù)示例詳解

 更新時(shí)間:2023年09月06日 10:30:41   作者:codecraft  
這篇文章主要為大家介紹了詳解如何獲取PreparedStatement參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下如何獲取PreparedStatement的參數(shù)

PreparedStatement

java/sql/PreparedStatement.java

public interface PreparedStatement extends Statement {
     void setNull(int parameterIndex, int sqlType) throws SQLException;
     void setBoolean(int parameterIndex, boolean x) throws SQLException;
     void setInt(int parameterIndex, int x) throws SQLException;
     void setLong(int parameterIndex, long x) throws SQLException;
     //......
    default void setObject(int parameterIndex, Object x, SQLType targetSqlType,
             int scaleOrLength) throws SQLException {
        throw new SQLFeatureNotSupportedException("setObject not implemented");
    }
    default void setObject(int parameterIndex, Object x, SQLType targetSqlType)
      throws SQLException {
        throw new SQLFeatureNotSupportedException("setObject not implemented");
    }
    /**
     * Retrieves the number, types and properties of this
     * <code>PreparedStatement</code> object's parameters.
     *
     * @return a <code>ParameterMetaData</code> object that contains information
     *         about the number, types and properties for each
     *  parameter marker of this <code>PreparedStatement</code> object
     * @exception SQLException if a database access error occurs or
     * this method is called on a closed <code>PreparedStatement</code>
     * @see ParameterMetaData
     * @since 1.4
     */
    ParameterMetaData getParameterMetaData() throws SQLException;
}
PreparedStatement繼承了Statement接口,它主要是多定義了一系列的set方法,但是沒有定義get方法,只是定義了getParameterMetaData方法返回ParameterMetaData

ParameterMetaData

java/sql/ParameterMetaData.java

public interface ParameterMetaData extends Wrapper {
    /**
     * Retrieves the number of parameters in the <code>PreparedStatement</code>
     * object for which this <code>ParameterMetaData</code> object contains
     * information.
     *
     * @return the number of parameters
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    int getParameterCount() throws SQLException;
    /**
     * Retrieves whether null values are allowed in the designated parameter.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return the nullability status of the given parameter; one of
     *        <code>ParameterMetaData.parameterNoNulls</code>,
     *        <code>ParameterMetaData.parameterNullable</code>, or
     *        <code>ParameterMetaData.parameterNullableUnknown</code>
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    int isNullable(int param) throws SQLException;
    /**
     * The constant indicating that a
     * parameter will not allow <code>NULL</code> values.
     */
    int parameterNoNulls = 0;
    /**
     * The constant indicating that a
     * parameter will allow <code>NULL</code> values.
     */
    int parameterNullable = 1;
    /**
     * The constant indicating that the
     * nullability of a parameter is unknown.
     */
    int parameterNullableUnknown = 2;
    /**
     * Retrieves whether values for the designated parameter can be signed numbers.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return <code>true</code> if so; <code>false</code> otherwise
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    boolean isSigned(int param) throws SQLException;
    /**
     * Retrieves the designated parameter's specified column size.
     *
     * <P>The returned value represents the maximum column size for the given parameter.
     * For numeric data, this is the maximum precision.  For character data, this is the length in characters.
     * For datetime datatypes, this is the length in characters of the String representation (assuming the
     * maximum allowed precision of the fractional seconds component). For binary data, this is the length in bytes.  For the ROWID datatype,
     * this is the length in bytes. 0 is returned for data types where the
     * column size is not applicable.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return precision
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    int getPrecision(int param) throws SQLException;
    /**
     * Retrieves the designated parameter's number of digits to right of the decimal point.
     * 0 is returned for data types where the scale is not applicable.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return scale
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    int getScale(int param) throws SQLException;
    /**
     * Retrieves the designated parameter's SQL type.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return SQL type from <code>java.sql.Types</code>
     * @exception SQLException if a database access error occurs
     * @since 1.4
     * @see Types
     */
    int getParameterType(int param) throws SQLException;
    /**
     * Retrieves the designated parameter's database-specific type name.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return type the name used by the database. If the parameter type is
     * a user-defined type, then a fully-qualified type name is returned.
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    String getParameterTypeName(int param) throws SQLException;
    /**
     * Retrieves the fully-qualified name of the Java class whose instances
     * should be passed to the method <code>PreparedStatement.setObject</code>.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return the fully-qualified name of the class in the Java programming
     *         language that would be used by the method
     *         <code>PreparedStatement.setObject</code> to set the value
     *         in the specified parameter. This is the class name used
     *         for custom mapping.
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    String getParameterClassName(int param) throws SQLException;
    /**
     * The constant indicating that the mode of the parameter is unknown.
     */
    int parameterModeUnknown = 0;
    /**
     * The constant indicating that the parameter's mode is IN.
     */
    int parameterModeIn = 1;
    /**
     * The constant indicating that the parameter's mode is INOUT.
     */
    int parameterModeInOut = 2;
    /**
     * The constant indicating that the parameter's mode is  OUT.
     */
    int parameterModeOut = 4;
    /**
     * Retrieves the designated parameter's mode.
     *
     * @param param the first parameter is 1, the second is 2, ...
     * @return mode of the parameter; one of
     *        <code>ParameterMetaData.parameterModeIn</code>,
     *        <code>ParameterMetaData.parameterModeOut</code>, or
     *        <code>ParameterMetaData.parameterModeInOut</code>
     *        <code>ParameterMetaData.parameterModeUnknown</code>.
     * @exception SQLException if a database access error occurs
     * @since 1.4
     */
    int getParameterMode(int param) throws SQLException;
}
ParameterMetaDatat提供了getParameterCount、getParameterType、getParameterTypeName、getParameterClassName、getParameterMode

com.mysql.jdbc.PreparedStatement

com/mysql/jdbc/PreparedStatement.java

public class PreparedStatement extends com.mysql.jdbc.StatementImpl implements
        java.sql.PreparedStatement {
    //......
    protected int parameterCount;
    protected MysqlParameterMetadata parameterMetaData;
    private InputStream[] parameterStreams = null;
    private byte[][] parameterValues = null;
    /**
     * Only used by statement interceptors at the moment to
     * provide introspection of bound values
     */
    protected int[] parameterTypes = null;
    public ParameterBindings getParameterBindings() throws SQLException {
        synchronized (checkClosed()) {
            return new EmulatedPreparedStatementBindings();
        }
    }
    //......
}
mysql的PreparedStatement實(shí)現(xiàn)定義了parameterCount、parameterMetaData、parameterStreams、parameterValues、parameterTypes屬性,提供了getParameterBindings方法,返回的是EmulatedPreparedStatementBindings

ParameterBindings

com/mysql/jdbc/ParameterBindings.java

public interface ParameterBindings {
    public abstract Array getArray(int parameterIndex) throws SQLException;
    public abstract InputStream getAsciiStream(int parameterIndex) throws SQLException;
    public abstract BigDecimal getBigDecimal(int parameterIndex) throws SQLException;
    public abstract InputStream getBinaryStream(int parameterIndex) throws SQLException;
    public abstract java.sql.Blob getBlob(int parameterIndex) throws SQLException;
    public abstract boolean getBoolean(int parameterIndex) throws SQLException;
    public abstract byte getByte(int parameterIndex) throws SQLException;
    public abstract byte[] getBytes(int parameterIndex) throws SQLException;
    public abstract Reader getCharacterStream(int parameterIndex) throws SQLException;
    public abstract Clob getClob(int parameterIndex) throws SQLException;
    public abstract Date getDate(int parameterIndex) throws SQLException;
    public abstract double getDouble(int parameterIndex) throws SQLException;
    public abstract float getFloat(int parameterIndex) throws SQLException;
    public abstract int getInt(int parameterIndex) throws SQLException;
    public abstract long getLong(int parameterIndex) throws SQLException;
    public abstract Reader getNCharacterStream(int parameterIndex) throws SQLException;
    public abstract Reader getNClob(int parameterIndex) throws SQLException;
    public abstract Object getObject(int parameterIndex) throws SQLException;
    public abstract Ref getRef(int parameterIndex) throws SQLException;
    public abstract short getShort(int parameterIndex) throws SQLException;
    public abstract String getString(int parameterIndex) throws SQLException;
    public abstract Time getTime(int parameterIndex) throws SQLException;
    public abstract Timestamp getTimestamp(int parameterIndex) throws SQLException;
    public abstract URL getURL(int parameterIndex) throws SQLException;
    public abstract boolean isNull(int parameterIndex) throws SQLException;
}
ParameterBindings定義了一系列的get方法

EmulatedPreparedStatementBindings

class EmulatedPreparedStatementBindings implements ParameterBindings {
        private ResultSetImpl bindingsAsRs;
        private boolean[] parameterIsNull;
        EmulatedPreparedStatementBindings() throws SQLException {
            List<ResultSetRow> rows = new ArrayList<ResultSetRow>();
            parameterIsNull = new boolean[parameterCount];
            System
                    .arraycopy(isNull, 0, this.parameterIsNull, 0,
                            parameterCount);
            byte[][] rowData = new byte[parameterCount][];
            Field[] typeMetadata = new Field[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                if (batchCommandIndex == -1)
                    rowData[i] = getBytesRepresentation(i);
                else
                    rowData[i] = getBytesRepresentationForBatch(i, batchCommandIndex);
                int charsetIndex = 0;
                if (parameterTypes[i] == Types.BINARY
                        || parameterTypes[i] == Types.BLOB) {
                    charsetIndex = 63;
                } else {
                    try {
                        String mysqlEncodingName = CharsetMapping
                                .getMysqlEncodingForJavaEncoding(connection
                                        .getEncoding(), connection);
                        charsetIndex = CharsetMapping
                                .getCharsetIndexForMysqlEncodingName(mysqlEncodingName);
                    } catch (SQLException ex) {
                        throw ex;
                    } catch (RuntimeException ex) {
                        SQLException sqlEx = SQLError.createSQLException(ex.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, null);
                        sqlEx.initCause(ex);
                        throw sqlEx;
                    }
                }
                Field parameterMetadata = new Field(null, "parameter_"
                        + (i + 1), charsetIndex, parameterTypes[i],
                        rowData[i].length);
                parameterMetadata.setConnection(connection);
                typeMetadata[i] = parameterMetadata;
            }
            rows.add(new ByteArrayRow(rowData, getExceptionInterceptor()));
            this.bindingsAsRs = new ResultSetImpl(connection.getCatalog(),
                    typeMetadata, new RowDataStatic(rows), connection, null);
            this.bindingsAsRs.next();
        }
        //......
    }
EmulatedPreparedStatementBindings實(shí)現(xiàn)了ParameterBindings接口,它主要是把參數(shù)組裝到rowData,然后創(chuàng)建了RowDataStatic,構(gòu)造ResultSetImpl這個(gè)對(duì)象來實(shí)現(xiàn)

小結(jié)

jdbc的PreparedStatement并未提供相應(yīng)的get參數(shù)的方法,只能從driver的實(shí)現(xiàn)類去找,比如mysql的PreparedStatement實(shí)現(xiàn)提供了getParameterBindings方法,返回的是EmulatedPreparedStatementBindings,可以獲取參數(shù)

以上就是詳解如何獲取PreparedStatement參數(shù)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于PreparedStatement參數(shù)獲取的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Redis之SpringDataRedis用法詳解

    Redis之SpringDataRedis用法詳解

    這篇文章主要介紹了Redis之SpringDataRedis的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java語言實(shí)現(xiàn)對(duì)MySql數(shù)據(jù)庫中數(shù)據(jù)的增刪改查操作的代碼

    Java語言實(shí)現(xiàn)對(duì)MySql數(shù)據(jù)庫中數(shù)據(jù)的增刪改查操作的代碼

    這篇文章主要介紹了Java語言實(shí)現(xiàn)對(duì)MySql數(shù)據(jù)庫中數(shù)據(jù)的增刪改查操作的代碼,實(shí)現(xiàn)了連接數(shù)據(jù)庫,和數(shù)據(jù)庫的增刪改查操作,有興趣的可以了解一下。
    2016-12-12
  • Servlet方法生命周期及執(zhí)行原理詳解

    Servlet方法生命周期及執(zhí)行原理詳解

    運(yùn)行在服務(wù)器端的小程序,Servlet就是一個(gè)接口,定義了Java類被瀏覽器訪問到(tomcat識(shí)別)的規(guī)則,將來我們自定義一個(gè)類,實(shí)現(xiàn)Servlet接口,復(fù)寫方法
    2021-09-09
  • Java運(yùn)行時(shí)數(shù)據(jù)區(qū)劃分原理解析

    Java運(yùn)行時(shí)數(shù)據(jù)區(qū)劃分原理解析

    這篇文章主要介紹了Java運(yùn)行時(shí)數(shù)據(jù)區(qū)劃分原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 詳解SpringCloudGateway內(nèi)存泄漏問題

    詳解SpringCloudGateway內(nèi)存泄漏問題

    這篇文章主要介紹了詳解SpringCloudGateway內(nèi)存泄漏問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • IDEA2020.3創(chuàng)建web工程的完整步驟

    IDEA2020.3創(chuàng)建web工程的完整步驟

    這篇文章主要給大家介紹了關(guān)于IDEA2020.3創(chuàng)建web工程的完整步驟,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Mybatis-plus操作json字段實(shí)戰(zhàn)教程

    Mybatis-plus操作json字段實(shí)戰(zhàn)教程

    這篇文章主要介紹了Mybatis-plus操作json字段實(shí)戰(zhàn)教程,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • Java實(shí)現(xiàn)冒泡排序簡(jiǎn)單示例

    Java實(shí)現(xiàn)冒泡排序簡(jiǎn)單示例

    冒泡排序(Bubble Sort)是一種簡(jiǎn)單的排序算法,它重復(fù)地走訪過要排序的數(shù)列,一次比較兩個(gè)元素,如果他們的順序錯(cuò)誤就把他們交換過來,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)冒泡排序的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Idea配置超詳細(xì)圖文教程(2020.2版本)

    Idea配置超詳細(xì)圖文教程(2020.2版本)

    這篇文章主要介紹了Idea配置超詳細(xì)圖文教程(2020.2版本),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • java回調(diào)機(jī)制實(shí)例詳解

    java回調(diào)機(jī)制實(shí)例詳解

    這篇文章主要介紹了java回調(diào)機(jī)制實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評(píng)論