Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form method = "post" enctype="multipart/form-data" action = "designservlet">
- Product Image:<input type = "file" name = "Image"/>
- Design Name:<input type = "text" placeholder = "Design" name = "design"/>
- <p>Date: <input type= "text" class="datepicker" name = "date"/></p>
- <input type = "submit" value = "save"/>
- </form>
- package com.jw;
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.Blob;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- 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;
- import com.mysql.jdbc.PreparedStatement;
- /**
- * Servlet implementation class designservlet
- */
- @WebServlet("/designservlet")
- @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
- public class designservlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private static final String Design_name = null;
- private static final String Date = null;
- private static final Blob Image = null;
- private String dbURL = "jdbc:mysql://localhost:3306/design";
- private String dbUser = "root";
- private String dbPass = "yellow1";
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String name12 = request.getParameter("design");
- InputStream inputStream = null; // input stream of the upload file
- Part filePart = request.getPart("Image");
- if (filePart != null) {
- // prints out some information for debugging
- System.out.println(filePart.getName());
- System.out.println(filePart.getSize());
- System.out.println(filePart.getContentType());
- // obtains input stream of the upload file
- inputStream = filePart.getInputStream();
- }
- Connection conn = null; // connection to the database
- String message = null; // message will be sent back to client
- try {
- DateFormat formatter;
- formatter = new SimpleDateFormat("dd-MM-YYY");
- java.util.Date date1 = formatter.parse(request.getParameter("date"));
- // connects to the database
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
- // constructs SQL statement
- String sql = "INSERT INTO extra (Design_name, Date, Image) VALUES (?, ?, ?)";
- java.sql.PreparedStatement statement = conn.prepareStatement(sql);
- statement.setString(1, name12);
- statement.setDate(2, (java.sql.Date) date1);
- if (inputStream != null) {
- // fetches input stream of the upload file for the blob column
- statement.setBlob(3, inputStream);
- }
- // sends the statement to the database server
- int row = statement.executeUpdate();
- if(row > 0)
- {
- message = "File uploaded and saved into database";
- }
- }catch (SQLException | ClassNotFoundException | ParseException ex) {
- message = "ERROR: " + ex.getMessage();
- ex.printStackTrace();
- } finally {
- if (conn != null) {
- // closes the database connection
- try {
- conn.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement