Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. <%@ page import="java.sql.*,model.Upload,java.io.*,java.util.*" %>
  2. <HTML>
  3. <table border="1">
  4. <tr><th>ID</th><th>Image</th></tr>
  5. <%
  6. try{
  7. Class.forName("com.mysql.jdbc.Driver").newInstance();
  8. Connection con=DriverManager.getConnection("jdbc:mysql://localhost/backup","root","mysql");
  9. Statement stmt=con.createStatement();
  10. String strQuery = "select * from contacts";
  11. ResultSet rs = stmt.executeQuery(strQuery);
  12. while(rs.next()){
  13. %>
  14. <tr>
  15. <td><%=rs.getInt("contact_id")%></td>
  16. <td>
  17. <img src="RetrieveImages.java?id=<%=rs.getInt(1)%>" width="100" height="100">
  18.  
  19. </a></td>
  20. </tr>
  21. <%
  22. }
  23. rs.close();
  24. con.close();
  25. stmt.close();
  26. }
  27. catch(Exception e)
  28. {
  29. e.getMessage();
  30. }
  31. %>
  32. </table>
  33. </HTML>
  34.  
  35. <%@page import="java.sql.Blob"%>
  36. <%@page import="java.io.OutputStream"%>
  37. <%@page import="java.sql.ResultSet"%>
  38. <%@page import="java.sql.PreparedStatement"%>
  39. <%@page import="java.sql.DriverManager"%>
  40. <%@page import="java.sql.Connection"%>
  41. <%@ page import="model.Upload" %>
  42.  
  43. <%
  44. String contact_id = request.getParameter("contact_id");
  45. String dbURL = "jdbc:mysql://localhost/backup";
  46. String dbUser = "root";
  47. String dbPass = "mysql";
  48.  
  49. Connection con = null;
  50.  
  51. try{
  52. Class.forName("com.mysql.jdbc.Driver");
  53. con = DriverManager.getConnection(dbURL, dbUser, dbPass);
  54.  
  55. PreparedStatement ps = con.prepareStatement("select * from contacts where contact_id=?");
  56. ps.setString(1, contact_id);
  57.  
  58. ResultSet rs = ps.executeQuery();
  59.  
  60. if(rs.next()){
  61. Blob blob = rs.getBlob("photo");
  62. byte byteArray[] = blob.getBytes(1, (int)blob.length());
  63.  
  64. response.setContentType("image");
  65. OutputStream os = response.getOutputStream();
  66. os.write(byteArray);
  67. os.flush();
  68. os.close();
  69. }
  70.  
  71. }catch(Exception ex){
  72. ex.printStackTrace();
  73. }finally{
  74. if(con != null){
  75. try{
  76. con.close();
  77. }catch(Exception e){
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement