Example usage for java.sql ParameterMetaData getParameterType

List of usage examples for java.sql ParameterMetaData getParameterType

Introduction

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

Prototype

int getParameterType(int param) throws SQLException;

Source Link

Document

Retrieves the designated parameter's SQL type.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    String simpleProc = "{ call simpleproc(?) }";
    CallableStatement cs = conn.prepareCall(simpleProc);
    cs.registerOutParameter(1, java.sql.Types.INTEGER);
    cs.execute();/*  www  .  j  ava2s . com*/
    int param1 = cs.getInt(1);
    System.out.println("param1=" + param1);
    ParameterMetaData pmeta = cs.getParameterMetaData();
    if (pmeta == null) {
        System.out.println("Vendor does not support ParameterMetaData");
    } else {
        System.out.println(pmeta.getParameterType(1));
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    // Step-2: identify the stored procedure
    String simpleProc = "{ call simpleproc(?) }";
    // Step-3: prepare the callable statement
    CallableStatement cs = conn.prepareCall(simpleProc);
    // Step-4: register output parameters ...
    cs.registerOutParameter(1, java.sql.Types.INTEGER);
    // Step-5: execute the stored procedures: proc3
    cs.execute();//ww w  . j a v  a  2  s.c o  m
    // Step-6: extract the output parameters
    int param1 = cs.getInt(1);
    System.out.println("param1=" + param1);
    // Step-7: get ParameterMetaData
    ParameterMetaData pmeta = cs.getParameterMetaData();
    if (pmeta == null) {
        System.out.println("Vendor does not support ParameterMetaData");
    } else {
        System.out.println(pmeta.getParameterType(1));
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,name varchar(30));");

    String sql = "INSERT INTO survey (id) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    ParameterMetaData pmd = pstmt.getParameterMetaData();

    int totalDigits = pmd.getPrecision(1);
    int digitsAfterDecimal = pmd.getScale(1);
    boolean b = pmd.isSigned(1);
    System.out.println("The first parameter ");
    System.out.println("    has precision " + totalDigits);
    System.out.println("    has scale " + digitsAfterDecimal);
    System.out.println("    may be a signed number " + b);

    int count = pmd.getParameterCount();
    System.out.println("count is " + count);

    for (int i = 1; i <= count; i++) {
        int type = pmd.getParameterType(i);
        String typeName = pmd.getParameterTypeName(i);
        System.out.println("Parameter " + i + ":");
        System.out.println("    type is " + type);
        System.out.println("    type name is " + typeName);
    }/*from w  w w.j  a va2 s. c  om*/

    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("create view surveyView as (select * from survey);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    PreparedStatement pstmt = null;
    ParameterMetaData paramMetaData = null;
    String query = "select * from survey where id > ? and name = ?";
    pstmt = conn.prepareStatement(query);
    paramMetaData = pstmt.getParameterMetaData();
    if (paramMetaData == null) {
        System.out.println("db vendor does NOT support ParameterMetaData");
    } else {//  w w  w. ja  va  2s  .com
        System.out.println("db vendor supports ParameterMetaData");
        // find out the number of dynamic parameters
        int paramCount = paramMetaData.getParameterCount();
        System.out.println("paramCount=" + paramCount);
        System.out.println("-------------------");
        for (int param = 1; param <= paramCount; param++) {
            System.out.println("param number=" + param);
            int sqlTypeCode = paramMetaData.getParameterType(param);
            System.out.println("param SQL type code=" + sqlTypeCode);

        }
    }

    pstmt.close();
    conn.close();

}

From source file:GetParamMetaData.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";

    Connection con;//from w  w w .ja v  a  2  s . c  o m
    PreparedStatement pstmt;
    ParameterMetaData pmd;

    String sql = "UPDATE COFFEES SET SALES = ? " + "WHERE COF_NAME = ?";

    try {

        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {

        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        pstmt = con.prepareStatement(sql);

        pmd = pstmt.getParameterMetaData();

        int totalDigits = pmd.getPrecision(1);
        int digitsAfterDecimal = pmd.getScale(1);
        boolean b = pmd.isSigned(1);
        System.out.println("The first parameter ");
        System.out.println("    has precision " + totalDigits);
        System.out.println("    has scale " + digitsAfterDecimal);
        System.out.println("    may be a signed number " + b);

        int count = pmd.getParameterCount();
        System.out.println("count is " + count);

        for (int i = 1; i <= count; i++) {
            int type = pmd.getParameterType(i);
            String typeName = pmd.getParameterTypeName(i);
            System.out.println("Parameter " + i + ":");
            System.out.println("    type is " + type);
            System.out.println("    type name is " + typeName);
        }

        pstmt.close();
        con.close();

    } catch (Exception e) {
        e.printStackTrace();

    }
}

From source file:com.example.querybuilder.server.Jdbc.java

public static int getParameterType(ParameterMetaData parameterMetaData, int parameterNumber) {
    try {//from w ww .  j a  va 2  s  . com
        return parameterMetaData.getParameterType(parameterNumber);
    } catch (SQLException e) {
        throw new SqlRuntimeException(e);
    }
}

From source file:com.tesora.dve.db.mysql.MysqlNative.java

@Override
public ColumnMetadata getParameterColumnInfo(ParameterMetaData pmd, int colIdx) throws SQLException {
    ColumnMetadata out = new ColumnMetadata();
    out.setDataType(pmd.getParameterType(colIdx));
    out.setPrecision(pmd.getPrecision(colIdx));
    out.setScale(pmd.getScale(colIdx));//from  www  .j a va2  s . c o  m
    if (ParameterMetaData.parameterNullable == pmd.isNullable(colIdx))
        out.setNullable(Boolean.TRUE);
    out.setTypeName(NativeType.fixName(pmd.getParameterTypeName(colIdx)));
    return out;
}

From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java

private void checkParameter(PreparedStatement prep, int index, String className, int type, String typeName,
        int precision, int scale) throws SQLException {
    ParameterMetaData meta = prep.getParameterMetaData();
    assertEquals(className, meta.getParameterClassName(index));
    assertEquals(type, meta.getParameterType(index));
    assertEquals(typeName, meta.getParameterTypeName(index));
    assertEquals(precision, meta.getPrecision(index));
    assertEquals(scale, meta.getScale(index));
}

From source file:org.tradex.jdbc.JDBCHelper.java

/**
 * Generates a SqlParameterSource for the passed SQL text and supplied binds
 * @param sql The SQL to bind to//from  ww  w  . j  av  a2s  . c  o m
 * @param binds The supplied variables to bind
 * @return a SqlParameterSource
 */
public SqlParameterSource getBinds(String sql, final Object... binds) {
    final MapSqlParameterSource sqlParamSource = new MapSqlParameterSource();
    int[] parameterTypes = TYPE_CACHE.get(sql);
    if (parameterTypes == null) {
        synchronized (TYPE_CACHE) {
            parameterTypes = TYPE_CACHE.get(sql);
            if (parameterTypes == null) {
                Connection conn = null;
                PreparedStatement ps = null;
                try {
                    conn = ds.getConnection();
                    ps = conn.prepareStatement(NamedParameterUtils.parseSqlStatementIntoString(sql).toString());
                    ParameterMetaData pmd = ps.getParameterMetaData();
                    int paramCount = pmd.getParameterCount();
                    if (paramCount > 0 && (binds == null || binds.length != paramCount)) {
                        throw new RuntimeException("Bind Count [" + (binds == null ? 0 : binds.length)
                                + "] was not equal to parameter count [" + paramCount + "]");
                    }
                    parameterTypes = new int[paramCount];
                    for (int i = 0; i < paramCount; i++) {
                        parameterTypes[i] = pmd.getParameterType(i + 1);
                    }
                } catch (RuntimeException re) {
                    throw re;
                } catch (Exception e) {
                    throw new RuntimeException("Failed to get binds for [" + sql + "]", e);
                } finally {
                    try {
                        ps.close();
                    } catch (Exception e) {
                    }
                    try {
                        conn.close();
                    } catch (Exception e) {
                    }
                }

            }
            TYPE_CACHE.put(sql, parameterTypes);
        }
    }
    for (int i = 0; i < parameterTypes.length; i++) {
        sqlParamSource.addValue("" + i, binds[i], parameterTypes[i]);
    }
    return sqlParamSource;
}

From source file:edu.utah.further.core.data.logging.LoggingPreparedStatement.java

/**
 * Log prepared statement. Report parameters separately from the query.
 *
 * @throws SQLException//ww w  .  j  a v a  2 s  .c  o m
 */
@SuppressWarnings("unused")
private void logPreparedStatement() throws SQLException {
    if (log.isInfoEnabled()) {
        log.info("Executing prepared statement: " + substituteParameters(parameterValues));
        log.info("Executing prepared statement: " + query);
        final ParameterMetaData parameterMetaData = statement.getParameterMetaData();
        for (int i = 1; i <= parameterMetaData.getParameterCount(); i++) {
            log.info("Parameter " + i + ": type " + parameterMetaData.getParameterType(i) + " value "
                    + parameterValues.get(i));
        }
    }
}