Example usage for java.sql PreparedStatement getParameterMetaData

List of usage examples for java.sql PreparedStatement getParameterMetaData

Introduction

In this page you can find the example usage for java.sql PreparedStatement getParameterMetaData.

Prototype

ParameterMetaData getParameterMetaData() throws SQLException;

Source Link

Document

Retrieves the number, types and properties of this PreparedStatement object's parameters.

Usage

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

public RowMetaInterface getParameterMetaData(PreparedStatement ps) {
    RowMetaInterface par = new RowMeta();
    try {//  w w  w  . j  a va  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;
}