尝试在tomcat中执行数据源JNDI查找时获取空指针异常

时间:2013-03-14 10:18:14

标签: java java-ee

我尝试通过DatabaseSource JNDI查找建立数据库连接。我按照Tomcat网站上的说明操作,如下所示

我将资源添加到server.xml

 <Resource name="jdbc/myDB" auth="Container"
              type="javax.sql.DataSource"/> 
<ResourceParams name="jdbc/myDB">
  <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
  </parameter>
  <parameter>
    <name>driverClassName</name>
    <value>oracle.jdbc.OracleDriver</value>
  </parameter>
  <parameter>
    <name>url</name>
    <value>jdbc:oracle:thin:@111.111.111.111:1234:DBNME</value>
  </parameter>
  <parameter>
    <name>username</name>
    <value>Dobby</value>
  </parameter>
  <parameter>
    <name>password</name>
    <value>pwd</value>
  </parameter>
  <parameter>
    <name>maxActive</name>
    <value>20</value>
  </parameter>
  <parameter>
    <name>maxIdle</name>
    <value>10</value>
  </parameter>
  <parameter>
    <name>maxWait</name>
    <value>-1</value>
  </parameter>
</ResourceParams>

的web.xml

 <resource-ref>
    <description>MY DataBase</description>
    <res-ref-name>jdbc/myDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

代码

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

我收到此错误

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at com.sample.XML.doGet(XML.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source)
    at java.sql.DriverManager.getDriver(Unknown Source)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    ... 21 more

我的tomcat lib中有OJDBC14.jar。

我在stackoverflow中发现了同样的问题,但那里提供的解决方案对我不起作用。这就是为什么我要再次发布它。

2 个答案:

答案 0 :(得分:1)

尝试:

datasource = (DataSource) initialContext.lookup( "java:/comp/env/jdbc/myDB" );

当资源名称为jdbc/myDB时。

答案 1 :(得分:0)

你可以试试把xml

   <resource-ref>     
    <description>     
     </description>     
    <res-ref-name></res-ref-name>     
    <res-type></res-type>     
    <res-auth></res-auth>     
  </resource-ref>
tomcat Server.xml中的

标记,并重新启动服务器并查看是否能解决您的问题?