我已经把mysql得配置文件里的客户端编码和服务器端编码都改成了gbk可还是出现乱码,但是把客户端得编码改成latin1就可以在mysql Client里插入中文,但是在jsp中却不可以向mysql中插入中文。
从网上找了好多解决办法,但是那个都不适于我啊 !
我得JSP程序
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html; charset=gbk" %>
<html>
<head>
<title>使用JSP连接MySQL数据库案例测试</title>
</head>
<body>
<%
Connection con;
Statement stmt;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/person?user=root&password=123&useUnicode=true&characterEncoding=gbk";
con= DriverManager.getConnection(dbUrl);
stmt = con.createStatement();
//stmt.executeUpdate("insert into note values (null,'sadf','dsffd','ddffd')");
stmt.executeUpdate("insert into note values (null,'垃圾','菜鸟','狗屎')");
//关闭数据库连接
stmt.close();
con.close();
%>
</body>
</html>
错误信息
javax.servlet.ServletException: Data truncation: Data too long for column 'title' at row 1
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.zhongwen_jsp._jspService(zhongwen_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'title' at row 1
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3374)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2537)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
org.apache.jsp.zhongwen_jsp._jspService(zhongwen_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
我的mysql配置文档
[client]
port=3306
[mysql]
#default-character-set=latin1
default-character-set=gbk
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"
#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
帮帮忙
谢谢了
--转自