通过使用getParameterValues()从html通过servlet检索多个值后,我无法将结果数组设置为Pojo类

时间:2017-10-09 13:37:12

标签: java hibernate jsp servlets

这是我尝试设置阵列时遇到的例外情况'到Pojo班。

 org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch 
 update

 at org.jsp.app.Hibe.saveData(Hibe.java:24)
 at org.jsp.app.Servlet1.doGet(Servlet1.java:60)
 Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xAC\xED\x00\x05ur...' for column 'tid' at row 1

这是pojo课程。

  package org.jsp.app;
     import java.util.Set;
     import javax.persistence.CascadeType;
     import javax.persistence.Entity;
     import javax.persistence.Id;
     import javax.persistence.OneToMany;
     @Entity
     public class PatientMaster 
     {
     @Id
     private int id;
     private String name;
     private String age;
     private String[] tid;
     public String[] getTid() {
        return tid;
    }
    public void setTid(String[] tid) {
        this.tid = tid;
    }
    private String tresult;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getTresult() {
        return tresult;
    }
    public void setTresult(String tresult) {
        this.tresult = tresult;
    }
    @Override
    public String toString() 
    {
        return this.id+" "+this.name+" "+this.age+" "+this.tid+" "+this.tresult;
    } 

}

这是我通过它检索并尝试将数据设置为POJO类的Servlet。

   package org.jsp.app;

   import java.io.IOException;
   import java.io.PrintWriter;
   import java.sql.DriverManager;
   import java.sql.SQLException;
   import java.util.Collections;
   import java.util.HashSet;
   import java.util.Set;

   import javax.servlet.ServletException;
   import javax.servlet.annotation.WebServlet;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import com.mysql.jdbc.Connection;
   @WebServlet(urlPatterns="/serv1")
     public class Servlet1 extends HttpServlet
    {
    @Override
    public void init() throws ServletException 
    {
    try 
    {
        Class.forName("com.mysql.jdbc.Driver");
        java.sql.Connection conn = 
        DriverManager.getConnection("jdbc:mysql://localhost:3306?user=root&password=123456");
    } 
    catch (ClassNotFoundException | SQLException e) 
    {
        e.printStackTrace();
    }
    }

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String id = req.getParameter("id");
    System.out.println(id);
    int id1 = Integer.parseInt(id);
    String name = req.getParameter("name");
    String age = req.getParameter("age");
    String[] tid =req.getParameterValues("tid");        
    System.out.println();
    String tresult = req.getParameter("result");

    PrintWriter pw = resp.getWriter();
    pw.println("<html><body><a href=Home.html><<Home</a></body></html>");
    PatientMaster p1 = new PatientMaster();
    p1.setId(id1);
    p1.setName(name);
    p1.setAge(age);     
    p1.setTid(tid);
    p1.setTresult(tresult);

    Hibe.saveData(p1);
}
}

HTML文件

  <!DOCTYPE html>
  <html>
   <head>
   <meta charset="ISO-8859-1">
   <title>Insert title here</title>
  </head>
 <body>
 <p> Kindly Enter the details of patient </p>
 <form action="serv1" method="get">
 ID: <input type="text" name="id"/> <br><br>
 Name: <input type = "text" name = "name" /> </br></br>
 Age: <input type = "number" name = "age" /> </br></br>
 Test Taken: 1<input type="checkbox" name="tid" value="1"/>
 2<input type="checkbox" name="tid" value="2"/>
 3<input type="checkbox" name="tid" value="3"/>
 4<input type="checkbox" name="tid" value="4"/>
 5<input type="checkbox" name="tid" value="5"/>
 6<input type="checkbox" name="tid" value="6"/>
 7<input type="checkbox" name="tid" value="7"/>

</select></br></br>
<input type="submit" value="register">
</form>
</body>
</html>

请帮我将通过html页面检索的数据保存到数据库。我无法保存列tid的数据

1 个答案:

答案 0 :(得分:0)

使用http post方法提交表单。

<form action="serv1" method="post">

更改servlet代码以覆盖doPost方法,

protected void doPost(HttpServletRequest req,HttpServletResponse res)

然后在这种情况下,此数组将提交值

String[] tid =req.getParameterValues("tid");