tomcat、mysql、程序编码问题

arthur.dy.lee 2009-08-22 05:14:46
Tomcat提示错误:
信息: Server startup in 1142 ms
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown initial character set index '45' received from server. Initial client character set can be forced via the 'characterEncoding' property.)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:67)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Unknown initial character set index '45' received from server. Initial client character set can be forced via the 'characterEncoding' property.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:2345)
at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3913)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2683)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
... 20 more

------------------------
多网上查了一下,是程序编码 、 mysql编码 、web服务器编码不一致的问题。我修改了三方编码,如下:
Mysql:

mysql> show variables like 'character_set_%';
+--------------------------+----------------------------------------------------
-----+
| Variable_name | Value
|
+--------------------------+----------------------------------------------------
-----+
| character_set_client | utf8
|
| character_set_connection | utf8
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | utf8
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | D:\Program Files\MySQL\MySQL Server 6.0\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)

========

mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)


Tomcat 修改 server.xml如下:


<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>




============

web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>



index.jsp:

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page session="false"%>


<%
out.print("my test start");
DataSource ds=null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn=ds.getConnection();
Statement st=conn.createStatement();

String strSql="select * from users";
ResultSet rs=st.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1));
}
out.print("my test end");
}catch(Exception ex){
out.print("error happen attention:"+ex.getMessage());
ex.printStackTrace();
}

%>


========================================
========================================

我想做一个连接池,弄了一天多了,还没搞好,太郁闷了。实是找不出错误了,各位大虾有劳了,鞠躬。

我从网上看Tomcat6 mysql连接池配置,只用修改context.xml就行,现修改如下:
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
username="root"
password="pain"
maxActive="100"
maxIdle="30"
maxWait="10000"
disableUploadTimeout="true"
URIEncoding="UTF8"/>
应该没有问题吧。
...全文
1755 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang_yu_QIN 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 paincupid 的回复:]
因为报错,所以才在server.xml中加入URIEncoding="UTF-8"的

characterEncoding 应该改哪里?
[/Quote]

寫個過濾器 在web。xml配置下就OK了。
arthur.dy.lee 2009-08-23
  • 打赏
  • 举报
回复
因为报错,所以才在server.xml中加入URIEncoding="UTF-8"的

characterEncoding 应该改哪里?
ouyangyh 2009-08-23
  • 打赏
  • 举报
回复
characterEncoding
应该是字符编码的问题吧
closewbq 2009-08-23
  • 打赏
  • 举报
回复
先从server.xml中去掉URIEncoding="UTF-8"试验下。
xuexijava 2009-08-23
  • 打赏
  • 举报
回复
迷糊。。。。
帮顶
Aishangwan 2009-08-22
  • 打赏
  • 举报
回复
顶一个!看得挺乱的。
arthur.dy.lee 2009-08-22
  • 打赏
  • 举报
回复
我修改了程序编码 、 mysql编码 、web服务器编码,都设成了UTF-8,为何还是报这种错误呢?
arthur.dy.lee 2009-08-22
  • 打赏
  • 举报
回复
我修改了程序编码 、 mysql编码 、web服务器编码,都设成了UTF-8,为何还是报这种错误呢?
arthur.dy.lee 2009-08-22
  • 打赏
  • 举报
回复
我修改了程序编码 、 mysql编码 、web服务器编码,都设成了UTF-8,为何还是报这种错误呢?
xiaozejun 2009-08-22
  • 打赏
  • 举报
回复
楼主最下面的这段连接池是没有问题的 其他的就不知道啦 期待高手解决

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧