数据库连接池的DataSource ds=(DataSource)ctx.lookup(ds)出错

DataSourceds=(DataSource)ctx.lookup(ds);一直出现Themethodlookup(Name)inthetypeContextisno... 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="gu19901230"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="1000"
maxWait="5000"
url="jdbc:mysql://localhost:3306/store?characterEncoding=GBK"
maxActive="30"/>
</Context>
项目web.xml中配置
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/store</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
就是不知道这是倒是是咋回事,求高手解答
展开
 我来答
张鸣轩名原名张
2010-11-22 · TA获得超过5677个赞
知道大有可为答主
回答量:1337
采纳率:0%
帮助的人:1988万
展开全部
public class DBI {
private static DBI dbi = new DBI();
private static DataSource ds = null;
public static DBI getInstance() {
return dbi;
}
private Connection getConnection() throws SQLException {
String datasource = "java:comp/env/mysqlds";
Connection conn = null;
Enumeration e = null;
try {
if (ds == null) {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup(datasource);
}
} catch (NamingException ne) {
throw new SQLException("Lookup Datasource fail." + ne.getMessage());
}
conn = ds.getConnection();
return conn;
}
}
和你自己的对比下,改改吧
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式