Example usage for java.sql ParameterMetaData getScale

List of usage examples for java.sql ParameterMetaData getScale

Introduction

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

Prototype

int getScale(int param) throws SQLException;

Source Link

Document

Retrieves the designated parameter's number of digits to right of the decimal point.

Usage

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  ww w.j  a v  a 2s.  co m*/

    st.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.  j  a  v a  2 s.  c om*/
    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.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));
    if (ParameterMetaData.parameterNullable == pmd.isNullable(colIdx))
        out.setNullable(Boolean.TRUE);
    out.setTypeName(NativeType.fixName(pmd.getParameterTypeName(colIdx)));
    return out;//from   w  w  w  . ja  va  2 s  . com
}

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:com.alibaba.wasp.jdbc.TestPreparedStatement.java

public void testParameterMetaData() throws SQLException {
    PreparedStatement prep = conn.prepareStatement("SELECT ?, ?, ? FROM DUAL");
    ParameterMetaData pm = prep.getParameterMetaData();
    assertEquals("java.lang.String", pm.getParameterClassName(1));
    assertEquals("VARCHAR", pm.getParameterTypeName(1));
    assertEquals(3, pm.getParameterCount());
    assertEquals(ParameterMetaData.parameterModeIn, pm.getParameterMode(1));
    assertEquals(Types.VARCHAR, pm.getParameterType(1));
    assertEquals(0, pm.getPrecision(1));
    assertEquals(0, pm.getScale(1));
    assertEquals(ResultSetMetaData.columnNullableUnknown, pm.isNullable(1));
    assertEquals(pm.isSigned(1), true);/*from ww  w  . ja  va 2s.c o m*/
    assertThrows(SQLErrorCode.INVALID_VALUE_2, pm).getPrecision(0);
    assertThrows(SQLErrorCode.INVALID_VALUE_2, pm).getPrecision(4);
    prep.close();
    assertThrows(SQLErrorCode.OBJECT_CLOSED, pm).getPrecision(1);

    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST3(ID INT, NAME VARCHAR(255), DATA DECIMAL(10,2))");
    PreparedStatement prep1 = conn.prepareStatement("UPDATE TEST3 SET ID=?, NAME=?, DATA=?");
    PreparedStatement prep2 = conn.prepareStatement("INSERT INTO TEST3 VALUES(?, ?, ?)");
    checkParameter(prep1, 1, "java.lang.Integer", 4, "INTEGER", 10, 0);
    checkParameter(prep1, 2, "java.lang.String", 12, "VARCHAR", 255, 0);
    checkParameter(prep1, 3, "java.math.BigDecimal", 3, "DECIMAL", 10, 2);
    checkParameter(prep2, 1, "java.lang.Integer", 4, "INTEGER", 10, 0);
    checkParameter(prep2, 2, "java.lang.String", 12, "VARCHAR", 255, 0);
    checkParameter(prep2, 3, "java.math.BigDecimal", 3, "DECIMAL", 10, 2);
    PreparedStatement prep3 = conn
            .prepareStatement("SELECT * FROM TEST3 WHERE ID=? AND NAME LIKE ? AND ?>DATA");
    checkParameter(prep3, 1, "java.lang.Integer", 4, "INTEGER", 10, 0);
    checkParameter(prep3, 2, "java.lang.String", 12, "VARCHAR", 0, 0);
    checkParameter(prep3, 3, "java.math.BigDecimal", 3, "DECIMAL", 10, 2);
    stat.execute("DROP TABLE TEST3");
}

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

/**
 * Converts from JDBC metadata to AvaticaParameters
 *//*from w  w w . j a va  2 s .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.pentaho.di.core.database.Database.java

public RowMetaInterface getParameterMetaData(PreparedStatement ps) {
    RowMetaInterface par = new RowMeta();
    try {/*from w  ww  .java  2 s .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;
}