当前位置: 代码迷 >> Java Web开发 >> JDBC安插MySQL报错:Data truncated for column 'period' at row 1
  详细解决方案

JDBC安插MySQL报错:Data truncated for column 'period' at row 1

热度:952   发布时间:2016-04-17 01:01:50.0
JDBC插入MySQL报错:Data truncated for column 'period' at row 1
前台用jQuery的AJAX的get方法传递参数
JScript code
$("#submit_button").click(function(){            $.get("exchange_back.jsp",{title:$("#title").val(),                exchange:$("#exchange").val(),                period:$("input[type=radio]:checked").val(),                description:$("#description").val()},function(responseText){                            });                });


exchange_back.jsp:
Java code
Class.forName("com.mysql.jdbc.Driver");        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/YiShu?user=root&password=sad&Unicode=true&characterEncoding=UTF-8");    String sql = "insert into book values(?,?,?,?,now(),adddate(now(),?),?,?)";    PreparedStatement pstmt = conn.prepareStatement(sql);    pstmt.setInt(1,(Integer)session.getAttribute("id"));     pstmt.setString(2,(String)session.getAttribute("username"));    pstmt.setString(3,request.getParameter("title"));    pstmt.setString(4,request.getParameter("exchange"));    pstmt.setString(5,new String(request.getParameter("period").getBytes("iso8859-1"),"utf-8"));    pstmt.setString(6,new String(request.getParameter("period").getBytes("iso8859-1"),"utf-8"));    pstmt.setString(7,request.getParameter("description"));


Eclipse的Console窗口报错:
严重: Servlet.service() for servlet [jsp] in context with path [/YiShu] threw exception [An exception occurred processing JSP page /exchange_back.jsp at line 57

54: System.out.println("adddate(now(),"+request.getParameter("period")+")");
55: //System.out.println(request.getParameter("description"));
56:
57: pstmt.executeUpdate();
58: pstmt.close();
59: conn.close();
60:  


Stacktrace:] with root cause
java.sql.SQLException: Data truncated for column 'period' at row 1.
......blablabla

已经把jsp页面、jsp中的html、mysql中的编码都改成了utf8了。period值是30,可以在Console中打印出来的。数据库中的period的格式是int(20)。求解。

------解决方案--------------------
把sql语句打印出来。到数据库执行看下有么有问题。
代码迷推荐解决方案:An exception occurred processing JSP page,http://www.daimami.com/search?q=33144
  相关解决方案