My successful experience on how to store and load an image (.jpg) using JavaDB+EclipseLink+Java 1.6.0_25 and NetBeans 7.0 is as follows.
- A table named, Customer, is used as an illustrating example. There are three attributes in this table: CustomerId, Name, and Photo.
- An SQL script of this table is shown below:
create table APP.Customer
(
CUSTOMER_ID INTEGER not null primary key generated always as identity (start with 1, increment by 1),
FIRST_NAME VARCHAR(255),
PHOTO BLOB(2147483647)
)
- Create a database named, CustDB, through the Services tab of NetBeans IDE.
- In NetBeans IDE 7.0, click on the Services tab.
- Right-click on the JavaDB node, and then click on Create Database…
- Enter the database name, user name and password in the provided text boxes. Use “mydb” for the user name and password.
- Create a new connection to the newly-created database.
- On the “Services” tab, right-click on the “Databases” node, and then click on “New Connection…”
- We will use JavaDB Network for this tutorial, so select “JavaDB (Network)” for the driver. Then, click Next.
- Enter Host Name: localhost, Port: 1527, Database: CustDB, User name: mydb, and Password: mydb.
- Click Next
- Select “APP” as the schema name
- Click Finish
- A new connection node: jdbc:derby:localhost:1527/CustDB [myapp on APP]
- Create a new table in CustDB
- Expand the CustDB connection node
- Expand the APP node under CustDB node
- Right-click on the “Table” node under the CustDB/APP node, and click on “Execute Command…” to open an SQL window
- Copy the SQL script shown above into the SQL window, and click “Execute” icon.
- Create Java Library Project
- On the Netbeans IDE 7.0 menu, File –> New Project
- Categories: Java and Projects: Java Class Library
- Click Next
- Project Name: MyCustomerLibraryProject
- Click Finish
- On the Project tab, right-click on the MyCustomerLibraryProject node –> New –> Entity Classes from Databases …
- Select the CustDB connection that is just created above for the Database Connection. Then, there should be only one available table, named Customer.
- Click “Add All” and then click “Next”
- Put “demo” as the package name, and then click “Finish”
- Modify the generated Customer.class file in the MyCustomerJavaLibraryProject
- Click the “demo” node to expand the node, there will be one Java file named, “Customer.java”.
- Click the file to open it in the NetBeans IDE editor.
- Since we use “mydb” as user name and password, we need to use a fully-qualified name for the table name. So, change:
@Table(name = "CUSTOMER")
to
@Table(name = "APP.CUSTOMER")
The reason is that JavaDB will presume that the user name, in this case “mydb”, is the schema name, however, we create the Customer table in “APP” schema. As a result, we need to use a fully-qualified name for the Customer table which is “APP.CUSTOMER”.
- In JavaDB, EclipseLink maps “BLOB” SQL data type into “Serializable” Java SQL data type. However, to store an image, we need to use “byte[]” Java SQL data type. Accordingly, we need to change:
- The data type of “photo” variable definition from Serializable to byte[].
@Lob
@Column(name = "PHOTO")
private Serializable photo;
to
@Lob
@Column(name = "PHOTO")
private byte[] photo;
- The returned data type of getPhoto() method from Serializable to byte[]
public Serializable getPhoto() {
return photo;
}
to
public byte[] getPhoto() {
return photo;
}
- The parameter data type of setPhoto() from Serializable to byte[]
public void setPhoto(Serializable photo) {
this.photo = photo;
}
to
public void setPhoto(byte[] photo) {
this.photo = photo;
}
- The data type of “photo” variable definition from Serializable to byte[].
- Now, we are ready to generate MyCustomerLibrary.jar by right-clicking on the “MyCustomerLibraryProject” and then click “Clean and Build”
- Create a Library Wrapper module containing “MyCustomerLibraryProject.jar” created above. Then, set the dependencies of this module to DerbyClient and EclipseLink library wrapper modules. Assume that these two modules are created beforehand.
- Now, in a Java code where we need to save an image from JLabel into our CustDB database in JavaDB, use the following code snippet.
//Store image from jLabel into JavaDB
Customer newCustomer = new Customer();
ImageIcon imgIcon = (ImageIcon) jLabelImage.getIcon();if (imgIcon != null) {
Image img = imgIcon.getImage();
byte[] imgByte = getByteArrayFromImage(img);newCustomer.setPhoto(imgByte);
- The getByteArrayFromImage() method is defined as follows.
private byte[] getByteArrayFromImage(Image img) {byte[] imgByte = null;
try {
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
int imgType = BufferedImage.TYPE_INT_ARGB; // you can experiment with this one
BufferedImage bi = new BufferedImage(imgWidth, imgHeight, imgType);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (ImageIO.write(bi, "JPEG", baos)) {
imgByte = baos.toByteArray();
}} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}return imgByte;
}
- In addition, for the Java code where an image is loaded from CustDB and displayed in JLabel, use the following code snippet:
//For image from database to jLabel
Customer existingCustomer = getCustomerFromDB(customerID);//load a Customer object from database
byte[] imageByte = existingCustomer.getPhoto();
if (imageByte != null) {ImageIcon imageIcon = new ImageIcon(imageByte);
java.awt.Image img = imageIcon.getImage();this.jLabelImage.setIcon(imageIcon);
this.jLabelImage.setText(null);}
- That’s pretty much all we need to do. However, you may need to specify allowable image file types, and scale an image before putting in a JLabel.
- The methods in steps #9, #10 and #11 can be used for MySQL database. The thing is that MySQL maps BLOB SQL data type into byte[] Java SQL data type. Therefore, we do not need to do step #7.
- The most important steps are #7, #9, #10 and #11 that we need to modify Customer.java file and define save/load methods.
Have fun!
Nice Tutorial…
.
.
Can you please provide a download link of tutorial.