1

Following code is to insert data into MySQL database.

But code is not inserting data to the database by clicking send button.

    <%
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);

    if(request.getParameter("send")!=null){
        String scom=request.getParameter("scompany");
        String porderno=request.getParameter("pono");
        String bdate=request.getParameter("date");
        String drug1=request.getParameter("d1");
        String qty1=request.getParameter("q1");


        //getting todaydate
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());

        String sql = "INSERT INTO purchaseorderinfo SET Supplier ='"+scom+"', PONo='"+porderno+"', ExpectedDate='"+bdate+"', PODate='"+timestamp+"' ";
        pst=conn.prepareStatement(sql);


        if((scom!=null && scom.length()>0)
            && (porderno!=null && porderno.length()>0)
            && (bdate!=null && bdate.length()>0)
            && (drug1!=null && drug1.length()>0)
            && (qty1!=null && qty1.length()>0)){

            pst.execute();
          %>
          <script language="javascript">
                alert("Send sucess");
          </script>
          <%    
        }
    }
%>

This is my send button;

<div class="col-md-8">
    <form action="adminpg-purchaseorder.jsp" method="post" id="login-form" role="form" style="display: block">
        <div class="form-group">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-3">
                    <input type="submit" name="send" id="submit" tabindex="1" class="form-control btn btn-login" value="Send">
                </div>
            </div>
        </div>    
    </form>
</div>

How to fix this code for inserting data to the database?

6
  • Please share the exception log Commented Aug 25, 2015 at 9:16
  • 2
    first step is to remove it from your jsp (which should be UI only) and put all the actual java code in servlets. Commented Aug 25, 2015 at 9:16
  • 1
    In your function you are getting values from request parameter but in your form there is no such field... Commented Aug 25, 2015 at 9:18
  • No errors are showing @SaurabhJhunjhunwala Commented Aug 25, 2015 at 9:18
  • yes I have separate forms for those values :(@Luffy how can I fix this without including only one form? Commented Aug 25, 2015 at 9:19

4 Answers 4

2

Your INSERT query is wrong. It is similar to UPDATE query. Change it

String sql = "INSERT INTO purchaseorderinfo SET Supplier ='"+scom+"', PONo='"+porderno+"', ExpectedDate='"+bdate+"', PODate='"+timestamp+"' ";

to

String sql = "INSERT INTO purchaseorderinfo(Supplier,PONo,ExpectedDate,PODate)
      VALUES('"+scom+"', '"+porderno+"', '"+bdate+"', '"+timestamp+"' ";
Sign up to request clarification or add additional context in comments.

3 Comments

This is not the only change that will help OP to solve his/her problem.
I've change the code like above but still not inserting data to the database @Harshit Shrivastava
@tenten, Check even if you are getting data from the form!!
1

To sum all:

  1. you should not have any business logic in JSP
  2. verify if your connection is made to the DB
  3. update your insert statement
  4. close your connection, may be data is getting inserted but no getting commited
  5. make sure auto commit is on

Comments

0

I would suggest you to seperate java code from jsp file. write your insert code in servlet

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
            response.setContentType("text/html");
            PrintWriter pw = response.getWriter();
            String CONN_STRING = "jdbc:mysql://localhost/dbName"; 
            Connection connection;
       try{
           String scom=request.getParameter("scompany");
           String porderno=request.getParameter("pono");
           String bdate=request.getParameter("date");
           String drug1=request.getParameter("d1");
           String qty1=request.getParameter("q1");

          Class.forName("com.mysql.jdbc.Driver");
          conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
          PreparedStatement pst = connection.prepareStatement("insert into purchaseorderinfo values(?,?,?,?,?)"); 
          pst.setString(1,scom);
          pst.setString(2,porderno);      
          pst.setString(3,bdate);
          pst.setString(4,drug1);
          pst.setString(5,qty1);
          int i = pst.executeUpdate();

       if(i!=0){
           pw.println("<br>Data has been inserted");
       }
       else{
          pw.println("failed!");
       }

       } catch (Exception e){
         pw.println(e);
     }
   }

Comments

0

Following changes are required

  1. You should include all required fields in single form. Right now request.getParameter("scompany"); will return null.
  2. Update your insert query as suggested by @HarshitShrivastava. Note: You should use preparedStatement to prevent sql injection.
  3. Verify your database connection is getting created properly.
  4. Commit your connection (if auto commit is false on database side) and also close the connection at the end.
  5. Suggestion: Move your code from JSP to Servlet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.