vendredi 12 avril 2019

how to fix http 500 error and blob file is not uploading

post and dopost in jsp and servlet resp. are not working instead get and services is working. And blob image file is also not uploading

jsp code:

servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");

//retrieving image InputStream inputStream = null; // input stream of the upload file

    String Title = request.getParameter("title");

     // obtains the upload file part in this multipart request
    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();

          pw.println("Got photooo");
    }


    String Author = request.getParameter("author");
    String Price = request.getParameter("price");

    float Pricef = Float.valueOf(Price);
      pw.println("Got parameters");

Connection conn = null; Statement stmt = null;

  try{
        Class.forName("com.mysql.cj.jdbc.Driver");

  pw.println("Driver Loaded");
  conn = DriverManager.getConnection(dbURL, dbUser, dbPass); 
  pw.println("Connection Established_1");
  stmt = conn.createStatement();

 // String query = "Insert into books(title,image,author,price) values('"+Title +"','"+inputStream +"','"+Author+"','"+Pricef +"')" ;
 String query = "Insert into books(title,image,author,price) values(?,?,?,?)" ;

 PreparedStatement ps =conn.prepareStatement(query);
 ps.setNString(1,Title);
 if(inputStream != null){
 // fetches input stream of the upload file for the blob column
 ps.setBinaryStream(2, inputStream);
 }
 ps.setNString(3,Author);
 ps.setFloat(4, Pricef);



 // Sends the statement to the database server
 int row= ps.executeUpdate();
 if(row>0){
 pw.println("data inserted");

 }


 /*int row = stmt.executeUpdate(query);
        if (row > 0) {

            pw.println("Data Inserted");
        }  */


  } catch (SQLException ex) {
         ex.getMessage();
      //  ex.printStackTrace();
      ex.printStackTrace(pw);
        pw.println("Connection not Established_1");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(insertbooks.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (conn != null) {
            // closes the database connection
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        System.out.println("Successfully");
        // sets the message in request scope
      //  request.setAttribute("message", message);

        // forwards to the message page
       // getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
    }

pw.close();
}

}




Aucun commentaire:

Enregistrer un commentaire