Example usage for java.sql ResultSetMetaData getScale

List of usage examples for java.sql ResultSetMetaData getScale

Introduction

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

Prototype

int getScale(int column) throws SQLException;

Source Link

Document

Gets the designated column's number of digits to right of the decimal point.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class.forName(DRIVER);/*w  ww .  j av  a2 s  .  c om*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT stock_id, name, price FROM stocks";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();
    int precision = metadata.getPrecision(3);
    int scale = metadata.getScale(3);

    System.out.println("Precision: " + precision);
    System.out.println("Scale    : " + scale);
}

From source file:Main.java

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

    st = conn.createStatement();//from  w w  w.  j a  v  a  2 s. com
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // gets the designated column's number of
        // digits to right of the decimal point.
        System.out.println(rsMetaData.getScale(i));
    }

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

From source file:Main.java

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

    st = conn.createStatement();/*from  www .j a v a2  s .co m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);
        System.out.println(rsMetaData.getColumnDisplaySize(i));
        System.out.println(rsMetaData.getColumnLabel(i));
        System.out.println(rsMetaData.getColumnName(i));
        System.out.println(rsMetaData.getColumnType(i));
        System.out.println(rsMetaData.getColumnTypeName(i));
        System.out.println(rsMetaData.getColumnClassName(i));
        System.out.println(rsMetaData.getTableName(i));
        System.out.println(rsMetaData.getPrecision(i));
        System.out.println(rsMetaData.getScale(i));
        System.out.println(rsMetaData.isAutoIncrement(i));
        System.out.println(rsMetaData.isCurrency(i));
        System.out.println(rsMetaData.isWritable(i));
        System.out.println(rsMetaData.isDefinitelyWritable(i));
        System.out.println(rsMetaData.isNullable(i));
        System.out.println(rsMetaData.isReadOnly(i));
        System.out.println(rsMetaData.isCaseSensitive(i));
        System.out.println(rsMetaData.isSearchable(i));
        System.out.println(rsMetaData.isSigned(i));
        System.out.println(rsMetaData.getCatalogName(i));
        System.out.println(rsMetaData.getSchemaName(i));
    }
    st.close();
    conn.close();
}

From source file:Main.java

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

    st = conn.createStatement();//from www .j  a v a  2s  . c  om
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);
        // indicates the designated column's normal maximum width in
        // characters
        System.out.println(rsMetaData.getColumnDisplaySize(i));
        // gets the designated column's suggested title
        // for use in printouts and displays.
        System.out.println(rsMetaData.getColumnLabel(i));
        // get the designated column's name.
        System.out.println(rsMetaData.getColumnName(i));

        // get the designated column's SQL type.
        System.out.println(rsMetaData.getColumnType(i));

        // get the designated column's SQL type name.
        System.out.println(rsMetaData.getColumnTypeName(i));

        // get the designated column's class name.
        System.out.println(rsMetaData.getColumnClassName(i));

        // get the designated column's table name.
        System.out.println(rsMetaData.getTableName(i));

        // get the designated column's number of decimal digits.
        System.out.println(rsMetaData.getPrecision(i));

        // gets the designated column's number of
        // digits to right of the decimal point.
        System.out.println(rsMetaData.getScale(i));

        // indicates whether the designated column is
        // automatically numbered, thus read-only.
        System.out.println(rsMetaData.isAutoIncrement(i));

        // indicates whether the designated column is a cash value.
        System.out.println(rsMetaData.isCurrency(i));

        // indicates whether a write on the designated
        // column will succeed.
        System.out.println(rsMetaData.isWritable(i));

        // indicates whether a write on the designated
        // column will definitely succeed.
        System.out.println(rsMetaData.isDefinitelyWritable(i));

        // indicates the nullability of values
        // in the designated column.
        System.out.println(rsMetaData.isNullable(i));

        // Indicates whether the designated column
        // is definitely not writable.
        System.out.println(rsMetaData.isReadOnly(i));

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isCaseSensitive(i));

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isSearchable(i));

        // indicates whether values in the designated
        // column are signed numbers.
        System.out.println(rsMetaData.isSigned(i));

        // Gets the designated column's table's catalog name.
        System.out.println(rsMetaData.getCatalogName(i));

        // Gets the designated column's table's schema name.
        System.out.println(rsMetaData.getSchemaName(i));
    }

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

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

public static int getScale(ResultSetMetaData resultSetMetaData, int columnNumber) {
    try {// w w  w.j  a  va  2s .  c o  m
        return resultSetMetaData.getScale(columnNumber);
    } catch (SQLException e) {
        throw new SqlRuntimeException(e);
    }
}

From source file:org.tinygroup.tinydb.util.TinyBeanUtil.java

public static List<Field> getFieldsWithResultSet(ResultSet rs, BeanDbNameConverter converter)
        throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    List<Field> fields = new ArrayList<Field>();
    Map<String, Integer> columnViewnum = new HashMap<String, Integer>();
    for (int index = 1; index <= columnCount; index++) {
        String columnName = JdbcUtils.lookupColumnName(rsmd, index);
        String propertyName = converter.dbFieldNameToPropertyName(columnName);
        String name = propertyName;
        if (!columnViewnum.containsKey(propertyName)) {
            columnViewnum.put(propertyName, 1);
        } else {// w  w w  . java  2 s .  c  om
            int number = columnViewnum.get(propertyName);
            name = propertyName + number;
            columnViewnum.put(propertyName, number++);
        }
        Field field = new Field();
        field.setName(name);
        field.setIndex(index);
        field.setPrecision(rsmd.getPrecision(index));
        field.setScale(rsmd.getScale(index));
        field.setType(rsmd.getColumnType(index));
        fields.add(field);
    }
    return fields;
}

From source file:org.jabsorb.ext.DataList.java

/**
 * Build an array of ColumnMetaData object from a ResultSetMetaData object.
 *
 * @param rmd ResultSetMetaData to build ColumnMetaData from.
 * @return ColumnMetaData array or null if ResultSetMetaData is null.
 *
 * @throws SQLException if there is a problem processing the 
 * ResultSetMetaData object./*from  ww  w.j a  v  a 2s .  com*/
 */
public static ColumnMetaData[] buildColumnMetaDataFromResultSetMetaData(ResultSetMetaData rmd)
        throws SQLException {
    if (rmd == null) {
        return null;
    }

    int j = rmd.getColumnCount();

    ColumnMetaData[] cmd = new ColumnMetaData[j];

    for (int i = 1; i <= j; i++) {
        ColumnMetaData c = new ColumnMetaData();

        c.setColumnName(rmd.getColumnName(i));
        c.setCatalogName(rmd.getCatalogName(i));
        c.setColumnClassName(rmd.getColumnClassName(i));
        c.setColumnDisplaySize(rmd.getColumnDisplaySize(i));
        c.setColumnLabel(rmd.getColumnLabel(i));
        c.setColumnType(rmd.getColumnType(i));
        c.setColumnTypeName(rmd.getColumnTypeName(i));
        c.setPrecision(rmd.getPrecision(i));
        c.setScale(rmd.getScale(i));
        c.setSchemaName(rmd.getSchemaName(i));
        c.setTableName(rmd.getTableName(i));
        c.setAutoIncrement(rmd.isAutoIncrement(i));
        c.setCaseSensitive(rmd.isCaseSensitive(i));
        c.setCurrency(rmd.isCurrency(i));
        c.setNullable(rmd.isNullable(i));
        c.setReadOnly(rmd.isReadOnly(i));
        c.setSearchable(rmd.isSearchable(i));
        c.setSigned(rmd.isSigned(i));
        c.setWritable(rmd.isWritable(i));
        c.setDefinitelyWritable(rmd.isDefinitelyWritable(i));

        cmd[i - 1] = c;
    }
    return cmd;
}

From source file:com.healthmarketscience.jackcess.util.ImportUtil.java

/**
 * Returns a List of Column instances converted from the given
 * ResultSetMetaData (this is the same method used by the various {@code
 * importResultSet()} methods)./*from  w w  w .jav a 2 s. co  m*/
 *
 * @return a List of Columns
 */
public static List<ColumnBuilder> toColumns(ResultSetMetaData md) throws SQLException {
    List<ColumnBuilder> columns = new LinkedList<ColumnBuilder>();
    for (int i = 1; i <= md.getColumnCount(); i++) {
        ColumnBuilder column = new ColumnBuilder(md.getColumnName(i)).escapeName();
        int lengthInUnits = md.getColumnDisplaySize(i);
        column.setSQLType(md.getColumnType(i), lengthInUnits);
        DataType type = column.getType();
        // we check for isTrueVariableLength here to avoid setting the length
        // for a NUMERIC column, which pretends to be var-len, even though it
        // isn't
        if (type.isTrueVariableLength() && !type.isLongValue()) {
            column.setLengthInUnits((short) lengthInUnits);
        }
        if (type.getHasScalePrecision()) {
            int scale = md.getScale(i);
            int precision = md.getPrecision(i);
            if (type.isValidScale(scale)) {
                column.setScale((byte) scale);
            }
            if (type.isValidPrecision(precision)) {
                column.setPrecision((byte) precision);
            }
        }
        columns.add(column);
    }
    return columns;
}

From source file:oscar.form.FrmRecordHelp.java

private Properties getResultsAsProperties(ResultSet rs) throws SQLException {
    Properties p = new Properties();
    ResultSetMetaData md = rs.getMetaData();
    for (int i = 1; i <= md.getColumnCount(); i++) {
        String name = md.getColumnName(i);
        String value;/*from w  w  w  .j av a  2 s .  co  m*/

        if (md.getColumnTypeName(i).startsWith("TINY") && md.getScale(i) == 1) {
            if (rs.getInt(i) == 1)
                value = "on";
            else
                value = "off";
        } else if (md.getColumnTypeName(i).equalsIgnoreCase("date"))
            value = UtilDateUtilities.DateToString(rs.getDate(i), _dateFormat);
        else
            value = oscar.Misc.getString(rs, i);

        if (value != null)
            p.setProperty(name, value);
    }

    return (p);
}

From source file:org.exoprax.assay.input.BasicDataSourceInput.java

public void runInner() throws SQLException {
    if (this.sql == null) {
        if (this.rowLimit > 0) {
            this.sql = "SELECT * FROM " + this.tableName + " WHERE ROWNUM <= " + this.rowLimit;
        } else {/*from  w  ww.  ja  v a  2s . c o  m*/
            this.sql = "SELECT * FROM " + this.tableName;
        }
    }

    final Connection connection = this.dataSource.getConnection();

    try {
        final Statement statement = connection.createStatement();

        try {
            final ResultSet results = statement.executeQuery(this.sql);

            try {
                final ResultSetMetaData metadata = results.getMetaData();

                final int columnCount = metadata.getColumnCount();

                for (int i = 1; i <= columnCount; i++) {
                    final String columnName = metadata.getColumnName(i);
                    final String columnType = metadata.getColumnTypeName(i);
                    final int columnPrecision = metadata.getPrecision(i);
                    final int columnScale = metadata.getScale(i);

                    final String columnTypeString = formatTypeString(columnType, columnPrecision, columnScale);

                    this.assay.addAttribute(columnName, columnTypeString);
                }

                long rows = 0;
                boolean hitLimit = false;

                while (results.next()) {
                    if ((this.rowLimit > 0) && (rows >= this.rowLimit)) {
                        hitLimit = true;
                        break;
                    }

                    for (int i = 1; i <= columnCount; i++) {
                        final String columnName = metadata.getColumnName(i);

                        final String value = results.getString(i);

                        this.assay.addAttributeValue(columnName, value);
                    }

                    rows++;
                }

                if (hitLimit) {
                    this.assay.setTitle("Table: " + this.tableName + " (subset of " + rows + " rows)");
                } else {
                    this.assay.setTitle("Table: " + this.tableName + " (all " + rows + " rows)");
                }
            } finally {
                try {
                    results.close();
                } catch (final Exception e) {
                    // no-op. Closing is best-effort only.
                }
            }
        } finally {
            try {
                statement.close();
            } catch (final Exception e) {
                // no-op. Closing is best-effort only.
            }
        }
    } finally {
        try {
            connection.close();
        } catch (final Exception e) {
            // no-op. Closing is best-effort only.
        }
    }
}