List of usage examples for java.sql Types VARBINARY
int VARBINARY
To view the source code for java.sql Types VARBINARY.
Click Source Link
The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type VARBINARY
.
From source file:org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform.java
/** * {@inheritDoc}//from ww w.j av a2s . co m */ protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException { int typeCode = property.getColumn().getTypeCode(); Object value = dynaBean.get(property.getName()); // PostgreSQL doesn't like setNull for BYTEA columns if (value == null) { switch (typeCode) { case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: case Types.BLOB: statement.setBytes(sqlIndex, null); break; default: statement.setNull(sqlIndex, typeCode); break; } } else { super.setObject(statement, sqlIndex, dynaBean, property); } }
From source file:lineage2.gameserver.cache.CrestCache.java
/** * Method removeAllyCrest.//from w ww .j a v a 2s. c o m * @param pledgeId int */ public void removeAllyCrest(int pledgeId) { writeLock.lock(); try { _allyCrest.remove(_allyCrestId.remove(pledgeId)); } finally { writeLock.unlock(); } Connection con = null; PreparedStatement statement = null; try { con = DatabaseFactory.getInstance().getConnection(); statement = con.prepareStatement("UPDATE ally_data SET crest=? WHERE ally_id=?"); statement.setNull(1, Types.VARBINARY); statement.setInt(2, pledgeId); statement.execute(); } catch (Exception e) { _log.error("", e); } finally { DbUtils.closeQuietly(con, statement); } }
From source file:br.bookmark.db.util.ResultSetUtils.java
/** * Map JDBC objects to Java equivalents. * Used by getBean() and getBeans()./* www.ja v a 2s .c o m*/ * <p> * Some types not supported. * Many not work with all drivers. * <p> * Makes binary conversions of BIGINT, DATE, DECIMAL, DOUBLE, FLOAT, INTEGER, * REAL, SMALLINT, TIME, TIMESTAMP, TINYINT. * Makes Sting conversions of CHAR, CLOB, VARCHAR, LONGVARCHAR, BLOB, LONGVARBINARY, * VARBINARY. * <p> * DECIMAL, INTEGER, SMALLINT, TIMESTAMP, CHAR, VARCHAR tested with MySQL and Poolman. * Others not guaranteed. * @param classeDestino * @throws NoSuchFieldException * @throws SecurityException */ private static void putEntry(Map properties, ResultSetMetaData metaData, ResultSet resultSet, int i, Class classeDestino) throws Exception { /* In a perfect universe, this would be enough properties.put( metaData.getColumnName(i), resultSet.getObject(i)); But only String, Timestamp, and Integer seem to get through that way. */ String columnName = metaData.getColumnName(i); // Testa se uma FK /*Field[] fields = classeDestino.getDeclaredFields(); for (int j = 0; j < fields.length; j++) { if (fields[j].getAnnotation(DBFK.class) != null) { properties.put(columnName, resultSet.getString(i)); } }*/ //System.out.println(i+"-"+metaData.getColumnType(i)); switch (metaData.getColumnType(i)) { // http://java.sun.com/j2se/1.3.0/docs/api/java/sql/Types.html case Types.BIGINT: properties.put(columnName, new Long(resultSet.getLong(i))); break; case Types.DATE: properties.put(columnName, resultSet.getDate(i)); break; case Types.DECIMAL: case Types.DOUBLE: properties.put(columnName, new Double(resultSet.getDouble(i))); break; case Types.FLOAT: properties.put(columnName, new Float(resultSet.getFloat(i))); break; case Types.INTEGER: int valor = 0; try { // Se o campo esta vazio d erro valor = resultSet.getInt(i); } catch (SQLException e) { } properties.put(columnName, new Integer(valor)); break; case Types.REAL: properties.put(columnName, new Double(resultSet.getString(i))); break; case Types.SMALLINT: properties.put(columnName, new Short(resultSet.getShort(i))); break; case Types.TIME: properties.put(columnName, resultSet.getTime(i)); break; case Types.TIMESTAMP: properties.put(columnName, resultSet.getTimestamp(i)); break; // :FIXME: Throws java.lang.ClassCastException: java.lang.Integer // :FIXME: with Poolman and MySQL unless use getString. case Types.TINYINT: properties.put(columnName, new Byte(resultSet.getString(i))); break; case Types.CHAR: case Types.CLOB: case Types.VARCHAR: case Types.LONGVARCHAR: // :FIXME: Handle binaries differently? case Types.BLOB: case Types.LONGVARBINARY: case Types.VARBINARY: properties.put(columnName, resultSet.getString(i)); break; /* :FIXME: Add handlers for ARRAY BINARY BIT DISTINCT JAVA_OBJECT NULL NUMERIC OTHER REF STRUCT */ // Otherwise, pass as *String property to be converted default: properties.put(columnName + "String", resultSet.getString(i)); break; } // end switch }
From source file:com.flexive.core.storage.GenericDivisionExporter.java
/** * Dump a generic table to XML/*www. j a v a2 s .c o m*/ * * @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.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 . java 2 s. c o m*/ b.append(AND); } } b.replace(b.length() - AND.length(), b.length(), ""); return b.toString(); }
From source file:org.waarp.common.database.data.AbstractDbData.java
/** * Set Value into PreparedStatement// www .j ava2s . co m * * @param ps * @param value * @param rank * >= 1 * @throws WaarpDatabaseSqlException */ static public void setTrueValue(PreparedStatement ps, DbValue value, int rank) throws WaarpDatabaseSqlException { try { switch (value.type) { case Types.VARCHAR: if (value.value == null) { ps.setNull(rank, Types.VARCHAR); break; } ps.setString(rank, (String) value.value); break; case Types.LONGVARCHAR: if (value.value == null) { ps.setNull(rank, Types.LONGVARCHAR); break; } ps.setString(rank, (String) value.value); break; case Types.BIT: if (value.value == null) { ps.setNull(rank, Types.BIT); break; } ps.setBoolean(rank, (Boolean) value.value); break; case Types.TINYINT: if (value.value == null) { ps.setNull(rank, Types.TINYINT); break; } ps.setByte(rank, (Byte) value.value); break; case Types.SMALLINT: if (value.value == null) { ps.setNull(rank, Types.SMALLINT); break; } ps.setShort(rank, (Short) value.value); break; case Types.INTEGER: if (value.value == null) { ps.setNull(rank, Types.INTEGER); break; } ps.setInt(rank, (Integer) value.value); break; case Types.BIGINT: if (value.value == null) { ps.setNull(rank, Types.BIGINT); break; } ps.setLong(rank, (Long) value.value); break; case Types.REAL: if (value.value == null) { ps.setNull(rank, Types.REAL); break; } ps.setFloat(rank, (Float) value.value); break; case Types.DOUBLE: if (value.value == null) { ps.setNull(rank, Types.DOUBLE); break; } ps.setDouble(rank, (Double) value.value); break; case Types.VARBINARY: if (value.value == null) { ps.setNull(rank, Types.VARBINARY); break; } ps.setBytes(rank, (byte[]) value.value); break; case Types.DATE: if (value.value == null) { ps.setNull(rank, Types.DATE); break; } ps.setDate(rank, (Date) value.value); break; case Types.TIMESTAMP: if (value.value == null) { ps.setNull(rank, Types.TIMESTAMP); break; } ps.setTimestamp(rank, (Timestamp) value.value); break; case Types.CLOB: if (value.value == null) { ps.setNull(rank, Types.CLOB); break; } ps.setClob(rank, (Reader) value.value); break; case Types.BLOB: if (value.value == null) { ps.setNull(rank, Types.BLOB); break; } ps.setBlob(rank, (InputStream) value.value); break; default: throw new WaarpDatabaseSqlException("Type not supported: " + value.type + " at " + rank); } } catch (ClassCastException e) { throw new WaarpDatabaseSqlException("Setting values casting error: " + value.type + " at " + rank, e); } catch (SQLException e) { DbSession.error(e); throw new WaarpDatabaseSqlException("Setting values in error: " + value.type + " at " + rank, e); } }
From source file:org.hibernate.dialect.Dialect.java
protected Dialect() { log.info("Using dialect: " + this); sqlFunctions.putAll(STANDARD_AGGREGATE_FUNCTIONS); // standard sql92 functions (can be overridden by subclasses) registerFunction("substring", new SQLFunctionTemplate(Hibernate.STRING, "substring(?1, ?2, ?3)")); registerFunction("locate", new SQLFunctionTemplate(Hibernate.INTEGER, "locate(?1, ?2, ?3)")); registerFunction("trim", new SQLFunctionTemplate(Hibernate.STRING, "trim(?1 ?2 ?3 ?4)")); registerFunction("length", new StandardSQLFunction("length", Hibernate.INTEGER)); registerFunction("bit_length", new StandardSQLFunction("bit_length", Hibernate.INTEGER)); registerFunction("coalesce", new StandardSQLFunction("coalesce")); registerFunction("nullif", new StandardSQLFunction("nullif")); registerFunction("abs", new StandardSQLFunction("abs")); registerFunction("mod", new StandardSQLFunction("mod", Hibernate.INTEGER)); registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE)); registerFunction("upper", new StandardSQLFunction("upper")); registerFunction("lower", new StandardSQLFunction("lower")); registerFunction("cast", new CastFunction()); registerFunction("extract", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(?1 ?2 ?3)")); //map second/minute/hour/day/month/year to ANSI extract(), override on subclasses registerFunction("second", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(second from ?1)")); registerFunction("minute", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(minute from ?1)")); registerFunction("hour", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(hour from ?1)")); registerFunction("day", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(day from ?1)")); registerFunction("month", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(month from ?1)")); registerFunction("year", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(year from ?1)")); registerFunction("str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as char)")); // register hibernate types for default use in scalar sqlquery type auto detection registerHibernateType(Types.BIGINT, Hibernate.BIG_INTEGER.getName()); registerHibernateType(Types.BINARY, Hibernate.BINARY.getName()); registerHibernateType(Types.BIT, Hibernate.BOOLEAN.getName()); registerHibernateType(Types.CHAR, Hibernate.CHARACTER.getName()); registerHibernateType(Types.DATE, Hibernate.DATE.getName()); registerHibernateType(Types.DOUBLE, Hibernate.DOUBLE.getName()); registerHibernateType(Types.FLOAT, Hibernate.FLOAT.getName()); registerHibernateType(Types.INTEGER, Hibernate.INTEGER.getName()); registerHibernateType(Types.SMALLINT, Hibernate.SHORT.getName()); registerHibernateType(Types.TINYINT, Hibernate.BYTE.getName()); registerHibernateType(Types.TIME, Hibernate.TIME.getName()); registerHibernateType(Types.TIMESTAMP, Hibernate.TIMESTAMP.getName()); registerHibernateType(Types.VARCHAR, Hibernate.STRING.getName()); registerHibernateType(Types.VARBINARY, Hibernate.BINARY.getName()); registerHibernateType(Types.NUMERIC, Hibernate.BIG_DECIMAL.getName()); registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName()); registerHibernateType(Types.BLOB, Hibernate.BLOB.getName()); registerHibernateType(Types.CLOB, Hibernate.CLOB.getName()); registerHibernateType(Types.REAL, Hibernate.FLOAT.getName()); }
From source file:org.apache.cayenne.unit.di.server.SchemaBuilder.java
private void dbEntitiesFilter(List<DbEntity> entities) { // filter various unsupported tests... // LOBs/*from w w w . j a v a2 s . c o m*/ boolean excludeLOB = !unitDbAdapter.supportsLobs(); boolean excludeBinPK = !unitDbAdapter.supportsBinaryPK(); if (excludeLOB || excludeBinPK) { List<DbEntity> filtered = new ArrayList<DbEntity>(); for (DbEntity ent : entities) { // check for LOB attributes if (excludeLOB) { if (Arrays.binarySearch(EXTRA_EXCLUDED_FOR_NO_LOB, ent.getName()) >= 0) { continue; } boolean hasLob = false; for (final DbAttribute attr : ent.getAttributes()) { if (attr.getType() == Types.BLOB || attr.getType() == Types.CLOB) { hasLob = true; break; } } if (hasLob) { continue; } } // check for BIN PK if (excludeBinPK) { boolean skip = false; for (final DbAttribute attr : ent.getAttributes()) { // check for BIN PK or FK to BIN Pk if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY || attr.getType() == Types.LONGVARBINARY) { if (attr.isPrimaryKey() || attr.isForeignKey()) { skip = true; break; } } } if (skip) { continue; } } filtered.add(ent); } entities = filtered; } }
From source file:org.cloudgraph.rdb.filter.RDBStatementExecutor.java
@Override public void execute(PlasmaType type, StringBuilder sql, Map<String, PropertyPair> values) { PreparedStatement statement = null; List<InputStream> streams = null; try {/*from ww w. j ava 2s .c o m*/ if (log.isDebugEnabled()) { log.debug("execute: " + sql.toString()); StringBuilder paramBuf = createParamDebug(values); log.debug("params: " + paramBuf.toString()); } statement = con.prepareStatement(sql.toString()); for (PropertyPair pair : values.values()) { PlasmaProperty valueProp = pair.getProp(); if (pair.getValueProp() != null) valueProp = pair.getValueProp(); int jdbcType = converter.toJDBCDataType(valueProp, pair.getValue()); Object jdbcValue = converter.toJDBCDataValue(valueProp, pair.getValue()); if (jdbcType != Types.BLOB && jdbcType != Types.VARBINARY) { statement.setObject(pair.getColumn(), jdbcValue, jdbcType); } else { byte[] bytes = (byte[]) jdbcValue; long len = bytes.length; ByteArrayInputStream is = new ByteArrayInputStream(bytes); statement.setBinaryStream(pair.getColumn(), is, len); if (streams == null) streams = new ArrayList<InputStream>(); streams.add(is); } if (pair.getOldValue() != null) { Object jdbcOldValue = converter.toJDBCDataValue(valueProp, pair.getOldValue()); if (jdbcType != Types.BLOB && jdbcType != Types.VARBINARY) { statement.setObject(pair.getOldValueColumn(), jdbcOldValue, jdbcType); } else { byte[] bytes = (byte[]) jdbcOldValue; long len = bytes.length; ByteArrayInputStream is = new ByteArrayInputStream(bytes); statement.setBinaryStream(pair.getOldValueColumn(), is, len); if (streams == null) streams = new ArrayList<InputStream>(); streams.add(is); } } } statement.executeUpdate(); } catch (Throwable t) { throw new DataAccessException(t); } finally { try { if (statement != null) statement.close(); } catch (SQLException e) { log.error(e.getMessage(), e); } if (streams != null) try { for (InputStream stream : streams) stream.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } }
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 www. j a v a 2s . c om*/ return "UNDEFINED";// } }