2017-10-09 14 views
0

這是我在嘗試將數組「tid」設置爲Pojo時遇到的異常類。通過使用getParameterValues()從html通過servlet檢索多個值後,我無法將結果數組設置爲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

+0

哪裏是你的SQL查詢中的值? –

+0

我正在通過Hibernate生成查詢。我不寫任何地方的查詢。 –

+0

該錯誤看起來像是由查詢生成的。 –

回答

0

使用http post方法提交表單。

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

更改你的servlet代碼覆蓋doPost方法,

protected void doPost(HttpServletRequest req,HttpServletResponse res) 

那麼在這種情況下,這個數組將提交

String[] tid =req.getParameterValues("tid"); 
相關問題