简体   繁体   中英

How to insert date selected separately into mysql using Java(JSP)

In my project I want to save date which is read separately as day,month,year by using a select box. The code used is

<%
     String value=null;
     String[] h=null;
      h=request.getParameterValues("qns[]");
     String expert=request.getParameter("expert");
     int sday=Integer.parseInt(request.getParameter("sday"));
     int smonth=Integer.parseInt(request.getParameter("smonth"));
     int syear=Integer.parseInt(request.getParameter("syear"));
     int eday=Integer.parseInt(request.getParameter("eday"));
     int emonth=Integer.parseInt(request.getParameter("emonth"));
     int eyear=Integer.parseInt(request.getParameter("eyear"));
     String start=syear+"-"+smonth+"-"+sday;
     String end=eyear+"-"+emonth+"-"+eday;    
   for(int i=0;i<h.length;i++)
      {
          s.savetask(h[1],expert,start,end);
      }
   %>

The savetask function is

  public int savetask(String qnid,String userid,String start,String end)
  {
     int n=0;
     try{

         String sql="insert into task(user_id,question_id,start_date,end_date,status)values('"+qnid+"','"+userid+"','"+start+"','"+end+"',0";
         n = db.modifyingQueries(sql);
     }
     catch(Exception e){

     }
     return n;
 }

But it is not inserting values to the table task The table format for task is

task_id      int (Auto increment)
user_id      int
question_id  int
start_date   date 
end_date     date
status       int

First of all, Your sql line is invalid.Missing bracket in the end.

   String sql="insert into task(user_id,question_id,start_date,end_date,status)values
                            ('"+qnid+"','"+userid+"','"+start+"','"+end+"',0");
                                                                            ^

one more syntax error is there is no need for " after the 0 in the end since it is an int (realized after you updated the column structure).

Still if you face further exceptions,provide the stack trace.

Update(comment that helpful to resolve the issue):

What you have to do is after the line String sql = ... ,Put System.out.println(sql) So it prints the query in console.Pick that line and fire it on your Data base query manager.And see for any syntax errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM