create table players ( playername varchar2(50) primary key, playerphoto blob );
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h2>Add Player</h2> <form id="form1" enctype="multipart/form-data" action="addplayer" method="post"> <table> <tr> <td>Enter Player Name :</td> <td><input type="text" name="playername"/></td> </tr> <tr> <td>Select Photo </td> <td><input type="file" name="playerphoto" /> </tr> </table> <p/> <input type="submit" value="Add Photo"/> </form> <p/> <a href="listplayers">List Players</a> </body> </html>
package servlets; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.Scanner; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet(name = "AddPlayerServlet", urlPatterns = {"/addplayer"}) @MultipartConfig public class AddPlayerServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Part p = request.getPart("playername"); Scanner scanner = new Scanner( p.getInputStream()); String playername = scanner.nextLine(); Part photo = request.getPart("playerphoto"); // Connect to Oracle Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); con.setAutoCommit(false); PreparedStatement ps = con.prepareStatement("insert into players values(?,?)"); ps.setString(1, playername); // size must be converted to int otherwise it results in error ps.setBinaryStream(2, photo.getInputStream(), (int) photo.getSize()); ps.executeUpdate(); con.commit(); con.close(); out.println("Added Player Successfully. <p> <a href='listplayers'>List Players </a>"); } catch(Exception ex) { System.out.println(ex.getMessage()); } finally { out.close(); } } }
package servlets; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "ListPlayersServlet", urlPatterns = {"/listplayers"}) public class ListPlayersServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); PreparedStatement ps = con.prepareStatement("select * from players"); ResultSet rs = ps.executeQuery(); out.println("<h1>Players</h1>"); while ( rs.next()) { out.println("<h3>" + rs.getString("playername") + "</h3>"); out.println("<img width='300' height='300' src=displayphoto?name=" + rs.getString("playername") + "></img> <p/>"); } con.close(); } catch(Exception ex) { System.out.println(ex.getMessage()); } finally { out.close(); } } }
package servlets; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "DisplayPhotoServlet", urlPatterns = {"/displayphoto"}) public class DisplayPhotoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); PreparedStatement ps = con.prepareStatement("select playerphoto from players where playername = ?"); String name = request.getParameter("name"); ps.setString(1,name ); ResultSet rs = ps.executeQuery(); rs.next(); Blob b = rs.getBlob("playerphoto"); response.setContentType("image/jpeg"); response.setContentLength((int) b.length()); InputStream is = b.getBinaryStream(); OutputStream os = response.getOutputStream(); byte buf[] = new byte[(int) b.length()]; is.read(buf); os.write(buf); os.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }