Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. <form method = "post" enctype="multipart/form-data" action = "designservlet">
  2. Product Image:<input type = "file" name = "Image"/>
  3. Design Name:<input type = "text" placeholder = "Design" name = "design"/>
  4. <p>Date: <input type= "text" class="datepicker" name = "date"/></p>
  5.  
  6. <input type = "submit" value = "save"/>
  7.  
  8. </form>
  9.  
  10. package com.jw;
  11.  
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.sql.Blob;
  15. import java.sql.Connection;
  16. import java.sql.Date;
  17. import java.sql.DriverManager;
  18. import java.sql.SQLException;
  19. import java.text.DateFormat;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22.  
  23. import javax.servlet.ServletException;
  24. import javax.servlet.annotation.MultipartConfig;
  25. import javax.servlet.annotation.WebServlet;
  26. import javax.servlet.http.HttpServlet;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import javax.servlet.http.Part;
  30.  
  31. import com.mysql.jdbc.PreparedStatement;
  32.  
  33.  
  34.  
  35.  
  36.  
  37. /**
  38. * Servlet implementation class designservlet
  39. */
  40. @WebServlet("/designservlet")
  41. @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
  42. public class designservlet extends HttpServlet {
  43. private static final long serialVersionUID = 1L;
  44.  
  45. private static final String Design_name = null;
  46.  
  47. private static final String Date = null;
  48.  
  49. private static final Blob Image = null;
  50.  
  51. private String dbURL = "jdbc:mysql://localhost:3306/design";
  52. private String dbUser = "root";
  53. private String dbPass = "yellow1";
  54.  
  55. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  56.  
  57. String name12 = request.getParameter("design");
  58.  
  59. InputStream inputStream = null; // input stream of the upload file
  60.  
  61.  
  62.  
  63.  
  64.  
  65. Part filePart = request.getPart("Image");
  66.  
  67.  
  68.  
  69. if (filePart != null) {
  70. // prints out some information for debugging
  71. System.out.println(filePart.getName());
  72. System.out.println(filePart.getSize());
  73. System.out.println(filePart.getContentType());
  74.  
  75. // obtains input stream of the upload file
  76. inputStream = filePart.getInputStream();
  77. }
  78.  
  79.  
  80. Connection conn = null; // connection to the database
  81. String message = null; // message will be sent back to client
  82.  
  83. try {
  84.  
  85. DateFormat formatter;
  86.  
  87. formatter = new SimpleDateFormat("dd-MM-YYY");
  88.  
  89.  
  90. java.util.Date date1 = formatter.parse(request.getParameter("date"));
  91. // connects to the database
  92. Class.forName("com.mysql.jdbc.Driver");
  93.  
  94.  
  95.  
  96.  
  97. conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
  98.  
  99. // constructs SQL statement
  100. String sql = "INSERT INTO extra (Design_name, Date, Image) VALUES (?, ?, ?)";
  101. java.sql.PreparedStatement statement = conn.prepareStatement(sql);
  102.  
  103.  
  104.  
  105. statement.setString(1, name12);
  106. statement.setDate(2, (java.sql.Date) date1);
  107.  
  108.  
  109.  
  110.  
  111. if (inputStream != null) {
  112. // fetches input stream of the upload file for the blob column
  113.  
  114. statement.setBlob(3, inputStream);
  115.  
  116. }
  117.  
  118. // sends the statement to the database server
  119. int row = statement.executeUpdate();
  120.  
  121. if(row > 0)
  122. {
  123. message = "File uploaded and saved into database";
  124. }
  125.  
  126. }catch (SQLException | ClassNotFoundException | ParseException ex) {
  127. message = "ERROR: " + ex.getMessage();
  128. ex.printStackTrace();
  129. } finally {
  130. if (conn != null) {
  131. // closes the database connection
  132. try {
  133. conn.close();
  134. } catch (SQLException ex) {
  135. ex.printStackTrace();
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142. }
  143.  
  144.  
  145.  
  146. }
  147.  
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement