/** * Copyright (C) 2006 - present David Bulmore * All Rights Reserved. * * This file is part of Easy Java Persistence. * * EJP is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the accompanying license * for more details. * * You should have received a copy of the license along with EJP; if not, * go to http://www.EasierJava.com and download the latest version. */ package ejp.examples; import ejp.Database; import ejp.DatabaseException; import ejp.DatabaseManager; import ejp.PersistentClassManager; import ejp.interfaces.BinaryStream; import ejp.interfaces.BinaryStreamAdapter; import java.io.ByteArrayInputStream; import java.io.IOException; import java.sql.Blob; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import javax.sql.rowset.serial.SerialBlob; import javax.sql.rowset.serial.SerialException; /** MySQL * CREATE TABLE images * ( * image_id int(11) NOT NULL AUTO_INCREMENT, * image_data blob, * PRIMARY KEY (image_id) * ); * * HSQL * CREATE MEMORY TABLE IMAGES * ( * IMAGE_ID INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY, * IMAGE_DATA BLOB * ); * * Dealing with blobs is very database dependent. Most databases support * blobs, but their JDBC implementations widely differ. The database * table definition is usually along the lines of blob (tinyblob, mediumblob, * longblob), or some form of binary definition (binary, varbinary, longvarbinary). * It doesn't matter how you define it or how your database defines it. * * As with the definition of blobs, there are many different JDBC implementations * for handling blobs. Some drivers allow strings and byte arrays to be used. * Some allow Input/Output streams and/or readers/writers. And some drivers allow * the use of Blob, Clob, and NClob. Again it doesn't matter, it is either going * to work with your database and/or JDBC driver or exceptions will be thrown. * * Any way you work it EJP supports some version that will work with your database. */ public class Blobs { /** * Using Blob, Clob, or NClob */ public static class Image { int imageId; Blob imageData; public Image() {} public Image(Blob blob) { imageData = blob; } public Integer getImageId() { return imageId; } public void setImageId(Integer id) { imageId = id; } public Blob getImageData() { return imageData; } public void setImageData(Blob blob) { imageData = blob; } } /** * using BinaryStream, AsciiStream or CharacterStream */ public static class Image2 { int imageId; BinaryStream imageData; public Image2() {} public Image2(BinaryStream blob) { imageData = blob; } public Integer getImageId() { return imageId; } public void setImageId(int id) { imageId = id; } public BinaryStream getImageData() { return imageData; } public void setImageData(BinaryStream blob) { imageData = blob; } } /** * You can use SerialBlob with many databases and JDBC drivers. */ static void blobs1(DatabaseManager dbm) throws SerialException, DatabaseException, SQLException { dbm.saveObject(new Image(new SerialBlob("This is where you would store your data".getBytes()))); } /** * You can use java.sql.Connection to retrieve a Blob, Clob, NClob */ static void blobs2(DatabaseManager dbm) throws DatabaseException, SQLException { Database db = dbm.getDatabase(); try { Blob blob = db.getConnection().createBlob(); blob.setBytes(1, "You can also use the blob interface returned by java.sql.Connection to manage your data".getBytes()); db.saveObject(new Image(blob)); } finally { db.close(); } } /* * And you can use BinaryStream, AsciiStream, CharacterStream */ static void blobs3(DatabaseManager dbm) throws DatabaseException { String str = "Yet another form of handling binary large data"; dbm.saveObject(new Image2(new BinaryStreamAdapter(str.length(), new ByteArrayInputStream(str.getBytes())))); } /* * Reading blobs from your database is also simple. */ static void readingBlobs(DatabaseManager dbm) throws SQLException, IOException, DatabaseException { /* Dealing with Blob */ Collection images = dbm.loadObjects(new ArrayList(), Image.class); for (Image image : images) System.out.println(image.getImageId() + ", " + new String(image.imageData.getBytes(1L, (int)image.imageData.length()))); /* Dealing with BinaryStream */ Collection images2 = dbm.loadObjects(new ArrayList(), Image2.class); for (Image2 image : images2) { byte[] bytes = new byte[265]; int length = image.imageData.getInputStream().read(bytes); System.out.println(image.getImageId() + ", " + new String(bytes, 0, length)); } } public static void main(String[] args) throws DatabaseException, SQLException, IOException { DatabaseManager dbm = DatabaseManager.getDatabaseManager("ejp_hsql"); CreateDatabase.createHSQLDatabase(dbm); dbm.executeUpdate("delete from images"); PersistentClassManager.setTableMapping(Image2.class, "images"); blobs1(dbm); blobs2(dbm); blobs3(dbm); readingBlobs(dbm); } }