Jump to content

How to store multiple images in database table using JSP & servlets

add more file in ours to the form, give them all a different id insert them into a column.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, vorticalbox said:

add more file in ours to the form, give them all a different id insert them into a column.

Quote

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
        
        // gets values of text fields
        String title = request.getParameter("title");
        String brand = request.getParameter("brand");
        String condition = request.getParameter("condition");
        String price = request.getParameter("price");
        String category = request.getParameter("category");
        String description = request.getParameter("description");
        String image1 = request.getParameter("image1");
        String image2 = request.getParameter("image2");
        String image3 = request.getParameter("image3");
        
        
        InputStream inputStream = null; // input stream of the upload file
         
        // obtains the upload file part in this multipart request
        Part filePart1 = request.getPart("image1");
        if (filePart1 != null) 
        {
            // prints out some information for debugging
            System.out.println(filePart1.getName());
            System.out.println(filePart1.getSize());
            System.out.println(filePart1.getContentType());
             
            // obtains input stream of the upload file
            inputStream = filePart1.getInputStream();
        }
        
        Part filePart2 = request.getPart("image2");
        if (filePart2 != null) 
        {
            // prints out some information for debugging
            System.out.println(filePart2.getName());
            System.out.println(filePart2.getSize());
            System.out.println(filePart2.getContentType());
             
            // obtains input stream of the upload file
            inputStream = filePart2.getInputStream();
        }
         
        Part filePart3 = request.getPart("image3");
        if (filePart3 != null) 
        {
            // prints out some information for debugging
            System.out.println(filePart3.getName());
            System.out.println(filePart3.getSize());
            System.out.println(filePart3.getContentType());
             
            // obtains input stream of the upload file
            inputStream = filePart3.getInputStream();
        }
        
        
        Connection conn = null; // connection to the database
        String message = null;  // message will be sent back to client
         
        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
 
            // constructs SQL statement
            String sql = "INSERT INTO item (Name, Brand, Status, Price, Category, Description, Image1, Image2, Image3 ) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, title);
            statement.setString(2, brand);
            statement.setString(3, condition);
            statement.setString(4, price);
            statement.setString(5, category);
            statement.setString(6, description);
            

            if (inputStream != null)
            {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(7, inputStream);
            }
            
             if (inputStream != null)
            {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(8, inputStream);
            }
 
              if (inputStream != null)
            {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(9, inputStream);
            }
 
 
            // sends the statement to the database server
            int row = statement.executeUpdate();
            if (row > 0) 
            {
                message = "File uploaded and saved into database";
            }
        } 
        
        catch (SQLException ex)
        {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } 
        
        finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            // sets the message in request scope
            request.setAttribute("Message", message);
             
            // forwards to the message page
            getServletContext().getRequestDispatcher("/sellitem_success.jsp").forward(request, response);
        }
        
    } 

i have tried this in my servlet but no luck.it just saves one image only

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×