I'm using JDBC to connect to the oracle database. I know how to retrieve various data (strings, ints, etc.), but I don't know how to get ORDImage from this database. Is there any ResultSet method to accomplish this?
3 Answers
getBlob() of PreparedStatement is used to get Binary information, it returns the instance of Blob. By calling the getBytes() method on the blob object, you will get the array of binary information that can be written into the image file.
Here is the code..
PreparedStatement ps = con.prepareStatement("select * from MyImageTable");
ResultSet rs = ps.executeQuery();
if(rs.next()){
Blob blob = rs.getBlob(2); //Here 2 is second column data
byte b[] = blob.getBytes(1,(int)b.length()); //Here 1 represent first image
FileOutputStream output = new FileOutputStream("e:\\some_pic.jpg"); //path goes here
output.write(b);
output.close();
Comments
Check out the streaming lobs section of the oracle jdbc developers guide. There are several ways to access the data. There are different memory/performance impacts of the different options. The simplest solution is probably ResultSet.getBinaryStream. But if you want information about length of data, you can also use getBlob.
Comments
There are lot of ways to display to retrieve image from database in java library, this one is of the best way to retrieve image from database to java swing form and can display in JLable according to your required size.
ResultSet rs = stmnt.executeQuery("select * from table1 ");
Blob blob= rs.getBlob("image");
byte bt[]= blob.getBytes(1,(int)blob.length());
Image img= ImageIO.read(new ByteArrayInputStream(bt));
Image img1= img.getScaledInstance(200,230,Image.SCALE_SMOOTH);
ImageIcon icon= new ImageIcon(img1);
JLable1.setIcon(icon);