【问题标题】:Getting exception 'could not execute statement' while entering date in table在表中输入日期时出现异常“无法执行语句”
【发布时间】:2016-11-03 11:59:28
【问题描述】:

Leave.jsp 申请请假的jsp页面。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
 <body>
   <head>
 <title>
  Leave Apply</title>
   <style>
    body {
    font-family: Times New Roman;
     font-size: 200%;
      }
    table {
   border: 0px;
    font-family: Time New Roman;
    font-size: 50%;
    width: 30%;
   background-color: orange;
   padding: 15px;
   }
      </style>
</head>
<body >
 <center>
   Apply Leave<br/><br/>
    <form method="post" action="leaveservlet" name="form1">
        <table>
            <tr>
                <td>
                   Subject:
                </td>
                <td>
                    <input type="text" name="name" placeholder="Subject">
                 </td>
                </tr>
                <tr>
                <td>
                   Content:
                </td>
                <td>
                    <textarea name="content" rows="8" cols="30"></textarea>
                 </td>
                </tr>
                <tr>
                <td>
                   From:
                </td>
                <td>
                    <input type="date" name="from"> 
                </td>
                </tr>
                  <tr>
                <td>
                   Till:
                </td>
                <td>
                    <input type="date" name="till"> 

                </td>
                </tr>
                <input type="hidden" name="status" value="0">

                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value="Leave     Apply">&nbsp&nbsp<input type="reset" name="reset" value="Reset"></td>

                </tr>   
        </table>
    <br/>

    </form>
    </center>

    </body>
  </html>

leaveservlet 主题、内容、from 和 until 被发布到这个 servlet,然后我使用 SimpleDateFormat 解析字符串到日期。

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
     PrintWriter out= response.getWriter();
   String sub= request.getParameter("name");
   String con= request.getParameter("content");
   String from= request.getParameter("from");
   String till= request.getParameter("till");
   String status= request.getParameter("status");
   int i = Integer.parseInt(status);
   Session s= HibernateUtil.getSessionFactory().openSession();
   Transaction t= s.beginTransaction();
   Leave l= new Leave();
   l.setSubject(sub);
   l.setContent(con);
   try{
   l.setFrom(fmt.parse(from));
   l.setTill(fmt.parse(till));
   }
   catch(Exception e){
       out.print("sorry");
   }
   l.setStatus(i);
   s.save(l);
   t.commit();
   s.close();
}

离开.java

public class Leave  implements java.io.Serializable {


 private Integer id;
 private String subject;
 private String content;
 private Date from;
 private Date till;
 private Integer status;

public Leave() {
}


public Leave(String subject, String content, Date from) {
    this.subject = subject;
    this.content = content;
    this.from = from;
}
public Leave(String subject, String content, Date from, Date till, Integer status) {
   this.subject = subject;
   this.content = content;
   this.from = from;
   this.till = till;
   this.status = status;
}

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}
public String getSubject() {
    return this.subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}
public String getContent() {
    return this.content;
}

public void setContent(String content) {
    this.content = content;
}
public Date getFrom() {
    return this.from;
}

public void setFrom(Date from) {
    this.from = from;
}
public Date getTill() {
    return this.till;
}

public void setTill(Date till) {
    this.till = till;
}
public Integer getStatus() {
    return this.status;
}

public void setStatus(Integer status) {
    this.status = status;
}

ExceptionException thrown on browser

【问题讨论】:

    标签: hibernate jsp servlets


    【解决方案1】:

    出现此问题是因为您从输入页面为这些字段发送的数据不正确(using SQL reserved/syntax keywords 'from' 'select' ,etc..)

    subject, content, from
    

    尝试设置一些简单的数据,例如 subject=subjectA、content=contentB、from=FROMUS

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2013-08-28
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      相关资源
      最近更新 更多