-4

当我在 db 表中执行插入时,发生异常,它说:

Data truncation error in mySQL while inserting data..

在:->

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'branchname' at row 1

列类型是varchar

JSP:

<form action="BranchServlet" method="post">
<table align="center">
<tr>
<td align="center" colspan="2"><hr>Add Branch Details<hr></td>
</tr>


<tr>
<td align="left">Branch Name</td>
<td align="center"><input type="text" name="branchname"></td>
</tr>
<tr>
<td align="left">Branch Code</td>
<td align="center"><input type="text" name="branchcode"></td>
</tr>
<tr>
<td align="center">

</td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" Value="Submit"></td>
</tr>
</table>
</form>

小服务程序:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        String bname = request.getParameter("branchname");
        String bcode = request.getParameter("branchcode");

        BranchModel bm = new BranchModel();
        BranchDAO bd = new BranchDAO();

        bm.setBranchcode(bcode);
        bm.setBranchname(bname);

        bd.insert(bm);

        response.sendRedirect("branches.jsp");
    }

道方法:

public void insert(BranchModel m)
    {
        try{
            Class.forName(driver);
            con = DriverManager.getConnection(url+db,"root","root");
            Statement st = con.createStatement();

            st.executeUpdate("insert into branches values('"+m.getBranchcode()+"','"+m.getBranchname()+"')");
            System.out.println("record inserted");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
4

1 回答 1

4

您说该列是 varchar 类型,但不是什么大小的 varchar。这似乎是你问题的根源。我没有看到任何对文本输入大小进行限制的验证。输入验证需要将输入大小限制为相应 varchar 列的大小。

于 2012-04-04T17:37:30.163 回答