This servlet is used to retrieve BLOB type from database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DisplayBlobServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Blob photo = null ; Connection conn = null ; Statement stmt = null ; ResultSet rs = null ; String query = "select bill_image from bill_image where account_no in 136771 and bill_ref_no in 9228007" ; ServletOutputStream out = response.getOutputStream(); try { conn = getHSQLConnection(); } catch (Exception e) { response.setContentType( "text/html" ); out.println( "<title>Person Photo</title>" ); out.println( "<h1>Database Connection Problem.</h1>" ); return ; } try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); if (rs.next()) { photo = rs.getBlob( 1 ); } else { response.setContentType( "text/html" ); out.println( "<title>Person Photo</title>" ); out.println( "<h1>No photo found for id= 001 </h1>" ); return ; } response.setContentType( "text/plain" ); InputStream in = photo.getBinaryStream(); int length = ( int ) photo.length(); int bufferSize = 1024 ; byte [] buffer = new byte [bufferSize]; while ((length = in.read(buffer)) != - 1 ) { System.out.println( "writing " + length + " bytes" ); out.write(buffer, 0 , length); } in.close(); out.flush(); } catch (SQLException e) { response.setContentType( "text/html" ); out.println( "<title>Error: Person Photo</title>" ); out.println( "<h1>Error=" + e.getMessage() + "</h1>" ); return ; } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } private static Connection getHSQLConnection() throws Exception { Class.forName( "org.hsqldb.jdbcDriver" ); System.out.println( "Driver Loaded." ); String url = "jdbc:hsqldb:data/tutorial" ; return DriverManager.getConnection(url, "sa" , "" ); } public static Connection getMySqlConnection() throws Exception { String driver = "org.gjt.mm.mysql.Driver" ; String username = "oost" ; String password = "oost" ; Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, password); return conn; } public static Connection getOracleConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver" ; String url = "jdbc:oracle:thin:@localhost:1521:databaseName" ; String username = "username" ; String password = "password" ; Class.forName(driver); // load Oracle driver Connection conn = DriverManager.getConnection(url, username, password); return conn; } } |