问题是这样的,我在写一个网站,打算使用连接池。我使用J2EE开发,开始使用的是直连的方式,附上代码public class ConnDb {
private String getDriver = "com.mysql.jdbc.Driver";
private String getUrl = "jdbc:mysql://localhost:3306/itwork?useUnicode=true&characterEncoding=utf-8";
private String getName = "root";
private String getpwd = "";
static Connection con;
static Statement stat;
static ResultSet rs =null;
int i =0;
//定义一个方法用于获得Connection
public Connection getConn(){
try {
Class.forName(getDriver);
try {
con = DriverManager.getConnection(getUrl,getName,getpwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动");
}
return con;
}
// public Connection getConn(){
// try{
// Context ctx = new InitialContext();
// DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
// Connection con = ds.getConnection();
// return con;
// }catch(SQLException e){
// e.printStackTrace();
// }catch(NamingException e){
// e.printStackTrace();
// }
// return null;
// }
后来考虑到并发访问,就使用数据库连接池,我在META-INF下建立了context.xml文件
发代码<?xml version="1.0" encoding="UTF-8"?>
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/itwork?autoReconnect=true"/>
请看一下我在上面发的java代码中注释的部分,那是用来使用连接池来获取连接的。
接下来就是我的问题了,我在直连的代码中使用的Url中
jdbc:mysql://localhost:3306/itwork?useUnicode=true&characterEncoding=utf-8,指定了字符集编码,
而我在连接池中无法配置,导致我在使用数据库连接池时,出现中文乱码。
请指导我如何在连接池中指定字符集?
PS:我的数据库和界面以及Servlet中的编码都是UTF-8,都是没有问题的。我使用直连,也是没有乱码的,就是连接池出问题