主题:中文乱码问题
html文件:
<html>
<head>
<title>CH14 - Mysql.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
<h2>将信息存入 Mysql 中</h2>
<form name="form" action="Mysql.jsp" method="post" >
<p>姓:<input name="last_name" type="text" id="last_name"></p>
<p>名:<input name="first_name" type="text" id="first_name"></p>
<p>
<input type="submit" value="传送">
<input type="reset" value="取消">
</p>
</form>
</body>
</html>
jsp文件:
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>CH14 - Mysql.jsp</title>
</head>
<body>
<h2>将信息存入 Mysql 中</h2>
<%
Connection con = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
request.setCharacterEncoding("GB2312");
String employee_id = null;
String last_name = request.getParameter("last_name");
String first_name = request.getParameter("first_name");
String birth = "1978/12/11";
String sex = "M";
String email = "ihateyou@love.com";
String new_last_name = "";
String new_first_name = "";
%>
从 mysql.html 接收到的信息如下:<br>
姓:<%= last_name %>
名:<%= first_name %><br><br>
<%
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db?user=root&password=1234&useUnicode=true&characterEncoding=GB2312");
stmt = con.createStatement();
String upd = "INSERT INTO employee(employee_id, last_name, first_name, birth, sex, email) VALUES ("+employee_id+", '"+last_name+"', '"+first_name+"', '"+birth+"', '"+sex+"', '"+email+"')";
stmt.executeUpdate(upd);
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT * FROM employee";
rs = stmt1.executeQuery(query);
rs.last( );
new_last_name = rs.getString("last_name");
new_first_name = rs.getString("first_name");
stmt.close();
stmt1.close();
con.close();
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
finally
{
try {
if(con != null)
{
con.close();
}
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
}
%>
从 employee 取出最新新增的姓名:<br>
新增姓名:<%= new_last_name+new_first_name %><br>
</body>
</html>
为什么无法把中文写入数据库呢?
报告的错误是:sqle=com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'last_name' at row 1 从 employee!
大家帮忙看看,还有mysql中的文字设定也是gb2312.
<html>
<head>
<title>CH14 - Mysql.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
<h2>将信息存入 Mysql 中</h2>
<form name="form" action="Mysql.jsp" method="post" >
<p>姓:<input name="last_name" type="text" id="last_name"></p>
<p>名:<input name="first_name" type="text" id="first_name"></p>
<p>
<input type="submit" value="传送">
<input type="reset" value="取消">
</p>
</form>
</body>
</html>
jsp文件:
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>CH14 - Mysql.jsp</title>
</head>
<body>
<h2>将信息存入 Mysql 中</h2>
<%
Connection con = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
request.setCharacterEncoding("GB2312");
String employee_id = null;
String last_name = request.getParameter("last_name");
String first_name = request.getParameter("first_name");
String birth = "1978/12/11";
String sex = "M";
String email = "ihateyou@love.com";
String new_last_name = "";
String new_first_name = "";
%>
从 mysql.html 接收到的信息如下:<br>
姓:<%= last_name %>
名:<%= first_name %><br><br>
<%
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample_db?user=root&password=1234&useUnicode=true&characterEncoding=GB2312");
stmt = con.createStatement();
String upd = "INSERT INTO employee(employee_id, last_name, first_name, birth, sex, email) VALUES ("+employee_id+", '"+last_name+"', '"+first_name+"', '"+birth+"', '"+sex+"', '"+email+"')";
stmt.executeUpdate(upd);
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT * FROM employee";
rs = stmt1.executeQuery(query);
rs.last( );
new_last_name = rs.getString("last_name");
new_first_name = rs.getString("first_name");
stmt.close();
stmt1.close();
con.close();
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
finally
{
try {
if(con != null)
{
con.close();
}
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
}
%>
从 employee 取出最新新增的姓名:<br>
新增姓名:<%= new_last_name+new_first_name %><br>
</body>
</html>
为什么无法把中文写入数据库呢?
报告的错误是:sqle=com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'last_name' at row 1 从 employee!
大家帮忙看看,还有mysql中的文字设定也是gb2312.