Example usage for java.sql ParameterMetaData getParameterCount

List of usage examples for java.sql ParameterMetaData getParameterCount

Introduction

In this page you can find the example usage for java.sql ParameterMetaData getParameterCount.

Prototype

int getParameterCount() throws SQLException;

Source Link

Document

Retrieves the number of parameters in the PreparedStatement object for which this ParameterMetaData object contains information.

Usage

From source file:org.apache.calcite.avatica.jdbc.JdbcMeta.java

/**
 * Converts from JDBC metadata to AvaticaParameters
 *//*from www.  ja  v a 2s . c  o m*/
protected static List<AvaticaParameter> parameters(ParameterMetaData metaData) throws SQLException {
    if (metaData == null) {
        return Collections.emptyList();
    }
    final List<AvaticaParameter> params = new ArrayList<>();
    for (int i = 1; i <= metaData.getParameterCount(); i++) {
        params.add(new AvaticaParameter(metaData.isSigned(i), metaData.getPrecision(i), metaData.getScale(i),
                metaData.getParameterType(i), metaData.getParameterTypeName(i),
                metaData.getParameterClassName(i), "?" + i));
    }
    return params;
}

From source file:org.batoo.jpa.core.impl.jdbc.dbutils.AbstractQueryRunner.java

/**
 * Fill the <code>PreparedStatement</code> replacement parameters with the given objects.
 * /* ww  w. j ava2s  .c om*/
 * @param stmt
 *            PreparedStatement to fill
 * @param params
 *            Query replacement parameters; <code>null</code> is a valid value to pass in.
 * @throws SQLException
 *             if a database access error occurs
 */
public void fillStatement(PreparedStatement stmt, Object... params) throws SQLException {

    // check the parameter count, if we can
    ParameterMetaData pmd = null;
    if (!this.pmdKnownBroken) {
        pmd = stmt.getParameterMetaData();
        final int stmtCount = pmd.getParameterCount();
        final int paramsCount = params == null ? 0 : params.length;

        if (stmtCount != paramsCount) {
            throw new SQLException(
                    "Wrong number of parameters: expected " + stmtCount + ", was given " + paramsCount);
        }
    }

    // nothing to do here
    if (params == null) {
        return;
    }

    for (int i = 0; i < params.length; i++) {
        if (params[i] != null) {
            stmt.setObject(i + 1, params[i]);
        } else {
            // VARCHAR works with many drivers regardless
            // of the actual column type. Oddly, NULL and
            // OTHER don't work with Oracle's drivers.
            int sqlType = Types.VARCHAR;
            if (!this.pmdKnownBroken) {
                try {
                    sqlType = pmd.getParameterType(i + 1);
                } catch (final SQLException e) {
                    this.pmdKnownBroken = true;
                }
            }
            stmt.setNull(i + 1, sqlType);
        }
    }
}

From source file:org.pentaho.di.core.database.Database.java

public RowMetaInterface getParameterMetaData(PreparedStatement ps) {
    RowMetaInterface par = new RowMeta();
    try {/*from  www .j a  v  a  2s.  c o  m*/
        ParameterMetaData pmd = ps.getParameterMetaData();
        for (int i = 1; i <= pmd.getParameterCount(); i++) {
            String name = "par" + i;
            int sqltype = pmd.getParameterType(i);
            int length = pmd.getPrecision(i);
            int precision = pmd.getScale(i);
            ValueMeta val;

            switch (sqltype) {
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
                val = new ValueMeta(name, ValueMetaInterface.TYPE_STRING);
                break;
            case java.sql.Types.BIGINT:
            case java.sql.Types.INTEGER:
            case java.sql.Types.NUMERIC:
            case java.sql.Types.SMALLINT:
            case java.sql.Types.TINYINT:
                val = new ValueMeta(name, ValueMetaInterface.TYPE_INTEGER);
                break;
            case java.sql.Types.DECIMAL:
            case java.sql.Types.DOUBLE:
            case java.sql.Types.FLOAT:
            case java.sql.Types.REAL:
                val = new ValueMeta(name, ValueMetaInterface.TYPE_NUMBER);
                break;
            case java.sql.Types.DATE:
            case java.sql.Types.TIME:
            case java.sql.Types.TIMESTAMP:
                val = new ValueMeta(name, ValueMetaInterface.TYPE_DATE);
                break;
            case java.sql.Types.BOOLEAN:
            case java.sql.Types.BIT:
                val = new ValueMeta(name, ValueMetaInterface.TYPE_BOOLEAN);
                break;
            default:
                val = new ValueMeta(name, ValueMetaInterface.TYPE_NONE);
                break;
            }

            if (val.isNumeric() && (length > 18 || precision > 18)) {
                val = new ValueMeta(name, ValueMetaInterface.TYPE_BIGNUMBER);
            }

            par.addValueMeta(val);
        }
    } catch (AbstractMethodError e) {
        // Oops: probably the database or JDBC doesn't support it.
        return null;
    } catch (SQLException e) {
        return null;
    } catch (Exception e) {
        return null;
    }

    return par;
}