使用应用程序

时间:2015-09-30 16:24:11

标签: java tomcat jndi

我正在尝试使用以下代码为JNDI创建测试应用程序以及测试servlet,以验证连接是否已成功建立。我没有在 server.xml context.xml 上进行任何配置设置,因为我在配置context.xml时不必这样做在应用程序的META-INF中

但是,我总是得到错误" 无法创建类'''用于连接网址' null ' "

我搜索了旧问题,大多数类似错误消息的问题都是由错误的JDBC URL引起的。但就我而言,我并不认为这是JDBC URL的问题。
print stacktrace显示:

  

引起:java.sql.SQLException:没有合适的驱动程序   java.sql.DriverManager.getDriver(DriverManager.java:315)at   org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)     ... 43更多


我甚至尝试在Tomcat的lib目录下使用MySQL jar文件;它仍然是一样的。 完整堆栈跟踪如下:

  

信息:服务器启动时间为1116毫秒   org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法创建JDBC   上课的司机''用于连接网址' null'在   org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)     在   org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)     在   org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)     在   org.angelfrend.samples.StandaloneJNDI.dao.TestServlet.doGet(TestServlet.java:45)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:624)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)     在   org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)     在   org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)     在   org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)     在   org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750)     在   org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720)     在org.apache.jsp.index_jsp._jspService(index_jsp.java:66)at   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)     在   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:745)引起:   java.sql.SQLException:没有合适的驱动程序   java.sql.DriverManager.getDriver(DriverManager.java:315)at   org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)     ... 43更多

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.samples.dao</groupId>
  <artifactId>StandaloneJNDI</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>StandaloneJNDI Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.4</version>
    <scope>provided</scope>
  </dependency>
  </dependencies>
  <build>
    <finalName>StandaloneJNDI</finalName>
  </build>

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
      version="2.4">
  <display-name>Servlet 2.4 Web Application</display-name>
  <description>MySQL Test</description>  
  <servlet>
    <servlet-name>TestServlet</servlet-name>    
    <servlet-class>org.samples.TestServlet</servlet-class>
  </servlet>   
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>  
  <resource-ref>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>  
  </resource-ref>

context.xml(放在src / main / resources的META-INF文件夹中)

<Context>
        <Resource   name="jdbc/TestDB" 
        auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="user" password="pass" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/JavaTest" /></Context>       

有人可以帮助我了解它的工作原理吗? 我们不能在不配置server.xml或context.xml的情况下创建JNDI连接吗?

0 个答案:

没有答案