首页 > jsp使用数据源连接数据库,出现 Cannot create resource instance

jsp使用数据源连接数据库,出现 Cannot create resource instance

错误原因:javax.naming.NamingException: Cannot create resource instance
使用数据源java代码如下:

                    Context ctx = new InitialContext();
                    DataSource ds  = (DataSource)ctx.lookup("java:comp/env/jdbc/BookDB");
                    con = ds.getConnection();

tomcat中 context.xml 配置如下:

<Context reloadable = "ture" >
    <Resource name = "jdbc/BookDB" auth = "Container" type = "javax.sql.DataSource"
        maxActive = "100" maxIdle = "30" maxWait = "10000"
        username = "sa" password = "truman"
        driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url = "jdbc:sqlserver://localhost:1433;DatabaseName=BookDB"/>
</Context>

web.xml 配置如下:

  <web-app>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/BookDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
  </web-app>

之前直接用加载注册驱动的方式,可以成功;对于数据源连接,我已经检查多遍,但实在找不到原因,还请各位大神帮忙看看,实在感激!


你看下这:http://stackoverflow.com/questions/10789683/creating-jndi-java-bean-error

大概说的是将Context标签内容要包含到server.xml中,不知道对你可行不。

【热门文章】
【热门文章】