This topic I'm going to talk about how to store image in database and retrieve them for display.
Step 1. Create table
Code: |
create table test_blob ( blob_id varchar2(2), blob_file varchar2(50), blob_type varchar2(50), blob_content blob ); |
Step 2 Create class to hold data
Code: |
package app.image.demo; import java.awt.Image; import java.awt.Toolkit; public class BlobRec { private String id; private String file; private String desc; private String type; private byte[] byteStream; public void setID (String id) { this.id = id; } public String getID () { return this.id; } public void setFile (String file) { this.file = file; } public String getFile () { return this.file; } public void setDesc (String desc) { this.desc = desc; } public String getDesc () { return this.desc; } public void setType (String type) { this.type = type; } public String getType () { return this.type; } public void setByteStream (byte[] byteStream) { this.byteStream = byteStream; } public byte[] getByteStream () { return this.byteStream; } public Image getImage () { Image image = Toolkit.getDefaultToolkit().createImage(getByteStream()); return image; } } |
Step 3 Create class to process data
Code: |
package app.image.demo; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import oracle.jdbc.OraclePreparedStatement; import oracle.jdbc.OracleResultSet; import oracle.sql.BLOB; public class UploadBlob { private Connection connection = null; private ResultSet rs; private Statement stmt; private PreparedStatement pstmt; public UploadBlob() { try { //Load the JDBC driver String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName); //Create a connection to the database String serverName = "localhost"; String portNumber = "1521"; String sid = "testdbs"; String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid; String username = "scott"; String password = "tiger"; connection = DriverManager.getConnection(url, username, password); //System.out.println("Application connect to DB successfully."); } catch (ClassNotFoundException e){ // Could not find the database driver System.out.println("ClassNotFoundException: "+e.getMessage()); } catch (SQLException e){ // Could not connect to the database System.out.println("SQLException: "+e.getMessage()); } } public void close() { try { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (pstmt != null) { pstmt.close(); pstmt = null; } if (!connection.isClosed()) { connection.close(); connection = null; } } catch (SQLException e) { // nothing } //System.out.println("Disconnect from database."); } public void loadBlob(String bid, String files, String types, InputStream stream) { OraclePreparedStatement ops; String sql = "insert into test_blob values(?, ?, ?, ?)"; try { BLOB blob = getBLOB(connection, stream); ops = (OraclePreparedStatement)connection.prepareStatement(sql); ops.setString(1, bid); ops.setString(2, files); ops.setString(3, types); ops.setBLOB(4, blob); ops.execute(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public BlobRec getBlobRec(String blob_id) { BlobRec br = new BlobRec(); try { stmt = connection.createStatement(); String sql = "select * from test_blob where blob_id = '" + blob_id + "'"; rs = stmt.executeQuery(sql); if (rs.next()) { br.setID(rs.getString(1)); br.setFile(rs.getString(2)); br.setType(rs.getString(3)); BLOB blob = ((OracleResultSet)rs).getBLOB (4); if (blob != null) { br.setByteStream(getByteArray(blob)); } } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return br; } public byte[] getByteArray (BLOB blob) throws SQLException, IOException { BufferedInputStream bis; ByteArrayOutputStream bo; bis = new BufferedInputStream(blob.getBinaryStream()); bo = new ByteArrayOutputStream(); int MAXBUFSIZE = 1024; byte[] buf = new byte[MAXBUFSIZE]; int n = 0; while ((n = bis.read(buf, 0, MAXBUFSIZE)) != -1) { bo.write(buf, 0, n); } bo.flush(); bo.close(); bis.close(); buf = null; return bo.toByteArray(); } public BLOB getBLOB (Connection connection, InputStream inputStream) throws SQLException, IOException { BLOB blob; blob = BLOB.createTemporary(connection, false, BLOB.DURATION_SESSION); byte[] binaryBuffer; long position = 1; int bytesRead = 0; binaryBuffer = new byte[inputStream.available()]; while ((bytesRead = inputStream.read(binaryBuffer)) != -1) { blob.putBytes(position, binaryBuffer); position += bytesRead; } return blob; } } |
Step 4 Create class to read image from file
Code: |
package app.image.demo; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class LoadImage { public static void main (String arg[]) { try { UploadBlob upload = new UploadBlob(); System.out.println("Connected..."); String filename = "view.jpg"; String bid = "1"; // load the image from file InputStream inputStream = new FileInputStream(filename); // store in database upload.loadBlob(bid, filename, "jpeg", inputStream); upload.close(); System.out.println("Finished..."); inputStream.close(); catch (IOException e) { e.printStackTrace(); } } } |
Step 5 Create class to retrieve image and display
Code: |
package app.image.demo; import javax.swing.ImageIcon; |
Next topic I gonna talk about how to upload image in web application
Step 1 Create a servlet class
Code: |
package app.image.demo; import javax.servlet.ServletException; /** /** public class UploadServlet extends HttpServlet { |
Step 2 Create web.xml file
Code: |
<?xml version="1.0" encoding="UTF-8"?> <web-app> <!-- Establish the default MIME type mappings --> </web-app> |
Step 3 Create test.html file
Code: |
<HTML> <FORM ACTION="UploadServlet" ENCTYPE="multipart/form-data" METHOD=POST> </BODY> |
Lastly build togather a war file
เป็นประโยชน์อย่างมากครับ ขอบคุณครับ