mysql - JDNI数据库连接问题

查看:96
本文介绍了mysql - 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>

解决方案

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

这篇关于mysql - JDNI数据库连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆