List of usage examples for java.sql ResultSet getBlob
Blob getBlob(String columnLabel) throws SQLException;
ResultSet
object as a Blob
object in the Java programming language. From source file:org.apache.gora.sql.store.SqlStore.java
protected byte[] getBytes(ResultSet resultSet, int columnIndex, Schema schema, Column column) throws SQLException, IOException { switch (column.getJdbcType()) { case BLOB://w w w .jav a2 s . c o m Blob blob = resultSet.getBlob(columnIndex); return IOUtils.readFully(blob.getBinaryStream()); case BINARY: case VARBINARY: return resultSet.getBytes(columnIndex); case LONGVARBINARY: return IOUtils.readFully(resultSet.getBinaryStream(columnIndex)); } return null; }
From source file:org.ebayopensource.winder.quartz.QuartzSchedulerManager.java
private Object getObjectFromBlob(ResultSet rs, String colName) throws ClassNotFoundException, IOException, SQLException { Object obj = null;//w ww . j ava2s. c o m Blob blobLocator = rs.getBlob(colName); if (blobLocator != null && blobLocator.length() != 0) { InputStream binaryInput = blobLocator.getBinaryStream(); if (null != binaryInput) { if (binaryInput instanceof ByteArrayInputStream && ((ByteArrayInputStream) binaryInput).available() == 0) { //do nothing } else { ObjectInputStream in = new ObjectInputStream(binaryInput); try { obj = in.readObject(); } finally { in.close(); } } } } return obj; }
From source file:org.kawanfw.test.api.client.InsertAndUpdateBlobTest.java
/** * Test that the blob was were correctly inserted * // w ww.ja v a2 s .c o m * @param connection */ public void selectBlobTestAlternateSyntax(Connection connection, String originalFileName, String shaHexa) throws Exception { int customer_id; int item_id; String description; BigDecimal cost_price; Date date_placed; Timestamp date_shipped; Blob blob; boolean is_delivered; int quantity; String sql = "select * from orderlog where customer_id >= ? and item_id >= ? "; PreparedStatement prepStatement = connection.prepareStatement(sql); int i = 1; prepStatement.setInt(i++, 1); prepStatement.setInt(i++, 1); ResultSet rs = prepStatement.executeQuery(); MessageDisplayer.display(""); InputStream in = null; OutputStream out = null; SqlUtil sqlUtil = new SqlUtil(connection); while (rs.next()) { customer_id = rs.getInt("customer_id"); item_id = rs.getInt("item_id"); description = rs.getString("description"); cost_price = rs.getBigDecimal("cost_price"); date_placed = rs.getDate("date_placed"); date_shipped = rs.getTimestamp("date_shipped"); blob = rs.getBlob("jpeg_image"); if (sqlUtil.isIngres()) { is_delivered = (rs.getInt("is_delivered") == 1) ? true : false; } else { is_delivered = rs.getBoolean("is_delivered"); } quantity = rs.getInt("quantity"); i = 1; customer_id = rs.getInt(i++); item_id = rs.getInt(i++); description = rs.getString(i++); cost_price = rs.getBigDecimal(i++); date_placed = rs.getDate(i++); date_shipped = rs.getTimestamp(i++); File originalBlobFile = SqlTestParms.getFileFromUserHome(originalFileName); // String extension = "." // + StringUtils.substringAfterLast( // originalBlobFile.toString(), "."); File file = createTempFile(originalBlobFile.toString()); try { in = blob.getBinaryStream(); if (in != null) { out = new BufferedOutputStream(new FileOutputStream(file)); IOUtils.copy(in, out); } else { MessageDisplayer.display("jpeg_image column is null!"); } } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); try { blob.free(); } catch (Throwable e) { MessageDisplayer.display("blob.free() not done: " + e.toString()); } } i++; if (sqlUtil.isIngres()) { is_delivered = (rs.getInt(i++) == 1) ? true : false; } else { is_delivered = rs.getBoolean(i++); } quantity = rs.getInt(i++); MessageDisplayer.display(""); MessageDisplayer.display("customer_id : " + customer_id); MessageDisplayer.display("item_id : " + item_id); MessageDisplayer.display("description : " + description); MessageDisplayer.display("cost_price : " + cost_price); MessageDisplayer.display("date_placed : " + date_placed); MessageDisplayer.display("date_shipped: " + date_shipped); MessageDisplayer.display("jpeg_image : " + "content stored in file: " + file); MessageDisplayer.display("is_delivered: " + is_delivered); MessageDisplayer.display("quantity : " + quantity); // Compute the hash of the file Sha1Util sha1 = new Sha1Util(); String shaHexaNew = sha1.getHexFileHash(file); Assert.assertEquals(shaHexa, shaHexaNew); file.delete(); MessageDisplayer.display(""); MessageDisplayer.display("Ok, SHA-1 value of read file " + file + " is same as inserted file " + SqlTestParms.getFileFromUserHome(originalFileName)); } prepStatement.close(); rs.close(); MessageDisplayer.display("Select done!"); }
From source file:org.kawanfw.test.api.client.InsertAndUpdateBlobTestPsqlOID.java
/** * Test that the blob was were correctly inserted * //from w w w . ja v a 2 s.co m * @param connection */ public void selectBlobTestAlternateSyntax(Connection connection, String originalFileName, String shaHexa) throws Exception { int customer_id; int item_id; String description; BigDecimal cost_price; Date date_placed; Timestamp date_shipped; Blob blob; boolean is_delivered; int quantity; String sql = "select * from orderlog_2 where customer_id >= ? and item_id >= ? "; PreparedStatement prepStatement = connection.prepareStatement(sql); int i = 1; prepStatement.setInt(i++, 1); prepStatement.setInt(i++, 1); ResultSet rs = prepStatement.executeQuery(); MessageDisplayer.display(""); InputStream in = null; OutputStream out = null; SqlUtil sqlUtil = new SqlUtil(connection); while (rs.next()) { customer_id = rs.getInt("customer_id"); item_id = rs.getInt("item_id"); description = rs.getString("description"); cost_price = rs.getBigDecimal("cost_price"); date_placed = rs.getDate("date_placed"); date_shipped = rs.getTimestamp("date_shipped"); blob = rs.getBlob("jpeg_image"); if (sqlUtil.isIngres()) { is_delivered = (rs.getInt("is_delivered") == 1) ? true : false; } else { is_delivered = rs.getBoolean("is_delivered"); } quantity = rs.getInt("quantity"); i = 1; customer_id = rs.getInt(i++); item_id = rs.getInt(i++); description = rs.getString(i++); cost_price = rs.getBigDecimal(i++); date_placed = rs.getDate(i++); date_shipped = rs.getTimestamp(i++); File originalBlobFile = SqlTestParms.getFileFromUserHome(originalFileName); // String extension = "." // + StringUtils.substringAfterLast( // originalBlobFile.toString(), "."); File file = createTempFile(originalBlobFile.toString()); try { in = blob.getBinaryStream(); if (in != null) { out = new BufferedOutputStream(new FileOutputStream(file)); IOUtils.copy(in, out); } else { MessageDisplayer.display("jpeg_image column is null!"); } } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); try { blob.free(); } catch (Throwable e) { MessageDisplayer.display("blob.free() not done: " + e.toString()); } } i++; if (sqlUtil.isIngres()) { is_delivered = (rs.getInt(i++) == 1) ? true : false; } else { is_delivered = rs.getBoolean(i++); } quantity = rs.getInt(i++); MessageDisplayer.display(""); MessageDisplayer.display("customer_id : " + customer_id); MessageDisplayer.display("item_id : " + item_id); MessageDisplayer.display("description : " + description); MessageDisplayer.display("cost_price : " + cost_price); MessageDisplayer.display("date_placed : " + date_placed); MessageDisplayer.display("date_shipped: " + date_shipped); MessageDisplayer.display("jpeg_image : " + "content stored in file: " + file); MessageDisplayer.display("is_delivered: " + is_delivered); MessageDisplayer.display("quantity : " + quantity); // Compute the hash of the file Sha1Util sha1 = new Sha1Util(); String shaHexaNew = sha1.getHexFileHash(file); Assert.assertEquals(shaHexa, shaHexaNew); file.delete(); MessageDisplayer.display(""); MessageDisplayer.display("Ok, SHA-1 value of read file " + file + " is same as inserted file " + SqlTestParms.getFileFromUserHome(originalFileName)); } prepStatement.close(); rs.close(); MessageDisplayer.display("Select done!"); }
From source file:eionet.cr.dao.virtuoso.VirtuosoStagingDatabaseDAO.java
@Override public String getExportLog(int exportId) throws DAOException { ArrayList<Object> params = new ArrayList<Object>(); params.add(Integer.valueOf(exportId)); ResultSet rs = null; Statement stmt = null;/* www .j a va 2 s .co m*/ Connection conn = null; try { conn = getSQLConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(GET_EXPORT_LOG_SQL.replace("?", String.valueOf(exportId))); if (rs.next()) { Blob blob = rs.getBlob(1); if (blob != null) { try { return blob.length() == 0 ? "" : IOUtils.toString(blob.getBinaryStream()); } catch (IOException e) { LOGGER.warn("Could not retreive log of the RDF export with id = " + exportId + ": " + e); return null; } } else { return null; } } else { return null; } } catch (SQLException e) { throw new DAOException(e.getMessage(), e); } finally { SQLUtil.close(rs); SQLUtil.close(stmt); SQLUtil.close(conn); } }
From source file:eionet.cr.dao.virtuoso.VirtuosoStagingDatabaseDAO.java
@Override public String getImportLog(int databaseId) throws DAOException { ArrayList<Object> params = new ArrayList<Object>(); params.add(Integer.valueOf(databaseId)); ResultSet rs = null; Statement stmt = null;//from w w w . ja va2s . co m Connection conn = null; try { conn = getSQLConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(GET_IMPORT_LOG_SQL.replace("?", String.valueOf(databaseId))); if (rs.next()) { Blob blob = rs.getBlob(1); if (blob != null) { try { return blob.length() == 0 ? "" : IOUtils.toString(blob.getBinaryStream()); } catch (IOException e) { LOGGER.warn("Could not retreive import log of database #" + databaseId + ": " + e); return null; } } else { return null; } } else { return null; } } catch (SQLException e) { throw new DAOException(e.getMessage(), e); } finally { SQLUtil.close(rs); SQLUtil.close(stmt); SQLUtil.close(conn); } }
From source file:org.apache.syncope.core.util.ImportExport.java
private String getValues(final ResultSet rs, final String columnName, final Integer columnType) throws SQLException { String res = null;//from ww w . j ava 2s . c o m try { switch (columnType) { case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: final InputStream is = rs.getBinaryStream(columnName); if (is != null) { res = new String(Hex.encode(IOUtils.toByteArray(is))); } break; case Types.BLOB: final Blob blob = rs.getBlob(columnName); if (blob != null) { res = new String(Hex.encode(IOUtils.toByteArray(blob.getBinaryStream()))); } break; case Types.BIT: case Types.BOOLEAN: if (rs.getBoolean(columnName)) { res = "1"; } else { res = "0"; } break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: final Timestamp timestamp = rs.getTimestamp(columnName); if (timestamp != null) { res = DATE_FORMAT.get().format(new Date(timestamp.getTime())); } break; default: res = rs.getString(columnName); } } catch (IOException e) { LOG.error("Error retrieving hexadecimal string", e); } return res; }
From source file:com.hexin.core.dao.BaseDaoSupport.java
@Override public <T> List<T> findListWithBlob(String sql, Class<T> dtoClass, Object... args) throws SQLException, InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchFieldException, IOException { long startTime = System.currentTimeMillis(); long endTime; long durTime; debugSql(sql, args);/*from w w w. jav a 2 s.c om*/ PreparedStatement ps = jdbcTemplate.getDataSource().getConnection().prepareStatement(sql); setPreparedStatementParameter(ps, args); List<T> list = new ArrayList<T>(); ResultSet rs = ps.executeQuery(); while (rs.next()) { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); T obj = dtoClass.newInstance(); for (int i = 1; i <= colCount; i++) { String colName = rsmd.getColumnLabel(i); // ?? String colTypeName = rsmd.getColumnTypeName(i); String beanFiledName = IcpObjectUtil.underlineToCamel(colName); if ("blob".equalsIgnoreCase(colTypeName)) { InjectValueUtil.setFieldValue(obj, beanFiledName, rs.getBlob(i)); } else { InjectValueUtil.setFieldValue(obj, beanFiledName, rs.getObject(i)); } } list.add(obj); } endTime = System.currentTimeMillis(); durTime = endTime - startTime; logger.debug("This jdbc operation costs time: " + durTime); return list; }
From source file:info.raack.appliancelabeler.data.JDBCDatabase.java
public Map<String, Map<String, OAuthConsumerToken>> getOAuthTokensForAllUsers() { try {/* w w w . j a v a2 s. c om*/ List<UserOAuthTokenCarrier> carriers = jdbcTemplate.query(queryForAllUserOAuthTokens, new RowMapper<UserOAuthTokenCarrier>() { public UserOAuthTokenCarrier mapRow(ResultSet rs, int arg1) throws SQLException { UserOAuthTokenCarrier token = new UserOAuthTokenCarrier(); token.userId = rs.getString("user_id"); Blob blob = rs.getBlob("spring_oauth_serialized_token_map"); Object tokenObject = SerializationUtils.deserialize(blob.getBinaryStream()); Map<String, OAuthConsumerToken> tokens = (Map<String, OAuthConsumerToken>) tokenObject; token.tokens = tokens; return token; } }); Map<String, Map<String, OAuthConsumerToken>> tokens = new HashMap<String, Map<String, OAuthConsumerToken>>(); for (UserOAuthTokenCarrier carrier : carriers) { tokens.put(carrier.userId, carrier.tokens); } return tokens; } catch (Exception e) { throw new RuntimeException("Error while getting user tokens", e); } }
From source file:org.kawanfw.sql.servlet.sql.ResultSetWriter.java
/** * the binary content is dumped in a server file that will be available for * the client the name of the file will be stored in the output stream ; * /* w w w. j a va2 s . co m*/ * @param resultSet * the result set in progress to send back to the client side * @param columnIndex * the column index * @param columnType * the column type * @param columnName * the column name * @param columnTable * the table name of the column * @return the formated binary column * * @throws SQLException */ private String formatBinaryColumn(ResultSet resultSet, int columnIndex, int columnType, String columnName, String columnTable) throws SQLException, IOException { String columnValueStr; FileNameFromBlobBuilder fileNameFromBlobBuilder = new FileNameFromBlobBuilder(sqlOrder, columnIndex, false); String fileName = fileNameFromBlobBuilder.getFileName(); // Maybe null, we want to keep the info InputStream in = null; if (isTerradata) { in = resultSet.getBlob(columnIndex).getBinaryStream(); } // For PostgreSQL columns OID columns have the BIGINT type else if (isPostgreSQL && columnType == Types.BIGINT) { in = PostgreSqlUtil.getPostgreSqlnputStream(resultSet, columnIndex); } else { in = resultSet.getBinaryStream(columnIndex); } // BufferedInputStream bufferedIn = new BufferedInputStream(in); if (fileConfigurator == null) // Servlet Awake FILE not // configured. { columnValueStr = TransportConverter.KAWANFW_BYTES_STREAM_FILE + TransportConverter.KAWANFW_STREAM_FAILURE; return columnValueStr; } OutputStream outStream = null; String hostFileName = null; try { hostFileName = HttpConfigurationUtil.addRootPath(fileConfigurator, username, fileName); outStream = new BufferedOutputStream(new FileOutputStream(hostFileName)); debug("formatBinaryColumn:outStream: " + hostFileName); if (in == null) { debug("formatBinaryColumn: in == null"); // DO NOTHING: just closing will create an empty file outStream.write(TransportConverter.KAWANFW_STREAM_NULL.getBytes()); } else { IOUtils.copy(in, outStream); } } catch (IOException e) { throw new SQLException(e); } finally { // IOUtils.closeQuietly(in); NOT DONE. Why? IOUtils.closeQuietly(outStream); } // The column value is a file name with a tag for identification columnValueStr = TransportConverter.KAWANFW_BYTES_STREAM_FILE + fileName; return columnValueStr; }