List of usage examples for java.sql ResultSetMetaData getPrecision
int getPrecision(int column) throws SQLException;
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/*ww w . j a v a 2 s.c o m*/ 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 va 2 s . 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); // get the designated column's number of decimal digits. System.out.println(rsMetaData.getPrecision(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 ww w . j av a 2 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();/*w ww . j ava 2 s . c o 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); // 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 getPrecision(ResultSetMetaData resultSetMetaData, int columnNumber) { try {/* ww w. java 2s . c o m*/ return resultSetMetaData.getPrecision(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 {/*from w ww. j a v a 2 s. c o m*/ 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 w w w .j a va 2 s . c o m*/ */ 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:de.ufinke.cubaja.sql.Sequence.java
public Sequence(SequenceConfig config) throws Exception { this.config = config.clone(); if (config.isLog()) { logger = LogFactory.getLog(Sequence.class); }//www .j a v a2 s.co m database = new Database(config.getDatabase()); Sql sqlSelect = new Sql(getClass(), "sequence_select"); sqlSelect.resolve("tableName", config.getTableName()); sqlSelect.resolve("seqName", config.getSeqName()); select = database.createQuery(sqlSelect); selectFirstValuePos = select.getColumnPosition("first_value"); selectLastValuePos = select.getColumnPosition("last_value"); Sql sqlLock = new Sql(getClass(), "sequence_lock"); sqlLock.resolve("tableName", config.getTableName()); sqlLock.resolve("seqName", config.getSeqName()); lock = database.createUpdate(sqlLock); Sql sqlDelete = new Sql(getClass(), "sequence_delete"); sqlDelete.resolve("tableName", config.getTableName()); sqlDelete.resolve("seqName", config.getSeqName()); delete = database.createUpdate(sqlDelete); deleteOldValuePos = delete.getVariablePositions("old_value")[0]; Sql sqlUpdate = new Sql(getClass(), "sequence_update"); sqlUpdate.resolve("tableName", config.getTableName()); sqlUpdate.resolve("seqName", config.getSeqName()); update = database.createUpdate(sqlUpdate); updateOldValuePos = update.getVariablePositions("old_value")[0]; updateNewValuePos = update.getVariablePositions("new_value")[0]; Sql sqlInsert = new Sql(getClass(), "sequence_insert"); sqlInsert.resolve("tableName", config.getTableName()); sqlInsert.resolve("seqName", config.getSeqName()); insert = database.createUpdate(sqlInsert); insertFirstValuePos = insert.getVariablePositions("first_value")[0]; insertLastValuePos = insert.getVariablePositions("last_value")[0]; ResultSetMetaData meta = select.getMetaData(); int precision = Math.min(18, meta.getPrecision(insertLastValuePos)); maxValue = 1; for (int i = 0; i < precision; i++) { maxValue *= 10; } maxValue--; select.closeResultSet(); }
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 ww w .ja v a 2 s . c o 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:com.flexive.core.search.genericSQL.GenericSQLForeignTableSelector.java
protected GenericSQLForeignTableSelector(String mainColumn, String tableName, String linksOn, boolean hasTranslationTable, String translatedColumn) { FxSharedUtils.checkParameterNull(tableName, "tableName"); FxSharedUtils.checkParameterNull(linksOn, "linksOn"); Connection con = null;//from w w w. j a va2 s . c o m Statement stmt = null; this.tableName = tableName; this.linksOn = linksOn; this.mainColumn = mainColumn; this.hasTranslationTable = hasTranslationTable; this.translatedColumn = translatedColumn != null ? translatedColumn.toUpperCase() : null; try { con = Database.getDbConnection(); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + StorageManager.getLimit(false, 0)); ResultSetMetaData md = rs.getMetaData(); for (int pos = 1; pos <= md.getColumnCount(); pos++) { String columnName = md.getColumnName(pos); FxDataType columnType; switch (md.getColumnType(pos)) { case java.sql.Types.CHAR: if (md.getPrecision(pos) == 1) { columnType = FxDataType.Boolean; //oracle break; } case java.sql.Types.VARCHAR: case java.sql.Types.LONGVARCHAR: case java.sql.Types.CLOB: columnType = FxDataType.String1024; break; case java.sql.Types.BOOLEAN: case java.sql.Types.BIT: columnType = FxDataType.Boolean; break; case java.sql.Types.TINYINT: case java.sql.Types.SMALLINT: case java.sql.Types.INTEGER: columnType = FxDataType.Number; break; case java.sql.Types.DECIMAL: columnType = FxDataType.Double; break; case java.sql.Types.FLOAT: columnType = FxDataType.Float; break; case java.sql.Types.NUMERIC: case java.sql.Types.BIGINT: if ("CREATED_AT".equalsIgnoreCase(columnName) || "MODIFIED_AT".equalsIgnoreCase(columnName)) columnType = FxDataType.DateTime; else if ("CAT_TYPE".equals(columnName) && "FXS_ACL".equals(tableName)) { columnType = FxDataType.Number; } else columnType = FxDataType.LargeNumber; break; case java.sql.Types.DATE: columnType = FxDataType.Date; break; case java.sql.Types.TIME: case java.sql.Types.TIMESTAMP: columnType = FxDataType.DateTime; break; default: if (LOG.isInfoEnabled()) { LOG.info("Assigning String to " + tableName + "." + columnName + " found type=" + md.getColumnType(pos)); } columnType = FxDataType.String1024; } columns.put(columnName.toUpperCase(), columnType); } } catch (Throwable t) { @SuppressWarnings({ "ThrowableInstanceNeverThrown" }) FxSqlSearchException ex = new FxSqlSearchException(LOG, "ex.sqlSearch.fieldSelector.initializeFailed", tableName, t.getMessage()); LOG.error(ex.getMessage(), ex); throw ex.asRuntimeException(); } finally { Database.closeObjects(GenericSQLForeignTableSelector.class, con, stmt); } }