MySQL数据库在使用汉字进行查询的时候容易出错,如查询:
select * from e_document where E_WENHAO like '山大%'
明明在数据库中有相关数据,但是却查询不出结果。这时候需要在建立连接的时候,将连接改为如下形式:
DriverManager.getConnection("jdbc:mysql://localhost/edoas2?user=root&password=&useUnicode=true&characterEncoding=GB2312")
问题解决。但是在tomcat中使用datasource数据源来配置连接,这时候如果将连接数据源中的:
url="jdbc:mysql://192.168.1.6/123paibbs?useUnicode=true&characterEncoding=GBK"
由于tomcat得xml文件解析问题,tomcat启动会产生错误:
Parse Fatal Error at line 373 column 80: The reference to entity "characterEncoding" must end with the ';' delimiter.
这时候需要将url改为如下形式:
url="jdbc:mysql://127.0.0.1:3306/edoas2?useUnicode=true&characterEncoding=GB2312"
。
最后Server.xml配置如下:
<Resource
name="java/mysql"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1:3306/edoas2?useUnicode=true&characterEncoding=GB2312"
password=""
maxActive="20"/>
web.xml配置如:
<resource-ref>
<description>MySQL DB Connection Pool</description>
<res-ref-name>java/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
context.xml配置为:
<ResourceLink
name="java/mysql"
type="javax.sql.DataSource"
global="java/mysql"/>