数据库连接池的DataSource ds.doc
文本预览下载声明
数据库连接池的DataSource ds=(DataSource)ctx.lookup(ds)出错
浏览次数:325次悬赏分:0 | 提问时间:2010-11-7 14:26 | 提问者:goshinever
DataSource ds=(DataSource)ctx.lookup(ds);一直出现The method lookup(Name) in the type Context is not applicable for the arguments (DataSource)错误。下面是我的java代码和数据源的配置
package store;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DBConn {
private String ds=jdbc/store;
private Connection conn;
public DBConn(){
try{
Context initCtx=new InitialContext();
Context ctx=(Context)initCtx.lookup(java:comp/env);
DataSource ds=(DataSource)ctx.lookup(ds);
conn=ds.getConnection();
}
catch(Exception e){
System.out.print(e.toString());
}
}
public synchronized Statement getStmt() throws Exception{
Statement stmt;
stmt=conn.createStatement();
return stmt;
}
public synchronized PreparedStatement getPstmt(String sql) throws Exception{
PreparedStatement pstmt;
pstmt=conn.prepareStatement(sql);
return pstmt;
}
public synchronized ResultSet query(String sql) throws Exception{
try{
Statement stmt;
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
return rs;
}
catch(Exception e){
System.out.print(e.toString());
return null;
}
}
public synchronized int update(String sql) throws Exception{
int nn;
try{
Statement stmt;
stmt=conn.createStatement();
nn=stmt.executeUpdate(sql);
}
catch(Exception e){
System.out.print(e.toString());
nn=0;
}
return nn;
}
public void DBclose() throws SQLException{
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
if(stmt!=null)
stmt.close();
}
}
tomcat中serve.xml中配置
Context docBase=store path=/store reloadable=true debug=0
Resouce
name=jdbc/store
type=javax.sql.DataSource
password=g driverClassName=com.mysql.jdbc.Driver
maxIdle=1000
maxWait=5000
url=jdbc:mysql://localhost:3306/store?characterEncoding=GBK
maxA
显示全部