List of usage examples for java.sql Blob getBytes
byte[] getBytes(long pos, int length) throws SQLException;
From source file:org.pentaho.di.jdbc.Support.java
/** * Embed the data object as a string literal in the buffer supplied. * * @param buf The buffer in which the data will be embeded. * @param value The data object.//from w w w.ja v a 2 s . c o m * @param isUnicode Set to <code>true</code> if Unicode strings should be used, else <code>false</code>. * @param connection The {@link ConnectionJDBC3} object. */ static void embedData(StringBuffer buf, Object value, boolean isUnicode, ConnectionJDBC3 connection) throws SQLException { buf.append(' '); if (value == null) { buf.append("NULL "); return; } if (value instanceof Blob) { Blob blob = (Blob) value; value = blob.getBytes(1, (int) blob.length()); } else if (value instanceof Clob) { Clob clob = (Clob) value; value = clob.getSubString(1, (int) clob.length()); } if (value instanceof DateTime) { buf.append('\''); buf.append(value); buf.append('\''); } else if (value instanceof byte[]) { byte[] bytes = (byte[]) value; int len = bytes.length; if (len >= 0) { buf.append('0').append('x'); if (len == 0) { // Zero length binary values are not allowed buf.append('0').append('0'); } else { for (int i = 0; i < len; i++) { int b1 = bytes[i] & 0xFF; buf.append(hex[b1 >> 4]); buf.append(hex[b1 & 0x0F]); } } } } else if (value instanceof String) { String tmp = (String) value; int len = tmp.length(); if (isUnicode) { buf.append('N'); } buf.append('\''); for (int i = 0; i < len; i++) { char c = tmp.charAt(i); if (c == '\'') { buf.append('\''); } buf.append(c); } buf.append('\''); } else if (value instanceof java.sql.Date) { DateTime dt = new DateTime((java.sql.Date) value); buf.append('\''); buf.append(dt); buf.append('\''); } else if (value instanceof java.sql.Time) { DateTime dt = new DateTime((java.sql.Time) value); buf.append('\''); buf.append(dt); buf.append('\''); } else if (value instanceof java.sql.Timestamp) { DateTime dt = new DateTime((java.sql.Timestamp) value); buf.append('\''); buf.append(dt); buf.append('\''); } else if (value instanceof Boolean) { buf.append(((Boolean) value).booleanValue() ? '1' : '0'); } else if (value instanceof BigDecimal) { // // Ensure large decimal number does not overflow the // maximum precision of the server. // Main problem is with small numbers e.g. BigDecimal(1.0).toString() = // 0.1000000000000000055511151231.... // String tmp = value.toString(); int maxlen = connection.getMaxPrecision(); if (tmp.charAt(0) == '-') { maxlen++; } if (tmp.indexOf('.') >= 0) { maxlen++; } if (tmp.length() > maxlen) { buf.append(tmp.substring(0, maxlen)); } else { buf.append(tmp); } } else { buf.append(value.toString()); } buf.append(' '); }
From source file:org.apache.lucene.store.jdbc.index.FetchOnOpenJdbcIndexInput.java
public void configure(final String name, final JdbcDirectory jdbcDirectory, JdbcFileEntrySettings settings) throws IOException { jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(), new PreparedStatementCallback<Object>() { @Override/*from ww w .java 2 s . c o m*/ public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, name); try (ResultSet rs = ps.executeQuery()) { if (!rs.next()) { throw new JdbcStoreException( "No entry for [" + name + "] table " + jdbcDirectory.getTable()); } length = rs.getInt(3); Blob blob = rs.getBlob(2); data = blob.getBytes(1, (int) length); if (data.length != length) { throw new IOException("read past EOF"); } } catch (JdbcStoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }); }
From source file:org.beangle.ems.avatar.service.DataBaseAvatarBase.java
protected byte[] getBytes(String name) { SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql, name); Blob obj = null; if (rowSet.next()) { obj = (Blob) rowSet.getObject(1); try {// ww w . j a va 2 s. com return obj.getBytes(1L, (int) obj.length()); } catch (SQLException e) { e.printStackTrace(); return null; } } else { return null; } }
From source file:org.jboss.dashboard.database.hibernate.BinaryBlobType.java
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { if (log.isDebugEnabled()) log.debug("Getting value with names " + Arrays.asList(names)); Object o = rs.getObject(names[0]); if (o == null) { return null; } else if (o instanceof Blob) { final Blob blob = (Blob) o; if (blob == null) return null; return blob.getBytes(1, (int) blob.length()); } else if (o instanceof byte[]) { return o; } else {/*from w w w . j a v a2 s . c o m*/ throw new IllegalArgumentException( "Unexpected value read. Must be Blob or byte[], but it is " + o.getClass()); } }
From source file:org.jboss.dashboard.database.hibernate.StringBlobType.java
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { if (log.isDebugEnabled()) log.debug("Getting value with names " + Arrays.asList(names)); Object o = rs.getObject(names[0]); if (o == null) { return null; } else if (o instanceof Blob) { final Blob blob = (Blob) o; try {/* w w w. j a v a2s. c om*/ byte bytes[] = blob.getBytes(1, (int) blob.length()); if (bytes == null) return null; return new String(bytes, STRING_ENCODING); } catch (UnsupportedEncodingException e) { log.error("Error:", e); return new String(blob.getBytes(1, (int) blob.length())); } } else if (o instanceof String) { return o; } else if (o instanceof byte[]) { try { return new String((byte[]) o, STRING_ENCODING); } catch (UnsupportedEncodingException e) { log.error("Error: ", e); return null; } } else if (o instanceof Clob) { //added for H2 support final Clob clob = (Clob) o; return clob.getSubString(1, (int) clob.length()); } else { throw new IllegalArgumentException( "Unexpected value read. Must be Blob or String, but it is " + o.getClass()); } }
From source file:com.mirth.connect.server.mule.transformers.ResultMapToXML.java
public Object doTransform(Object source) throws TransformerException { if (source instanceof Map) { Map data = (Map) source; try {/*from w ww. j a va2 s . com*/ Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = document.createElement("result"); document.appendChild(root); for (Iterator iter = data.keySet().iterator(); iter.hasNext();) { String key = (String) iter.next(); Element child = document.createElement(key); String value = new String(); Object objectValue = data.get(key); if (objectValue != null) { if (objectValue instanceof byte[]) { value = new String((byte[]) objectValue); } else if (objectValue instanceof java.sql.Clob) { // convert it to a string java.sql.Clob clobValue = (java.sql.Clob) objectValue; Reader reader = clobValue.getCharacterStream(); if (reader == null) { value = ""; } StringBuffer sb = new StringBuffer(); try { char[] charbuf = new char[(int) clobValue.length()]; for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) { sb.append(charbuf, 0, i); } } catch (IOException e) { logger.error("Error reading clob value.\n" + ExceptionUtils.getStackTrace(e)); } value = sb.toString(); } else if (objectValue instanceof java.sql.Blob) { try { java.sql.Blob blobValue = (java.sql.Blob) objectValue; value = new String(blobValue.getBytes(1, (int) blobValue.length())); } catch (Exception ex) { logger.error("Error reading blob value.\n" + ExceptionUtils.getStackTrace(ex)); } } else { value = objectValue.toString(); } } child.appendChild(document.createTextNode(value)); root.appendChild(child); } DocumentSerializer docSerializer = new DocumentSerializer(); return docSerializer.toXML(document); } catch (Exception e) { throw new TransformerException( org.mule.config.i18n.Message.createStaticMessage("Failed to parse result map"), this); } } else if (source instanceof String) { return source.toString(); } else { throw new TransformerException( org.mule.config.i18n.Message.createStaticMessage("Unregistered result type"), this); } }
From source file:org.pentaho.platform.repository.hibernate.usertypes.BlobtoByteArrayUserType.java
public Object nullSafeGet(final ResultSet arg0, final String[] arg1, final Object arg2) throws HibernateException, SQLException { Blob blob = arg0.getBlob(arg1[0]); if (blob != null) { byte[] bytes = blob.getBytes(1, (int) blob.length()); return bytes; }//from w w w .j a v a2 s. co m return null; }
From source file:com.trackplus.ddl.GenericStringValueConverter.java
protected String extractColumnValue(ResultSet resultSet, int columnIdx, int jdbcType) throws SQLException, DDLException { String value = resultSet.getString(columnIdx); if (value != null) { switch (jdbcType) { case Types.NUMERIC: case Types.DECIMAL: break; case Types.BIT: case Types.BOOLEAN: case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: case Types.REAL: case Types.FLOAT: case Types.DOUBLE: { break; }/*from ww w . j ava 2s .c o m*/ case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.BINARY: case Types.VARBINARY: case Types.TIME: case Types.CLOB: case Types.ARRAY: case Types.REF: { value = "'" + value.replaceAll("'", "''") + "'"; break; } case Types.DATE: case Types.TIMESTAMP: { Date d = resultSet.getDate(columnIdx); Calendar cal = Calendar.getInstance(); cal.setTime(d); int year = cal.get(Calendar.YEAR); if (year < 1900) { throw new DDLException("Invalid date:" + d); } else { value = "'" + value + "'"; } break; } case Types.BLOB: case Types.LONGVARBINARY: { Blob blobValue = resultSet.getBlob(columnIdx); String str = new String(Base64.encodeBase64(blobValue.getBytes(1l, (int) blobValue.length()))); value = "'" + str + "'"; break; } default: break; } } return value; }
From source file:org.sakaiproject.mailarchive.impl.conversion.ExtractXMLToColumns.java
public Object getValidateSource(String id, ResultSet rs) throws SQLException { ResultSetMetaData metadata = rs.getMetaData(); byte[] rv = null; switch (metadata.getColumnType(1)) { case Types.BLOB: Blob blob = rs.getBlob(1); if (blob != null) { rv = blob.getBytes(1L, (int) blob.length()); } else {/*from ww w . j a v a2s . c o m*/ System.out.println("getValidateSource(" + id + ") blob == null"); } break; case Types.CLOB: Clob clob = rs.getClob(1); if (clob != null) { rv = clob.getSubString(1L, (int) clob.length()).getBytes(); } break; case Types.CHAR: case Types.LONGVARCHAR: case Types.VARCHAR: rv = rs.getString(1).getBytes(); break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: rv = rs.getBytes(1); break; } // System.out.println("getValidateSource(" + id + ") \n" + rv + "\n"); return rv; }
From source file:org.sakaiproject.mailarchive.impl.conversion.ExtractXMLToColumns.java
public Object getSource(String id, ResultSet rs) throws SQLException { ResultSetMetaData metadata = rs.getMetaData(); String rv = null;/*from w ww . j a v a 2s . com*/ switch (metadata.getColumnType(1)) { case Types.BLOB: Blob blob = rs.getBlob(1); if (blob != null) { rv = new String(blob.getBytes(1L, (int) blob.length())); } break; case Types.CLOB: Clob clob = rs.getClob(1); if (clob != null) { rv = clob.getSubString(1L, (int) clob.length()); } break; case Types.CHAR: case Types.LONGVARCHAR: case Types.VARCHAR: case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: byte[] bytes = rs.getBytes(1); if (bytes != null) { rv = new String(bytes); } break; } // System.out.println("getSource(" + id + ") \n" + rv + "\n"); return rv; }