Guest User

Untitled

a guest
Mar 19th, 2018
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <%@page import="javax.servlet.annotation.MultipartConfig"%>
  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  3. pageEncoding="ISO-8859-1" import="java.sql.*"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <%
  12. try{
  13. Class.forName("com.mysql.jdbc.Driver");
  14. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","apoorv","june1998");
  15. Statement sta = con.createStatement();
  16. ResultSet rs = sta.executeQuery("select * from mehul;");
  17. while(rs.next()){
  18. String s = rs.getString("name");
  19. %><p><%=s %></p>
  20.  
  21. <img src="<%=rs.getString("image")%>>
  22. <%
  23. x }
  24. }
  25. catch(Exception e){
  26.  
  27. } %>
  28. </body>
  29. </html>
  30.  
  31.  
  32.  
  33.  
  34. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  35. pageEncoding="ISO-8859-1"%>
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  37. <html>
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  40. <title>Insert title here</title>
  41. </head>
  42. <body>
  43. <form method="post" action="ImageProc" enctype="multiport/form-data">
  44. <input type="text" name="name"/>
  45.  
  46. <input type="file" name="image" />
  47. <input type="submit" value="Submit">
  48. </form>
  49. </body>
  50. </html>
  51.  
  52.  
  53.  
  54. package controller;
  55.  
  56. import java.io.FileInputStream;
  57. import java.io.IOException;
  58. import java.io.InputStream;
  59. import java.sql.DriverManager;
  60.  
  61. import javax.servlet.ServletContext;
  62. import javax.servlet.ServletException;
  63. import javax.servlet.annotation.MultipartConfig;
  64. import javax.servlet.annotation.WebServlet;
  65. import javax.servlet.http.HttpServlet;
  66. import javax.servlet.http.HttpServletRequest;
  67. import javax.servlet.http.HttpServletResponse;
  68. import javax.servlet.http.Part;
  69.  
  70. import java.sql.*;
  71.  
  72. @MultipartConfig
  73. @WebServlet("/ImageProc")
  74. public class ImageProc extends HttpServlet {
  75. private static final long serialVersionUID = 1L;
  76.  
  77. @Override
  78. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  79. ServletContext ctx = getServletContext();
  80. System.out.println(ctx.getMajorVersion());
  81. System.out.println(ctx.getMinorVersion());
  82.  
  83. }
  84.  
  85. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  86. String name = request.getParameter("name");
  87. String img = request.getParameter("image");
  88. FileInputStream file = new FileInputStream(img);
  89.  
  90.  
  91. System.out.print(img);
  92.  
  93. // Part img = request.getPart("image");
  94.  
  95. // InputStream stream = img.getInputStream();
  96.  
  97.  
  98. try{
  99. Class.forName("com.mysql.jdbc.Driver");
  100. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","apoorv","june1998");
  101. PreparedStatement ps = con.prepareStatement("insert into mehul values(?,?) ;");
  102. ps.setString(1,name);
  103. ps.setBinaryStream(2,file,file.available());
  104. ps.execute();
  105. System.out.println("Mehu rocxxx");
  106. }
  107. catch(Exception e){
  108.  
  109. }
  110. }
  111.  
  112. public static void main(String[] args) {
  113.  
  114. }
  115. }
Add Comment
Please, Sign In to add comment