List of usage examples for java.sql Types BOOLEAN
int BOOLEAN
To view the source code for java.sql Types BOOLEAN.
Click Source Link
BOOLEAN
. From source file:com.liferay.portal.upgrade.util.Table.java
public Object getValue(ResultSet rs, String name, Integer type) throws Exception { Object value = null;// w w w . ja v a 2s . co m int t = type.intValue(); if (t == Types.BIGINT) { try { value = GetterUtil.getLong(rs.getLong(name)); } catch (SQLException e) { value = GetterUtil.getLong(rs.getString(name)); } } else if (t == Types.BOOLEAN) { value = GetterUtil.getBoolean(rs.getBoolean(name)); } else if (t == Types.CLOB) { try { Clob clob = rs.getClob(name); if (clob == null) { value = StringPool.BLANK; } else { UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(clob.getCharacterStream()); StringBundler sb = new StringBundler(); String line = null; while ((line = unsyncBufferedReader.readLine()) != null) { if (sb.length() != 0) { sb.append(SAFE_NEWLINE_CHARACTER); } sb.append(line); } value = sb.toString(); } } catch (Exception e) { // If the database doesn't allow CLOB types for the column // value, then try retrieving it as a String value = GetterUtil.getString(rs.getString(name)); } } else if (t == Types.DOUBLE) { value = GetterUtil.getDouble(rs.getDouble(name)); } else if (t == Types.FLOAT) { value = GetterUtil.getFloat(rs.getFloat(name)); } else if (t == Types.INTEGER) { value = GetterUtil.getInteger(rs.getInt(name)); } else if (t == Types.SMALLINT) { value = GetterUtil.getShort(rs.getShort(name)); } else if (t == Types.TIMESTAMP) { try { value = rs.getTimestamp(name); } catch (Exception e) { } if (value == null) { value = StringPool.NULL; } } else if (t == Types.VARCHAR) { value = GetterUtil.getString(rs.getString(name)); } else { throw new UpgradeException("Upgrade code using unsupported class type " + type); } return value; }
From source file:com.flexive.core.storage.GenericDivisionExporter.java
/** * Dump a generic table to XML//from w w w. ja v a 2 s . com * * @param tableName name of the table * @param stmt an open statement * @param out output stream * @param sb an available and valid StringBuilder * @param xmlTag name of the xml tag to write per row * @param idColumn (optional) id column to sort results * @param onlyBinaries process binary fields (else these will be ignored) * @throws SQLException on errors * @throws IOException on errors */ private void dumpTable(String tableName, Statement stmt, OutputStream out, StringBuilder sb, String xmlTag, String idColumn, boolean onlyBinaries) throws SQLException, IOException { ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + (StringUtils.isEmpty(idColumn) ? "" : " ORDER BY " + idColumn + " ASC")); final ResultSetMetaData md = rs.getMetaData(); String value, att; boolean hasSubTags; while (rs.next()) { hasSubTags = false; if (!onlyBinaries) { sb.setLength(0); sb.append(" <").append(xmlTag); } for (int i = 1; i <= md.getColumnCount(); i++) { value = null; att = md.getColumnName(i).toLowerCase(); switch (md.getColumnType(i)) { case java.sql.Types.DECIMAL: case java.sql.Types.NUMERIC: case java.sql.Types.BIGINT: if (!onlyBinaries) { value = String.valueOf(rs.getBigDecimal(i)); if (rs.wasNull()) value = null; } break; case java.sql.Types.INTEGER: case java.sql.Types.SMALLINT: case java.sql.Types.TINYINT: if (!onlyBinaries) { value = String.valueOf(rs.getLong(i)); if (rs.wasNull()) value = null; } break; case java.sql.Types.DOUBLE: case java.sql.Types.FLOAT: case java.sql.Types.REAL: if (!onlyBinaries) { value = String.valueOf(rs.getDouble(i)); if (rs.wasNull()) value = null; } break; case java.sql.Types.TIMESTAMP: case java.sql.Types.DATE: if (!onlyBinaries) { final Timestamp ts = rs.getTimestamp(i); if (rs.wasNull()) value = null; else value = FxFormatUtils.getDateTimeFormat().format(ts); } break; case java.sql.Types.BIT: case java.sql.Types.CHAR: case java.sql.Types.BOOLEAN: if (!onlyBinaries) { value = rs.getBoolean(i) ? "1" : "0"; if (rs.wasNull()) value = null; } break; case java.sql.Types.CLOB: case java.sql.Types.BLOB: case java.sql.Types.LONGVARBINARY: case java.sql.Types.LONGVARCHAR: case java.sql.Types.VARBINARY: case java.sql.Types.VARCHAR: case java.sql.Types.BINARY: case SQL_LONGNVARCHAR: case SQL_NCHAR: case SQL_NCLOB: case SQL_NVARCHAR: hasSubTags = true; break; default: LOG.warn("Unhandled type [" + md.getColumnType(i) + "] for [" + tableName + "." + att + "]"); } if (value != null && !onlyBinaries) sb.append(' ').append(att).append("=\"").append(value).append("\""); } if (hasSubTags) { if (!onlyBinaries) sb.append(">\n"); for (int i = 1; i <= md.getColumnCount(); i++) { switch (md.getColumnType(i)) { case java.sql.Types.VARBINARY: case java.sql.Types.LONGVARBINARY: case java.sql.Types.BLOB: case java.sql.Types.BINARY: if (idColumn == null) throw new IllegalArgumentException("Id column required to process binaries!"); String binFile = FOLDER_BINARY + "/BIN_" + String.valueOf(rs.getLong(idColumn)) + "_" + i + ".blob"; att = md.getColumnName(i).toLowerCase(); if (onlyBinaries) { if (!(out instanceof ZipOutputStream)) throw new IllegalArgumentException( "out has to be a ZipOutputStream to store binaries!"); ZipOutputStream zip = (ZipOutputStream) out; InputStream in = rs.getBinaryStream(i); if (rs.wasNull()) break; ZipEntry ze = new ZipEntry(binFile); zip.putNextEntry(ze); byte[] buffer = new byte[4096]; int read; while ((read = in.read(buffer)) != -1) zip.write(buffer, 0, read); in.close(); zip.closeEntry(); zip.flush(); } else { InputStream in = rs.getBinaryStream(i); //need to fetch to see if it is empty if (rs.wasNull()) break; in.close(); sb.append(" <").append(att).append(">").append(binFile).append("</").append(att) .append(">\n"); } break; case java.sql.Types.CLOB: case SQL_LONGNVARCHAR: case SQL_NCHAR: case SQL_NCLOB: case SQL_NVARCHAR: case java.sql.Types.LONGVARCHAR: case java.sql.Types.VARCHAR: if (!onlyBinaries) { value = rs.getString(i); if (rs.wasNull()) break; att = md.getColumnName(i).toLowerCase(); sb.append(" <").append(att).append('>'); escape(sb, value); sb.append("</").append(att).append(">\n"); } break; } } if (!onlyBinaries) sb.append(" </").append(xmlTag).append(">\n"); } else { if (!onlyBinaries) sb.append("/>\n"); } if (!onlyBinaries) write(out, sb); } }
From source file:org.agnitas.dao.impl.ImportProfileDaoImpl.java
public int insertImportProfile(ImportProfile importProfile) { int profileId; if (AgnUtils.isOracleDB()) { logSqlStatement(logger, SELECT_NEXT_PROFILEID); profileId = getSimpleJdbcTemplate().queryForInt(SELECT_NEXT_PROFILEID); logSqlStatement(logger, INSERT_ORACLE); getSimpleJdbcTemplate().update(INSERT_ORACLE, profileId, importProfile.getCompanyId(), importProfile.getAdminId(), importProfile.getName(), importProfile.getSeparator(), importProfile.getTextRecognitionChar(), importProfile.getCharset(), importProfile.getDateFormat(), importProfile.getImportMode(), importProfile.getNullValuesAction(), importProfile.getKeyColumn(), ImportUtils.getBooleanAsInt(importProfile.getExtendedEmailCheck()), importProfile.getMailForReport(), importProfile.getCheckForDuplicates(), importProfile.getDefaultMailType(), ImportUtils.getBooleanAsInt(importProfile.getUpdateAllDuplicates())); } else {//from w ww. j a va 2 s. c o m logSqlStatement(logger, INSERT_MYSQL); SqlUpdate sqlUpdate = new SqlUpdate(getDataSource(), INSERT_MYSQL, new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.BOOLEAN }); sqlUpdate.setReturnGeneratedKeys(true); sqlUpdate.setGeneratedKeysColumnNames(new String[] { FIELD_ID }); sqlUpdate.compile(); GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder(); Object[] values = new Object[] { importProfile.getCompanyId(), importProfile.getAdminId(), importProfile.getName(), importProfile.getSeparator(), importProfile.getTextRecognitionChar(), importProfile.getCharset(), importProfile.getDateFormat(), importProfile.getImportMode(), importProfile.getNullValuesAction(), importProfile.getKeyColumn(), ImportUtils.getBooleanAsInt(importProfile.getExtendedEmailCheck()), importProfile.getMailForReport(), importProfile.getCheckForDuplicates(), importProfile.getDefaultMailType(), ImportUtils.getBooleanAsInt(importProfile.getUpdateAllDuplicates()) }; sqlUpdate.update(values, generatedKeyHolder); profileId = generatedKeyHolder.getKey().intValue(); } importProfile.setId(profileId); insertColumnMappings(importProfile.getColumnMapping(), importProfile.getId()); insertGenderMappings(importProfile.getGenderMapping(), importProfile.getId()); return importProfile.getId(); }
From source file:com.mmnaseri.dragonfly.dialect.impl.Mysql5Dialect.java
@Override public String getType(ColumnMetadata columnMetadata) { if (columnMetadata.getType() == Types.BOOLEAN) { return "BIT"; }//from w w w . j a v a2s. com return super.getType(columnMetadata); }
From source file:org.opendatakit.common.persistence.engine.pgres.DatastoreImpl.java
public static void buildArgumentList(Object[] ol, int[] il, int idx, CommonFieldsBase entity, DataField f) { switch (f.getDataType()) { case BOOLEAN: ol[idx] = entity.getBooleanField(f); il[idx] = java.sql.Types.BOOLEAN; break;//from ww w. j a v a2 s .c om case STRING: case URI: ol[idx] = entity.getStringField(f); il[idx] = java.sql.Types.VARCHAR; break; case INTEGER: ol[idx] = entity.getLongField(f); il[idx] = java.sql.Types.BIGINT; break; case DECIMAL: ol[idx] = entity.getNumericField(f); il[idx] = java.sql.Types.DECIMAL; break; case DATETIME: ol[idx] = entity.getDateField(f); il[idx] = java.sql.Types.TIMESTAMP; break; case BINARY: ol[idx] = entity.getBlobField(f); il[idx] = java.sql.Types.LONGVARBINARY; break; case LONG_STRING: ol[idx] = entity.getStringField(f); il[idx] = java.sql.Types.LONGVARCHAR; break; default: throw new IllegalStateException("Unexpected data type"); } }
From source file:com.squid.core.domain.operators.ExtendedType.java
private String getTypeName(int SQLType) { switch (SQLType) { case Types.ARRAY: return "ARRAY"; case Types.BIGINT: return "INTEGER"; case Types.BINARY: return "BINARY"; case Types.BIT: return "BIT"; case Types.BLOB: return "BLOB"; case Types.BOOLEAN: return "BOOLEAN"; case Types.CHAR: return "CHAR"; case Types.CLOB: return "CLOB"; case Types.DATALINK: return "DATALINK"; case Types.DATE: return "DATE"; case Types.DECIMAL: return "DECIMAL"; case Types.DOUBLE: return "DOUBLE"; case Types.FLOAT: return "FLOAT"; case Types.INTEGER: return "INTEGER"; case Types.JAVA_OBJECT: return "JAVA_OBJECT"; case Types.LONGNVARCHAR: return "LONGNVARCHAR"; case Types.LONGVARBINARY: return "LONGVARBINARY"; case Types.NCHAR: return "NCHAR"; case Types.NCLOB: return "NCLOB"; case Types.NULL: return "UNDEFINED";// case Types.NUMERIC: return "NUMERIC"; case Types.NVARCHAR: return "NVARCHAR"; case Types.OTHER: return "UNDEFINED";// case Types.REAL: return "REAL"; case Types.REF: return "REF"; case Types.ROWID: return "ROWID"; case Types.SMALLINT: return "SMALLINT"; case Types.SQLXML: return "SQLXML"; case Types.STRUCT: return "STRUCT"; case Types.TIME: return "TIME"; case Types.TIMESTAMP: return "TIMESTAMP"; case Types.TINYINT: return "TINYINT"; case Types.VARBINARY: return "VARBINARY"; case Types.VARCHAR: return "VARCHAR"; default:/*from ww w. j a v a2 s . co m*/ return "UNDEFINED";// } }
From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java
public static void setBoolean(PreparedStatement stmt, Boolean value, int index) throws SQLException { if (value == null) { stmt.setNull(index, Types.BOOLEAN); return;// ww w. java 2 s . c o m } stmt.setBoolean(index, value); }
From source file:org.jumpmind.symmetric.db.derby.DerbyFunctions.java
public static String getPrimaryKeyWhereString(String[] pkColumnNames, ResultSet rs) throws SQLException { final String AND = " and "; ResultSetMetaData metaData = rs.getMetaData(); StringBuilder b = new StringBuilder(); for (int i = 0; i < pkColumnNames.length; i++) { String columnName = pkColumnNames[i]; int index = findColumnIndex(metaData, columnName); int type = metaData.getColumnType(index); if (type != Types.BINARY && type != Types.BLOB && type != Types.LONGVARBINARY && type != Types.VARBINARY) { b.append("\"").append(columnName).append("\"="); switch (type) { case Types.BIT: case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: case Types.FLOAT: case Types.REAL: case Types.DOUBLE: case Types.NUMERIC: case Types.DECIMAL: case Types.BOOLEAN: b.append(rs.getObject(index)); break; case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: b.append("\"").append(rs.getString(index)).append("\""); break; case Types.DATE: case Types.TIMESTAMP: b.append("{ts '"); b.append(rs.getString(index)); b.append("'}"); break; }/*from ww w .j av a 2 s.c o m*/ b.append(AND); } } b.replace(b.length() - AND.length(), b.length(), ""); return b.toString(); }
From source file:org.apache.ddlutils.model.Column.java
/** * Tries to parse the default value of the column and returns it as an object of the * corresponding java type. If the value could not be parsed, then the original * definition is returned./*www . j a v a 2 s.co m*/ * * @return The parsed default value */ public Object getParsedDefaultValue() { if ((_defaultValue != null) && (_defaultValue.length() > 0)) { try { switch (_typeCode) { case Types.TINYINT: case Types.SMALLINT: return new Short(_defaultValue); case Types.INTEGER: return new Integer(_defaultValue); case Types.BIGINT: return new Long(_defaultValue); case Types.DECIMAL: case Types.NUMERIC: return new BigDecimal(_defaultValue); case Types.REAL: return new Float(_defaultValue); case Types.DOUBLE: case Types.FLOAT: return new Double(_defaultValue); case Types.DATE: return Date.valueOf(_defaultValue); case Types.TIME: return Time.valueOf(_defaultValue); case Types.TIMESTAMP: return Timestamp.valueOf(_defaultValue); case Types.BIT: case Types.BOOLEAN: return ConvertUtils.convert(_defaultValue, Boolean.class); } } catch (NumberFormatException ex) { return null; } catch (IllegalArgumentException ex) { return null; } } return _defaultValue; }
From source file:org.wso2.cep.broker.RDBMSBrokerType.java
private TableInfo initializeTableInfo(String databaseName, String tableName, Object message, BrokerConfiguration brokerConfiguration, Connection connection) throws SQLException { TableInfo tableInfo = new TableInfo(); tableInfo.setTableName(tableName);/*from w ww. j av a 2s. com*/ // create the table. StringBuilder stringBuilder = new StringBuilder("CREATE TABLE IF NOT EXISTS "); stringBuilder.append(tableName); stringBuilder.append(" ("); boolean appendComma = false; for (Map.Entry<String, Object> entry : (((Map<String, Object>) message).entrySet())) { if (appendComma) { stringBuilder.append(","); } else { appendComma = true; } stringBuilder.append(entry.getKey()).append(" "); if (entry.getValue() instanceof Integer) { stringBuilder.append("INT"); } else if (entry.getValue() instanceof Long) { stringBuilder.append("BIGINT"); } else if (entry.getValue() instanceof Float) { stringBuilder.append("FLOAT"); } else if (entry.getValue() instanceof Double) { stringBuilder.append("DOUBLE"); } else if (entry.getValue() instanceof String) { stringBuilder.append("VARCHAR(255)"); } else if (entry.getValue() instanceof Boolean) { stringBuilder.append("BOOL"); } } stringBuilder.append(")"); Statement statement = connection.createStatement(); statement.executeUpdate(stringBuilder.toString()); statement.close(); ArrayList<Attribute> tableColumnList = new ArrayList<Attribute>(); stringBuilder = new StringBuilder("INSERT INTO "); stringBuilder.append(tableName); stringBuilder.append(" ( "); StringBuilder values = new StringBuilder(""); appendComma = false; DatabaseMetaData databaseMetaData = connection.getMetaData(); ResultSet rs = databaseMetaData.getColumns(null, null, databaseName + "." + tableName, null); while (rs.next()) { AttributeType type = null; int colType = rs.getInt("DATA_TYPE"); switch (colType) { case Types.VARCHAR: type = AttributeType.STRING; break; case Types.INTEGER: type = AttributeType.INT; break; case Types.BIGINT: type = AttributeType.LONG; break; case Types.DOUBLE: type = AttributeType.DOUBLE; break; case Types.FLOAT: type = AttributeType.FLOAT; break; case Types.BOOLEAN: type = AttributeType.BOOL; break; } Attribute attribute = new Attribute(rs.getString("COLUMN_NAME"), type); tableColumnList.add(attribute); if (appendComma) { stringBuilder.append(","); values.append(","); } else { appendComma = true; } stringBuilder.append(attribute.getName()); values.append("?"); } stringBuilder.append(") VALUES ("); stringBuilder.append(values); stringBuilder.append(")"); tableInfo.setColumnOrder(tableColumnList); tableInfo.setPreparedStatement(connection.prepareStatement(stringBuilder.toString())); return tableInfo; }