問題是這樣的,我在寫一個網站,打算使用連接池。我使用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,都是沒有問題的。我使用直連,也是沒有亂碼的,就是連接池出問題