List of usage examples for java.sql Types NUMERIC
int NUMERIC
To view the source code for java.sql Types NUMERIC.
Click Source Link
The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type NUMERIC
.
From source file:org.jfree.data.jdbc.JDBCCategoryDataset.java
/** * Populates the dataset by executing the supplied query against the * existing database connection. If no connection exists then no action * is taken./*from w ww . j ava2s.c o m*/ * <p> * The results from the query are extracted and cached locally, thus * applying an upper limit on how many rows can be retrieved successfully. * * @param con the connection. * @param query the query. * * @throws SQLException if there is a problem executing the query. */ public void executeQuery(Connection con, String query) throws SQLException { Statement statement = null; ResultSet resultSet = null; try { statement = con.createStatement(); resultSet = statement.executeQuery(query); ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); if (columnCount < 2) { throw new SQLException("JDBCCategoryDataset.executeQuery() : insufficient columns " + "returned from the database."); } // Remove any previous old data int i = getRowCount(); while (--i >= 0) { removeRow(i); } while (resultSet.next()) { // first column contains the row key... Comparable rowKey = resultSet.getString(1); for (int column = 2; column <= columnCount; column++) { Comparable columnKey = metaData.getColumnName(column); int columnType = metaData.getColumnType(column); switch (columnType) { case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: case Types.FLOAT: case Types.DOUBLE: case Types.DECIMAL: case Types.NUMERIC: case Types.REAL: { Number value = (Number) resultSet.getObject(column); if (this.transpose) { setValue(value, columnKey, rowKey); } else { setValue(value, rowKey, columnKey); } break; } case Types.DATE: case Types.TIME: case Types.TIMESTAMP: { Date date = (Date) resultSet.getObject(column); Number value = new Long(date.getTime()); if (this.transpose) { setValue(value, columnKey, rowKey); } else { setValue(value, rowKey, columnKey); } break; } case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: { String string = (String) resultSet.getObject(column); try { Number value = Double.valueOf(string); if (this.transpose) { setValue(value, columnKey, rowKey); } else { setValue(value, rowKey, columnKey); } } catch (NumberFormatException e) { // suppress (value defaults to null) } break; } default: // not a value, can't use it (defaults to null) break; } } } fireDatasetChanged(new DatasetChangeInfo()); //TODO: fill in real change info } finally { if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { // report this? } } if (statement != null) { try { statement.close(); } catch (Exception e) { // report this? } } } }
From source file:com.redhat.rhn.domain.config.ConfigurationFactory.java
/** * Save a new ConfigRevision./*from w w w.j a va 2s . c o m*/ * Note, this method uses a stored procedure, so it must be used for all newly * created configuration revisions. * NOTE: This configuration revision must have a persisted config file * attached to it. config files also used stored procedures for * insertions, so we can't simply ask hibernate to save it for us. * @param revision the new ConfigRevision we want to store. * @return returns revision id */ public static Long saveNewConfigRevision(ConfigRevision revision) { //This is designed to catch some of the cases in which the config file //was not saved before the config revision. //There is still the possibility that the config file hasn't been committed to //the database yet, but someone has set its id. This should never happen from the //web site, but it might happen from tests. if (revision.getConfigFile() == null || revision.getConfigFile().getId() == null) { throw new IllegalStateException("Config Channels must be " + "saved before config files"); } CallableMode m = ModeFactory.getCallableMode("config_queries", "create_new_config_revision"); if (revision.isFile()) { //We need to save the content first so that we have an id for // the stored procedure. singleton.saveObject(revision.getConfigContent()); } //We do not have to save the ConfigInfo, because the info should always already be // in the database. If this is not the case, please read the documentation for // lookupOrInsertConfigInfo(String, String, Long) and correct the problem. Map inParams = new HashMap(); Map outParams = new HashMap(); inParams.put("revision_in", revision.getRevision()); inParams.put("config_file_id_in", revision.getConfigFile().getId()); if (revision.isFile()) { inParams.put("config_content_id_in", revision.getConfigContent().getId()); } else { inParams.put("config_content_id_in", null); } inParams.put("config_info_id_in", revision.getConfigInfo().getId()); inParams.put("config_file_type_id", new Long(revision.getConfigFileType().getId())); // Outparam outParams.put("configRevisionId", new Integer(Types.NUMERIC)); Map result = m.execute(inParams, outParams); return (Long) result.get("configRevisionId"); }
From source file:com.redhat.rhn.common.db.datasource.test.AdvDataSourceTest.java
public void testStoredProcedureOracle() throws Exception { CallableMode m = ModeFactory.getCallableMode("test_queries", "stored_procedure_oracle_format"); Map inParams = new HashMap(); Map outParams = new HashMap(); inParams.put("label", "noarch"); outParams.put("arch", new Integer(Types.NUMERIC)); Map row = m.execute(inParams, outParams); assertNotNull(row);/*from w ww . j av a2 s. co m*/ assertEquals(100, ((Long) row.get("arch")).intValue()); }
From source file:org.apache.hadoop.sqoop.manager.SqlManager.java
public String toJavaType(int sqlType) { // mappings 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.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 {/*from www . j a v a 2 s.co m*/ // TODO(aaron): Support BINARY, VARBINARY, LONGVARBINARY, DISTINCT, CLOB, BLOB, ARRAY, // STRUCT, REF, JAVA_OBJECT. return null; } }
From source file:org.jumpmind.vaadin.ui.common.CommonUiUtils.java
public static Table putResultsInTable(final ResultSet rs, int maxResultSize, final boolean showRowNumbers, String... excludeValues) throws SQLException { final Table table = createTable(); table.setImmediate(true);// w w w . ja v a 2 s. co m table.setSortEnabled(true); table.setSelectable(true); table.setMultiSelect(true); table.setColumnReorderingAllowed(true); table.setColumnReorderingAllowed(true); table.setColumnCollapsingAllowed(true); final ResultSetMetaData meta = rs.getMetaData(); int columnCount = meta.getColumnCount(); table.addContainerProperty("#", Integer.class, null); Set<String> columnNames = new HashSet<String>(); Set<Integer> skipColumnIndexes = new HashSet<Integer>(); int[] types = new int[columnCount]; for (int i = 1; i <= columnCount; i++) { String realColumnName = meta.getColumnName(i); String columnName = realColumnName; if (!Arrays.asList(excludeValues).contains(columnName)) { int index = 1; while (columnNames.contains(columnName)) { columnName = realColumnName + "_" + index++; } columnNames.add(columnName); Class<?> typeClass = Object.class; int type = meta.getColumnType(i); types[i - 1] = type; switch (type) { case Types.FLOAT: case Types.DOUBLE: case Types.NUMERIC: case Types.REAL: case Types.DECIMAL: typeClass = BigDecimal.class; break; case Types.TINYINT: case Types.SMALLINT: case Types.BIGINT: case Types.INTEGER: typeClass = Long.class; break; case Types.VARCHAR: case Types.CHAR: case Types.NVARCHAR: case Types.NCHAR: case Types.CLOB: typeClass = String.class; default: break; } table.addContainerProperty(i, typeClass, null); table.setColumnHeader(i, columnName); } else { skipColumnIndexes.add(i - 1); } } int rowNumber = 1; while (rs.next() && rowNumber <= maxResultSize) { Object[] row = new Object[columnNames.size() + 1]; row[0] = new Integer(rowNumber); int rowIndex = 1; for (int i = 0; i < columnCount; i++) { if (!skipColumnIndexes.contains(i)) { Object o = getObject(rs, i + 1); int type = types[i]; switch (type) { case Types.FLOAT: case Types.DOUBLE: case Types.REAL: case Types.NUMERIC: case Types.DECIMAL: if (o == null) { o = new BigDecimal(-1); } if (!(o instanceof BigDecimal)) { o = new BigDecimal(castToNumber(o.toString())); } break; case Types.TINYINT: case Types.SMALLINT: case Types.BIGINT: case Types.INTEGER: if (o == null) { o = new Long(-1); } if (!(o instanceof Long)) { o = new Long(castToNumber(o.toString())); } break; default: break; } row[rowIndex] = o == null ? NULL_TEXT : o; rowIndex++; } } table.addItem(row, rowNumber); rowNumber++; } if (rowNumber < 100) { table.setColumnWidth("#", 18); } else if (rowNumber < 1000) { table.setColumnWidth("#", 25); } else { table.setColumnWidth("#", 30); } if (!showRowNumbers) { table.setColumnCollapsed("#", true); } return table; }
From source file:org.jfree.data.jdbc.JDBCXYDataset.java
/** * ExecuteQuery will attempt execute the query passed to it against the * provided database connection. If connection is null then no action is * taken./* w ww . java 2 s .c om*/ * * The results from the query are extracted and cached locally, thus * applying an upper limit on how many rows can be retrieved successfully. * * @param query the query to be executed. * @param con the connection the query is to be executed against. * * @throws SQLException if there is a problem executing the query. */ public void executeQuery(Connection con, String query) throws SQLException { if (con == null) { throw new SQLException("There is no database to execute the query."); } ResultSet resultSet = null; Statement statement = null; try { statement = con.createStatement(); resultSet = statement.executeQuery(query); ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); int numberOfValidColumns = 0; int[] columnTypes = new int[numberOfColumns]; for (int column = 0; column < numberOfColumns; column++) { try { int type = metaData.getColumnType(column + 1); switch (type) { case Types.NUMERIC: case Types.REAL: case Types.INTEGER: case Types.DOUBLE: case Types.FLOAT: case Types.DECIMAL: case Types.BIT: case Types.DATE: case Types.TIME: case Types.TIMESTAMP: case Types.BIGINT: case Types.SMALLINT: ++numberOfValidColumns; columnTypes[column] = type; break; default: columnTypes[column] = Types.NULL; break; } } catch (SQLException e) { columnTypes[column] = Types.NULL; throw e; } } if (numberOfValidColumns <= 1) { throw new SQLException("Not enough valid columns where generated by query."); } /// First column is X data this.columnNames = new String[numberOfValidColumns - 1]; /// Get the column names and cache them. int currentColumn = 0; for (int column = 1; column < numberOfColumns; column++) { if (columnTypes[column] != Types.NULL) { this.columnNames[currentColumn] = metaData.getColumnLabel(column + 1); ++currentColumn; } } // Might need to add, to free memory from any previous result sets if (this.rows != null) { for (int column = 0; column < this.rows.size(); column++) { ArrayList row = (ArrayList) this.rows.get(column); row.clear(); } this.rows.clear(); } // Are we working with a time series. switch (columnTypes[0]) { case Types.DATE: case Types.TIME: case Types.TIMESTAMP: this.isTimeSeries = true; break; default: this.isTimeSeries = false; break; } // Get all rows. // rows = new ArrayList(); while (resultSet.next()) { ArrayList newRow = new ArrayList(); for (int column = 0; column < numberOfColumns; column++) { Object xObject = resultSet.getObject(column + 1); switch (columnTypes[column]) { case Types.NUMERIC: case Types.REAL: case Types.INTEGER: case Types.DOUBLE: case Types.FLOAT: case Types.DECIMAL: case Types.BIGINT: case Types.SMALLINT: newRow.add(xObject); break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: newRow.add(new Long(((Date) xObject).getTime())); break; case Types.NULL: break; default: System.err.println("Unknown data"); columnTypes[column] = Types.NULL; break; } } this.rows.add(newRow); } /// a kludge to make everything work when no rows returned if (this.rows.size() == 0) { ArrayList newRow = new ArrayList(); for (int column = 0; column < numberOfColumns; column++) { if (columnTypes[column] != Types.NULL) { newRow.add(new Integer(0)); } } this.rows.add(newRow); } /// Determine max and min values. if (this.rows.size() < 1) { this.maxValue = 0.0; this.minValue = 0.0; } else { ArrayList row = (ArrayList) this.rows.get(0); this.maxValue = Double.NEGATIVE_INFINITY; this.minValue = Double.POSITIVE_INFINITY; for (int rowNum = 0; rowNum < this.rows.size(); ++rowNum) { row = (ArrayList) this.rows.get(rowNum); for (int column = 1; column < numberOfColumns; column++) { Object testValue = row.get(column); if (testValue != null) { double test = ((Number) testValue).doubleValue(); if (test < this.minValue) { this.minValue = test; } if (test > this.maxValue) { this.maxValue = test; } } } } } fireDatasetChanged(new DatasetChangeInfo()); //TODO: fill in real change info } finally { if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { // TODO: is this a good idea? } } if (statement != null) { try { statement.close(); } catch (Exception e) { // TODO: is this a good idea? } } } }
From source file:architecture.ee.web.logo.dao.jdbc.JdbcLogoImageDao.java
public List<Long> getLogoImageIds(int objectType, long objectId) { return getExtendedJdbcTemplate().queryForList( getBoundSql("ARCHITECTURE_WEB.SELECT_LOGO_IMAGE_IDS_BY_OBJECT_TYPE_AND_OBJECT_ID").getSql(), Long.class, new SqlParameterValue(Types.NUMERIC, objectType), new SqlParameterValue(Types.NUMERIC, objectId)); }
From source file:org.pentaho.metadata.util.SQLModelGenerator.java
private static DataType converDataType(int type) { switch (type) { case Types.FLOAT: case Types.BIT: case Types.DOUBLE: case Types.SMALLINT: case Types.REAL: case Types.DECIMAL: case Types.BIGINT: case Types.INTEGER: case Types.NUMERIC: return DataType.NUMERIC; case Types.BINARY: case Types.CLOB: case Types.BLOB: return DataType.BINARY; case Types.BOOLEAN: return DataType.BOOLEAN; case Types.DATE: return DataType.DATE; case Types.TIMESTAMP: return DataType.DATE; case Types.LONGVARCHAR: case Types.VARCHAR: return DataType.STRING; default:/*from w ww .j a v a 2 s .c om*/ return DataType.UNKNOWN; } }
From source file:org.apache.openjpa.jdbc.sql.SybaseDictionary.java
@Override public String[] getCreateTableSQL(Table table) { if (!createIdentityColumn) return super.getCreateTableSQL(table); StringBuilder buf = new StringBuilder(); buf.append("CREATE TABLE ").append(getFullName(table, false)).append(" ("); Column[] cols = table.getColumns();/*ww w .j av a 2 s. c om*/ boolean hasIdentity = false; for (int i = 0; i < cols.length; i++) { // can only have one identity column if (cols[i].isAutoAssigned()) { hasIdentity = true; } // The column may exist if dropping and recreating a table. if (cols[i].getIdentifier().getName().equals(identityColumnName)) { hasIdentity = true; // column type may be lost when recreating - reset to NUMERIC if (cols[i].getType() != Types.NUMERIC) { // should check if compatible cols[i].setType(Types.NUMERIC); } } buf.append(i == 0 ? "" : ", "); buf.append(getDeclareColumnSQL(cols[i], false)); } // add an identity column if we do not already have one if (!hasIdentity) buf.append(", ").append(identityColumnName).append(" NUMERIC IDENTITY UNIQUE"); PrimaryKey pk = table.getPrimaryKey(); if (pk != null) buf.append(", ").append(getPrimaryKeyConstraintSQL(pk)); Unique[] unqs = table.getUniques(); String unqStr; for (int i = 0; i < unqs.length; i++) { unqStr = getUniqueConstraintSQL(unqs[i]); if (unqStr != null) buf.append(", ").append(unqStr); } buf.append(")"); return new String[] { buf.toString() }; }
From source file:com.flexive.ejb.beans.BriefcaseEngineBean.java
/** * {@inheritDoc}/* w w w. j a v a 2 s .c o m*/ */ @Override @TransactionAttribute(TransactionAttributeType.REQUIRED) public void modify(long id, String name, String description, Long aclId) throws FxApplicationException { // Anything to do? if (name != null && name.trim().length() == 0) { name = null; } if (name == null && description == null && aclId == null) { return; } // Lookup the briefcase Briefcase br = load(id); if (br == null) { throw new FxNotFoundException("ex.briefcase.notFound", ("#" + id)); } // Permission checks checkEditBriefcase(br); // Delete operation Connection con = null; PreparedStatement ps = null; try { // Obtain a database connection con = Database.getDbConnection(); String sSql = "update " + DatabaseConst.TBL_BRIEFCASE + " set" + ((name == null) ? "" : " name=?, ") + ((aclId == null) ? "" : " acl=?, ") + ((description == null) ? "" : " description=?, ") + "mandator=mandator where id=" + id; ps = con.prepareStatement(sSql); int pos = 1; if (name != null) ps.setString(pos++, name); if (aclId != null) { if (aclId == -1) { ps.setNull(pos++, java.sql.Types.NUMERIC); } else { ps.setLong(pos++, aclId); } } if (description != null) ps.setString(pos, description); ps.executeUpdate(); } catch (SQLException exc) { EJBUtils.rollback(ctx); throw new FxLoadException(LOG, exc, "ex.briefcase.modifyFailed", br.getName()); } finally { closeObjects(BriefcaseEngineBean.class, con, ps); } }