主题:JSP向MYSQL中插入数据问题
当文本框输入汉字时,出现错误:Data truncation: Data too long for column 'name' at row 1。
<%@page contentType="text/html;charset=GB2312" import="java.sql.*"%>
<%@page import="java.util.*"%>
<html>
<head>
<title>mysql.jsp</title>
</head>
<body>
将信息存入MYQL
<%
Connection con = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
request.setCharacterEncoding("GB2312");
String name = request.getParameter("name");
String age = request.getParameter("age");
String new_name = "";
String new_age = "";
%>
<br>接受的的信息:姓名<%=name%>年龄<%=age%><br><br>
<%
try {
Class.forName("org.gjt.mm.mysql.Driver");
String db = "jdbc:mysql://127.0.0.1:3306/stu?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
con = DriverManager.getConnection(db);
stmt=con.createStatement();
String sql="insert into stu(name,age) values('"+name+"','"+age+"')";
stmt.executeUpdate(sql);
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query="select * from stu";
rs = stmt1.executeQuery(query);
rs.last();
new_name = rs.getString("name");
new_age = rs.getString("age");
stmt.close();
stmt1.close();
con.close();
}
catch(Exception e){
out.println(e.getMessage());
}
%>
新增的信息:姓名<%=new_name%>年龄<%=new_age%>
<%@page contentType="text/html;charset=GB2312" import="java.sql.*"%>
<%@page import="java.util.*"%>
<html>
<head>
<title>mysql.jsp</title>
</head>
<body>
将信息存入MYQL
<%
Connection con = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
request.setCharacterEncoding("GB2312");
String name = request.getParameter("name");
String age = request.getParameter("age");
String new_name = "";
String new_age = "";
%>
<br>接受的的信息:姓名<%=name%>年龄<%=age%><br><br>
<%
try {
Class.forName("org.gjt.mm.mysql.Driver");
String db = "jdbc:mysql://127.0.0.1:3306/stu?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
con = DriverManager.getConnection(db);
stmt=con.createStatement();
String sql="insert into stu(name,age) values('"+name+"','"+age+"')";
stmt.executeUpdate(sql);
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query="select * from stu";
rs = stmt1.executeQuery(query);
rs.last();
new_name = rs.getString("name");
new_age = rs.getString("age");
stmt.close();
stmt1.close();
con.close();
}
catch(Exception e){
out.println(e.getMessage());
}
%>
新增的信息:姓名<%=new_name%>年龄<%=new_age%>