List of usage examples for java.sql Types DECIMAL
int DECIMAL
To view the source code for java.sql Types DECIMAL.
Click Source Link
The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type DECIMAL
.
From source file:com.tesora.dve.common.TestDataGenerator.java
protected Object getColumnValue(ColumnMetadata cm) { Object cv = null;//w w w. j av a 2 s. c o m Calendar cal = Calendar.getInstance(); switch (cm.getDataType()) { case Types.BIT: case Types.BOOLEAN: cv = Boolean.TRUE; break; case Types.BIGINT: cv = Long.MAX_VALUE; break; case Types.CHAR: case Types.VARCHAR: cv = StringUtils.left(baseString, cm.getSize()); break; case Types.SMALLINT: cv = Short.MAX_VALUE; break; case Types.TINYINT: cv = Byte.MAX_VALUE; break; case Types.INTEGER: cv = Integer.MAX_VALUE; break; case Types.DOUBLE: cv = new Double(1234.5678); // TODO need to handle s,p break; case Types.FLOAT: cv = new Float(123.56); // TODO need to handle s,p break; case Types.DECIMAL: cv = new BigDecimal("12345.6789"); // TODO need to handle s,p break; case Types.DATE: cal.setTimeInMillis(123456789); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cv = cal.getTime(); break; case Types.TIMESTAMP: cal.setTimeInMillis(123456789); cv = cal.getTime(); break; case Types.TIME: cv = new Time(123456789); break; default: break; } return cv; }
From source file:com.sangupta.fileanalysis.db.DBResultViewer.java
/** * View resutls of a {@link ResultSet}.//from www . ja v a 2 s .c o m * * @param resultSet * @throws SQLException */ public void viewResult(ResultSet resultSet) throws SQLException { if (resultSet == null) { // nothing to do return; } // collect the meta ResultSetMetaData meta = resultSet.getMetaData(); final int numColumns = meta.getColumnCount(); final int[] displaySizes = new int[numColumns + 1]; final int[] colType = new int[numColumns + 1]; for (int index = 1; index <= numColumns; index++) { colType[index] = meta.getColumnType(index); displaySizes[index] = getColumnSize(meta.getTableName(index), meta.getColumnName(index), colType[index]); } // display the header row for (int index = 1; index <= numColumns; index++) { center(meta.getColumnLabel(index), displaySizes[index]); } System.out.println("|"); for (int index = 1; index <= numColumns; index++) { System.out.print("+" + StringUtils.repeat('-', displaySizes[index] + 2)); } System.out.println("+"); // start iterating over the result set int rowsDisplayed = 0; int numRecords = 0; while (resultSet.next()) { // read and display the value rowsDisplayed++; numRecords++; for (int index = 1; index <= numColumns; index++) { switch (colType[index]) { case Types.DECIMAL: case Types.DOUBLE: case Types.REAL: format(resultSet.getDouble(index), displaySizes[index]); continue; case Types.INTEGER: case Types.SMALLINT: format(resultSet.getInt(index), displaySizes[index]); continue; case Types.VARCHAR: format(resultSet.getString(index), displaySizes[index], false); continue; case Types.TIMESTAMP: format(resultSet.getTimestamp(index), displaySizes[index]); continue; case Types.BIGINT: format(resultSet.getBigDecimal(index), displaySizes[index]); continue; } } // terminator for row and new line System.out.println("|"); // check for rows displayed if (rowsDisplayed == 20) { // ask the user if more data needs to be displayed String cont = ConsoleUtils.readLine("Type \"it\" for more: ", true); if (!"it".equalsIgnoreCase(cont)) { break; } // continue; rowsDisplayed = 0; continue; } } System.out.println("\nTotal number of records found: " + numRecords); }
From source file:au.com.ish.derbydump.derbydump.metadata.Column.java
/** * Get a string value for the value in this column in the datarow * /*from ww w. j a v a2 s. c o m*/ * @param dataRow The row which we are exporting * @return an SQL statement compliant string version of the value */ public String toString(ResultSet dataRow) throws SQLException { switch (getColumnDataType()) { case Types.BINARY: case Types.VARBINARY: case Types.BLOB: { Blob obj = dataRow.getBlob(columnName); return (obj == null) ? "NULL" : processBinaryData(obj); } case Types.CLOB: { Clob obj = dataRow.getClob(columnName); return (obj == null) ? "NULL" : processClobData(obj); } case Types.CHAR: case Types.LONGNVARCHAR: case Types.VARCHAR: { String obj = dataRow.getString(columnName); return (obj == null) ? "NULL" : processStringData(obj); } case Types.TIME: { Time obj = dataRow.getTime(columnName); return (obj == null) ? "NULL" : processStringData(obj.toString()); } case Types.DATE: { Date obj = dataRow.getDate(columnName); return (obj == null) ? "NULL" : processStringData(obj.toString()); } case Types.TIMESTAMP: { Timestamp obj = dataRow.getTimestamp(columnName); return (obj == null) ? "NULL" : processStringData(obj.toString()); } case Types.SMALLINT: { Object obj = dataRow.getObject(columnName); return (obj == null) ? "NULL" : obj.toString(); } case Types.BIGINT: { Object obj = dataRow.getObject(columnName); return (obj == null) ? "NULL" : obj.toString(); } case Types.INTEGER: { Object obj = dataRow.getObject(columnName); return (obj == null) ? "NULL" : obj.toString(); } case Types.NUMERIC: case Types.DECIMAL: { BigDecimal obj = dataRow.getBigDecimal(columnName); return (obj == null) ? "NULL" : String.valueOf(obj); } case Types.REAL: case Types.FLOAT: { Float obj = dataRow.getFloat(columnName); // dataRow.getFloat() always returns a value. only way to check the null is wasNull() method return (dataRow.wasNull()) ? "NULL" : String.valueOf(obj); } case Types.DOUBLE: { Double obj = dataRow.getDouble(columnName); return (dataRow.wasNull()) ? "NULL" : String.valueOf(obj); } default: { Object obj = dataRow.getObject(columnName); return (obj == null) ? "NULL" : obj.toString(); } } }
From source file:org.silverpeas.core.test.util.SQLRequester.java
/** * Lists entities by executing the specified SQL query with the given parameters. * @param jdbcSqlQuery the {@link JdbcSqlQuery} instance. * @return a {@link List} of {@link ResultLine} instances. * @throws SQLException if an error occurs while executing the given {@link JdbcSqlQuery}. */// w w w . j ava 2s . co m public static List<ResultLine> list(JdbcSqlQuery jdbcSqlQuery) throws SQLException { final List<Pair<String, Integer>> metaData = new ArrayList<>(); return jdbcSqlQuery.execute(row -> { if (metaData.isEmpty()) { ResultSetMetaData rowMetaData = row.getMetaData(); for (int i = 1; i <= rowMetaData.getColumnCount(); i++) { metaData.add(Pair.of(rowMetaData.getColumnName(i), rowMetaData.getColumnType(i))); } } SQLRequester.ResultLine line = new SQLRequester.ResultLine(); for (int i = 1; i <= metaData.size(); i++) { Pair<String, Integer> columnNameAndType = metaData.get(i - 1); String name = columnNameAndType.getLeft().toUpperCase(); final Object value; int sqlType = columnNameAndType.getRight(); switch (sqlType) { case Types.BIGINT: value = row.getLong(i); break; case Types.DECIMAL: value = row.getBigDecimal(i); break; case Types.INTEGER: value = row.getInt(i); break; case Types.TIMESTAMP: value = row.getTimestamp(i); break; case Types.DATE: value = row.getDate(i); break; case Types.BOOLEAN: value = row.getBoolean(i); break; default: value = row.getString(i); } line.set(name, value); } return line; }); }
From source file:org.apache.syncope.core.persistence.jpa.content.ContentLoaderHandler.java
private Object[] getParameters(final String tableName, final Attributes attrs) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); Map<String, Integer> colTypes = jdbcTemplate.query("SELECT * FROM " + tableName + " WHERE 0=1", new ResultSetExtractor<Map<String, Integer>>() { @Override//from ww w. java 2s.c o m public Map<String, Integer> extractData(final ResultSet rs) throws SQLException { Map<String, Integer> colTypes = new HashMap<>(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { colTypes.put(rs.getMetaData().getColumnName(i).toUpperCase(), rs.getMetaData().getColumnType(i)); } return colTypes; } }); Object[] parameters = new Object[attrs.getLength()]; for (int i = 0; i < attrs.getLength(); i++) { Integer colType = colTypes.get(attrs.getQName(i).toUpperCase()); if (colType == null) { LOG.warn("No column type found for {}", attrs.getQName(i).toUpperCase()); colType = Types.VARCHAR; } switch (colType) { case Types.INTEGER: case Types.TINYINT: case Types.SMALLINT: try { parameters[i] = Integer.valueOf(attrs.getValue(i)); } catch (NumberFormatException e) { LOG.error("Unparsable Integer '{}'", attrs.getValue(i)); parameters[i] = attrs.getValue(i); } break; case Types.NUMERIC: case Types.DECIMAL: case Types.BIGINT: try { parameters[i] = Long.valueOf(attrs.getValue(i)); } catch (NumberFormatException e) { LOG.error("Unparsable Long '{}'", attrs.getValue(i)); parameters[i] = attrs.getValue(i); } break; case Types.DOUBLE: try { parameters[i] = Double.valueOf(attrs.getValue(i)); } catch (NumberFormatException e) { LOG.error("Unparsable Double '{}'", attrs.getValue(i)); parameters[i] = attrs.getValue(i); } break; case Types.REAL: case Types.FLOAT: try { parameters[i] = Float.valueOf(attrs.getValue(i)); } catch (NumberFormatException e) { LOG.error("Unparsable Float '{}'", attrs.getValue(i)); parameters[i] = attrs.getValue(i); } break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: try { parameters[i] = FormatUtils.parseDate(attrs.getValue(i)); } catch (ParseException e) { LOG.error("Unparsable Date '{}'", attrs.getValue(i)); parameters[i] = attrs.getValue(i); } break; case Types.BIT: case Types.BOOLEAN: parameters[i] = "1".equals(attrs.getValue(i)) ? Boolean.TRUE : Boolean.FALSE; break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: try { parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray()); } catch (DecoderException | IllegalArgumentException e) { parameters[i] = attrs.getValue(i); } break; case Types.BLOB: try { parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray()); } catch (DecoderException | IllegalArgumentException e) { LOG.warn("Error decoding hex string to specify a blob parameter", e); parameters[i] = attrs.getValue(i); } catch (Exception e) { LOG.warn("Error creating a new blob parameter", e); } break; default: parameters[i] = attrs.getValue(i); } } return parameters; }
From source file:org.apache.ddlutils.platform.oracle.Oracle8ModelReader.java
/** * {@inheritDoc}//w ww . jav a 2 s . com */ protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException { Column column = super.readColumn(metaData, values); if (column.getDefaultValue() != null) { // Oracle pads the default value with spaces column.setDefaultValue(column.getDefaultValue().trim()); } if (column.getTypeCode() == Types.DECIMAL) { // We're back-mapping the NUMBER columns returned by Oracle // Note that the JDBC driver returns DECIMAL for these NUMBER columns switch (column.getSizeAsInt()) { case 1: if (column.getScale() == 0) { column.setTypeCode(Types.BIT); } break; case 3: if (column.getScale() == 0) { column.setTypeCode(Types.TINYINT); } break; case 5: if (column.getScale() == 0) { column.setTypeCode(Types.SMALLINT); } break; case 18: column.setTypeCode(Types.REAL); break; case 22: if (column.getScale() == 0) { column.setTypeCode(Types.INTEGER); } break; case 38: if (column.getScale() == 0) { column.setTypeCode(Types.BIGINT); } else { column.setTypeCode(Types.DOUBLE); } break; } } else if (column.getTypeCode() == Types.FLOAT) { // Same for REAL, FLOAT, DOUBLE PRECISION, which all back-map to FLOAT but with // different sizes (63 for REAL, 126 for FLOAT/DOUBLE PRECISION) switch (column.getSizeAsInt()) { case 63: column.setTypeCode(Types.REAL); break; case 126: column.setTypeCode(Types.DOUBLE); break; } } else if ((column.getTypeCode() == Types.DATE) || (column.getTypeCode() == Types.TIMESTAMP)) { // Oracle has only one DATE/TIME type, so we can't know which it is and thus map // it back to TIMESTAMP column.setTypeCode(Types.TIMESTAMP); // we also reverse the ISO-format adaptation, and adjust the default value to timestamp if (column.getDefaultValue() != null) { Matcher matcher = _oracleIsoTimestampPattern.matcher(column.getDefaultValue()); Timestamp timestamp = null; if (matcher.matches()) { String timestampVal = matcher.group(1); timestamp = Timestamp.valueOf(timestampVal); } else { matcher = _oracleIsoDatePattern.matcher(column.getDefaultValue()); if (matcher.matches()) { String dateVal = matcher.group(1); timestamp = new Timestamp(Date.valueOf(dateVal).getTime()); } else { matcher = _oracleIsoTimePattern.matcher(column.getDefaultValue()); if (matcher.matches()) { String timeVal = matcher.group(1); timestamp = new Timestamp(Time.valueOf(timeVal).getTime()); } } } if (timestamp != null) { column.setDefaultValue(timestamp.toString()); } } } else if (TypeMap.isTextType(column.getTypeCode())) { column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''")); } return column; }
From source file:org.apache.openjpa.jdbc.schema.Schemas.java
/** * Return the {@link Types} constant for the given SQL type name. *//*ww w . ja v a 2 s .c om*/ public static int getJDBCType(String name) { if ("array".equalsIgnoreCase(name)) return Types.ARRAY; if ("bigint".equalsIgnoreCase(name)) return Types.BIGINT; if ("binary".equalsIgnoreCase(name)) return Types.BINARY; if ("bit".equalsIgnoreCase(name)) return Types.BIT; if ("blob".equalsIgnoreCase(name)) return Types.BLOB; if ("char".equalsIgnoreCase(name)) return Types.CHAR; if ("clob".equalsIgnoreCase(name)) return Types.CLOB; if ("date".equalsIgnoreCase(name)) return Types.DATE; if ("decimal".equalsIgnoreCase(name)) return Types.DECIMAL; if ("distinct".equalsIgnoreCase(name)) return Types.DISTINCT; if ("double".equalsIgnoreCase(name)) return Types.DOUBLE; if ("float".equalsIgnoreCase(name)) return Types.FLOAT; if ("integer".equalsIgnoreCase(name)) return Types.INTEGER; if ("java_object".equalsIgnoreCase(name)) return Types.JAVA_OBJECT; if ("longvarbinary".equalsIgnoreCase(name)) return Types.LONGVARBINARY; if ("longvarchar".equalsIgnoreCase(name)) return Types.LONGVARCHAR; if ("null".equalsIgnoreCase(name)) return Types.NULL; if ("numeric".equalsIgnoreCase(name)) return Types.NUMERIC; if ("other".equalsIgnoreCase(name)) return Types.OTHER; if ("real".equalsIgnoreCase(name)) return Types.REAL; if ("ref".equalsIgnoreCase(name)) return Types.REF; if ("smallint".equalsIgnoreCase(name)) return Types.SMALLINT; if ("struct".equalsIgnoreCase(name)) return Types.STRUCT; if ("time".equalsIgnoreCase(name)) return Types.TIME; if ("timestamp".equalsIgnoreCase(name)) return Types.TIMESTAMP; if ("tinyint".equalsIgnoreCase(name)) return Types.TINYINT; if ("varbinary".equalsIgnoreCase(name)) return Types.VARBINARY; if ("varchar".equalsIgnoreCase(name)) return Types.VARCHAR; if (name == null || name.toLowerCase().startsWith("unknown")) return Types.OTHER; throw new IllegalArgumentException("name = " + name); }
From source file:org.apache.sqoop.manager.ConnManager.java
/** * Resolve a database-specific type to the Java type that should contain it. * @param sqlType sql type//from w w w . ja v a 2s . c o m * @return the name of a Java type to hold the sql datatype, or null if none. */ public String toJavaType(int sqlType) { // Mappings taken from: // http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html if (sqlType == Types.INTEGER) { return "Integer"; } else if (sqlType == Types.VARCHAR) { return "String"; } else if (sqlType == Types.CHAR) { return "String"; } else if (sqlType == Types.LONGVARCHAR) { return "String"; } else if (sqlType == Types.NVARCHAR) { return "String"; } else if (sqlType == Types.NCHAR) { return "String"; } else if (sqlType == Types.LONGNVARCHAR) { return "String"; } else if (sqlType == Types.NUMERIC) { return "java.math.BigDecimal"; } else if (sqlType == Types.DECIMAL) { return "java.math.BigDecimal"; } else if (sqlType == Types.BIT) { return "Boolean"; } else if (sqlType == Types.BOOLEAN) { return "Boolean"; } else if (sqlType == Types.TINYINT) { return "Integer"; } else if (sqlType == Types.SMALLINT) { return "Integer"; } else if (sqlType == Types.BIGINT) { return "Long"; } else if (sqlType == Types.REAL) { return "Float"; } else if (sqlType == Types.FLOAT) { return "Double"; } else if (sqlType == Types.DOUBLE) { return "Double"; } else if (sqlType == Types.DATE) { return "java.sql.Date"; } else if (sqlType == Types.TIME) { return "java.sql.Time"; } else if (sqlType == Types.TIMESTAMP) { return "java.sql.Timestamp"; } else if (sqlType == Types.BINARY || sqlType == Types.VARBINARY) { return BytesWritable.class.getName(); } else if (sqlType == Types.CLOB) { return ClobRef.class.getName(); } else if (sqlType == Types.BLOB || sqlType == Types.LONGVARBINARY) { return BlobRef.class.getName(); } else { // TODO(aaron): Support DISTINCT, ARRAY, STRUCT, REF, JAVA_OBJECT. // Return null indicating database-specific manager should return a // java data type if it can find one for any nonstandard type. return null; } }
From source file:com.streamsets.pipeline.stage.it.DecimalTypeIT.java
@Test public void correctCases() throws Exception { executeUpdate("CREATE TABLE `tbl` (id int, dec decimal(4, 2)) PARTITIONED BY (dt string) STORED AS AVRO"); HiveMetadataProcessor processor = new HiveMetadataProcessorBuilder().decimalConfig(4, 2).build(); HiveMetastoreTarget hiveTarget = new HiveMetastoreTargetBuilder().build(); List<Record> records = new LinkedList<>(); Map<String, Field> map = new LinkedHashMap<>(); map.put("id", Field.create(Field.Type.INTEGER, 1)); map.put("dec", Field.create(BigDecimal.valueOf(12.12))); Record record = RecordCreator.create("s", "s:1"); record.set(Field.create(map)); records.add(record);/*from www . jav a 2 s.co m*/ map = new LinkedHashMap<>(); map.put("id", Field.create(Field.Type.INTEGER, 2)); map.put("dec", Field.create(BigDecimal.valueOf(1.0))); record = RecordCreator.create("s", "s:1"); record.set(Field.create(map)); records.add(record); map = new LinkedHashMap<>(); map.put("id", Field.create(Field.Type.INTEGER, 3)); map.put("dec", Field.create(BigDecimal.valueOf(12.0))); record = RecordCreator.create("s", "s:1"); record.set(Field.create(map)); records.add(record); map = new LinkedHashMap<>(); map.put("id", Field.create(Field.Type.INTEGER, 4)); map.put("dec", Field.create(BigDecimal.valueOf(0.1))); record = RecordCreator.create("s", "s:1"); record.set(Field.create(map)); records.add(record); map = new LinkedHashMap<>(); map.put("id", Field.create(Field.Type.INTEGER, 5)); map.put("dec", Field.create(BigDecimal.valueOf(0.12))); record = RecordCreator.create("s", "s:1"); record.set(Field.create(map)); records.add(record); processRecords(processor, hiveTarget, records); assertQueryResult("select * from tbl order by id", new QueryValidator() { @Override public void validateResultSet(ResultSet rs) throws Exception { assertResultSetStructure(rs, new ImmutablePair("tbl.id", Types.INTEGER), new ImmutablePair("tbl.dec", Types.DECIMAL), new ImmutablePair("tbl.dt", Types.VARCHAR)); Assert.assertTrue("Table tbl doesn't contain any rows", rs.next()); Assert.assertEquals(1, rs.getLong(1)); Assert.assertEquals(BigDecimal.valueOf(12.12), rs.getBigDecimal(2)); Assert.assertTrue("Unexpected number of rows", rs.next()); Assert.assertEquals(2, rs.getLong(1)); Assert.assertEquals(BigDecimal.valueOf(1), rs.getBigDecimal(2)); Assert.assertTrue("Unexpected number of rows", rs.next()); Assert.assertEquals(3, rs.getLong(1)); Assert.assertEquals(BigDecimal.valueOf(12), rs.getBigDecimal(2)); Assert.assertTrue("Unexpected number of rows", rs.next()); Assert.assertEquals(4, rs.getLong(1)); Assert.assertEquals(BigDecimal.valueOf(0.1), rs.getBigDecimal(2)); Assert.assertTrue("Unexpected number of rows", rs.next()); Assert.assertEquals(5, rs.getLong(1)); Assert.assertEquals(BigDecimal.valueOf(0.12), rs.getBigDecimal(2)); Assert.assertFalse("Unexpected number of rows", rs.next()); } }); }
From source file:org.apache.kylin.jdbc.KylinClient.java
public static Object wrapObject(String value, int sqlType) { if (null == value) { return null; }//from w w w .ja v a2 s. c o m switch (sqlType) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return value; case Types.NUMERIC: case Types.DECIMAL: return new BigDecimal(value); case Types.BIT: return Boolean.parseBoolean(value); case Types.TINYINT: return Byte.valueOf(value); case Types.SMALLINT: return Short.valueOf(value); case Types.INTEGER: return Integer.parseInt(value); case Types.BIGINT: return Long.parseLong(value); case Types.FLOAT: return Float.parseFloat(value); case Types.REAL: case Types.DOUBLE: return Double.parseDouble(value); case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: return value.getBytes(); case Types.DATE: return Date.valueOf(value); case Types.TIME: return Time.valueOf(value); case Types.TIMESTAMP: return Timestamp.valueOf(value); default: //do nothing break; } return value; }