JDNI数据库连接问题

通过jndi连接自己的数据库,非全局配置,但一直报空指针异常,代码贴下来,请大神看看哪里错了
java代码,第一个是获取连接,第二个获取返回结果,代码运行在ps = conn.prepareStatement(sql)这里报异常,表名字没错,参数也没错,语句也没错,快奔溃了~

Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:com/env/mysqlDataSource");
return ds.getConnection();
PreparedStatement ps = null;
        ResultSet rs = null;
        if(conn==null){
            conn = ConnectionInfoBean.getConnection();
        }
        String sql = "SELECT "+get+" FROM "+table;
        System.out.println(sql);
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

web.xml配置

<resource-ref>
        <description>MySQL DB Connection</description>
        <res-ref-name>mysqlDataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

tomcat 配置,项目叫bynn ,所以在localhost的xml名叫bynn.xml,配置如下,名字和密码为安全起见没写

<?xml version="1.0" encoding="UTF-8"?>
 <Context>
        <Resource name="mysqlDataSource" auth="Container"
                         type="javax.sql.DataSource"
                         maxActive="100" maxIdle="10" maxWait="10000"
                         driverClassName="com.mysql.jdbc.Driver"
                         url="jdbc:mysql://localhost:3306/bynn"
                         username="" password="" />
        <WatchedResource>WEB-INF/web.xml</WatchedResource>        
</Context>
阅读 2.4k
2 个回答

问问题都不贴异常信息的?

(DataSource)ctx.lookup("java:com/env/mysqlDataSource"); com 少了 一个P 应该为comp

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题