Example usage for java.sql Types BINARY

List of usage examples for java.sql Types BINARY

Introduction

In this page you can find the example usage for java.sql Types BINARY.

Prototype

int BINARY

To view the source code for java.sql Types BINARY.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type BINARY.

Usage

From source file:org.jumpmind.symmetric.db.derby.DerbyFunctions.java

public static String blobToString(String columnName, String tableName, String whereClause) throws SQLException {
    String str = null;/*from www .j  a  v a 2  s.  co m*/
    if (StringUtils.isNotBlank(whereClause)) {
        Connection conn = DriverManager.getConnection(CURRENT_CONNECTION_URL);
        String sql = "select " + columnName + " from " + tableName + " where " + whereClause;
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            byte[] bytes = null;
            int type = rs.getMetaData().getColumnType(1);
            if (type == Types.BINARY || type == Types.VARBINARY || type == Types.LONGVARBINARY) {
                bytes = rs.getBytes(1);
            } else {
                Blob blob = rs.getBlob(1);
                if (blob != null) {
                    bytes = blob.getBytes(1, MAX_BINARY_LENGTH);
                }
            }
            if (bytes != null) {
                str = new String(Base64.encodeBase64(bytes));
            }
        }
        ps.close();
        conn.close();
    }
    return str == null ? "" : "\"" + str + "\"";
}

From source file:org.apache.ddlutils.PlatformInfo.java

/**
 * Creates a new platform info object.//from   ww w.  j  a v  a 2s. c  o  m
 */
public PlatformInfo() {
    _typesWithNullDefault.add(new Integer(Types.CHAR));
    _typesWithNullDefault.add(new Integer(Types.VARCHAR));
    _typesWithNullDefault.add(new Integer(Types.LONGVARCHAR));
    _typesWithNullDefault.add(new Integer(Types.CLOB));
    _typesWithNullDefault.add(new Integer(Types.BINARY));
    _typesWithNullDefault.add(new Integer(Types.VARBINARY));
    _typesWithNullDefault.add(new Integer(Types.LONGVARBINARY));
    _typesWithNullDefault.add(new Integer(Types.BLOB));

    _typesWithSize.add(new Integer(Types.CHAR));
    _typesWithSize.add(new Integer(Types.VARCHAR));
    _typesWithSize.add(new Integer(Types.BINARY));
    _typesWithSize.add(new Integer(Types.VARBINARY));

    _typesWithPrecisionAndScale.add(new Integer(Types.DECIMAL));
    _typesWithPrecisionAndScale.add(new Integer(Types.NUMERIC));

    _supportedOnUpdateActions.addAll(CascadeActionEnum.getEnumList());
    _supportedOnDeleteActions.addAll(CascadeActionEnum.getEnumList());
}

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:/*  www  .ja v  a  2  s  . co  m*/
        return DataType.UNKNOWN;
    }
}

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Get value from given ResultSet at given index with given SQL type.
 * /*from   ww w  . ja  va 2  s .com*/
 * @param rs The ResultSet to get the value from.
 * @param index The index of the value in the ResultSet.
 * @param sqlType The SQL type of the value.
 * @return The value.
 * @throws SQLException If a database access error occurs.
 */
public static Object getValue(final ResultSet rs, final int index, final int sqlType) throws SQLException {
    switch (sqlType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        return rs.getString(index);
    case Types.DECIMAL:
    case Types.NUMERIC:
        return rs.getBigDecimal(index);
    case Types.INTEGER:
        int intVal = rs.getInt(index);
        return (rs.wasNull() ? null : new Integer(intVal));
    case Types.TIME:
        return rs.getTime(index, getCalendar());
    case Types.DATE:
        return rs.getDate(index);
    case Types.TIMESTAMP:
        return rs.getTimestamp(index, getCalendar());
    case Types.FLOAT:
    case Types.DOUBLE:
        double doubleVal = rs.getDouble(index);
        return (rs.wasNull() ? null : new Double(doubleVal));
    case Types.REAL:
        float floatVal = rs.getFloat(index);
        return (rs.wasNull() ? null : new Float(floatVal));
    case Types.SMALLINT:
        short shortVal = rs.getShort(index);
        return (rs.wasNull() ? null : new Short(shortVal));
    case Types.TINYINT:
        byte byteVal = rs.getByte(index);
        return (rs.wasNull() ? null : new Byte(byteVal));
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
    case Types.BINARY:
        return rs.getBytes(index);
    case Types.BLOB:
        Blob blob = rs.getBlob(index);
        return (blob == null ? null : blob.getBinaryStream());
    case Types.CLOB:
        return rs.getClob(index);
    case Types.BIGINT:
        long longVal = rs.getLong(index);
        return (rs.wasNull() ? null : new Long(longVal));
    case Types.BIT:
        boolean boolVal = rs.getBoolean(index);
        return (rs.wasNull() ? null : new Boolean(boolVal));
    default:
        Object value = rs.getObject(index);
        return (rs.wasNull() ? null : value);
    }
}

From source file:org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform.java

/**
 * {@inheritDoc}//w  w  w .  j  a v  a  2 s.  c o 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:com.flexive.core.storage.GenericDivisionExporter.java

/**
 * Dump a generic table to XML/*from  www  .  ja v  a  2 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.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.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;
            }//ww w  . j  a v a  2  s .c om
            b.append(AND);
        }
    }
    b.replace(b.length() - AND.length(), b.length(), "");
    return b.toString();
}

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://  www .  j av a 2 s  . co m
        return "UNDEFINED";//
    }
}

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 .ja  v  a  2  s. co 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;
    }
}