当我在 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();
}
}