连接池已经配置成功但是编译时提示lookup<javax.sql.DataSource>错误怎么解决 20
tomcat与mysql的连接池已经配置好了,可以从数据库里读出数据但是这段数据库连接访问的代码编译时就出错packagestore;importjava.sql.*;i...
tomcat与mysql的连接池已经配置好了,可以从数据库里读出数据但是这段数据库连接访问的代码编译时就出错
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.println(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.println(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();
}}
找不到符号
方法:lookup<javax.sql.DataSource>
javax.naming.Context
DataSource ds=<DataSource>ctx.lookup<ds>
我按照你说的改了代码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/jdbc/store");
DataSource ds=(DataSource)ctx.lookup(ds);
conn=ds.getConnection();
}
这是我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>
还是出现同样的问题
找不到符号 方法 lookup<javax.sql.DataSource>
javax.naming.Context
DataSource ds=<DataSource>ctx.lookup<ds> 展开
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.println(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.println(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();
}}
找不到符号
方法:lookup<javax.sql.DataSource>
javax.naming.Context
DataSource ds=<DataSource>ctx.lookup<ds>
我按照你说的改了代码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/jdbc/store");
DataSource ds=(DataSource)ctx.lookup(ds);
conn=ds.getConnection();
}
这是我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>
还是出现同样的问题
找不到符号 方法 lookup<javax.sql.DataSource>
javax.naming.Context
DataSource ds=<DataSource>ctx.lookup<ds> 展开
1个回答
2010-06-10
展开全部
java:comp/env
这个只是根目录.
正确的配置是tomcat下面的\conf\context.xml
<Resource name="jdbc/aaa" />
配置这样的话就把java:comp/env改成java:comp/env/jdbc/aaa
项目下web.xml中配置
<resource-ref>
<res-ref-name>jdbc/aaa<res-ref-name>
</resource-ref>
这个只是根目录.
正确的配置是tomcat下面的\conf\context.xml
<Resource name="jdbc/aaa" />
配置这样的话就把java:comp/env改成java:comp/env/jdbc/aaa
项目下web.xml中配置
<resource-ref>
<res-ref-name>jdbc/aaa<res-ref-name>
</resource-ref>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询