tomcat - java.sql.SQLException : No suitable driver 异常

标签 tomcat jdbc datasource

<分区>

我已经创建了一个 ma​​ven 项目。

我正在尝试将 MSSQL (Microsoft SQL Server 2014) 与我的 J2EE 应用程序连接起来。 但它抛出以下异常

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.jagir.ecommerce.database.DatabaseConnection.testConnection(DatabaseConnection.java:27)
        at com.jagir.ecommerce.user.servlet.Registration.doGet(Registration.java:34)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getDriver(DriverManager.java:315)
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(B
asicDataSource.java:1437)
        ... 18 more

我的代码:

网络.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <display-name>ecommerce</display-name>
    <resource-ref>
        <description>Database connection</description>
        <res-ref-name>jdbc/testdb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>  
    </resource-ref>

    <servlet>
        <servlet-name>Registration</servlet-name>
        <servlet-class>com.test.user.servlet.Registration</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Registration</servlet-name>
        <url-pattern>/registration</url-pattern>
    </servlet-mapping>
</web-app>

注册

public class Registration extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public Registration()
    {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {       
        response.getWriter().append("Served at: ").append(request.getContextPath());
        request.setAttribute("title", "User Registration");
        new DatabaseConnection().testConnection();
        request.getRequestDispatcher("pages/user/registration.jsp").forward(request, response);
    }
}

数据库连接

public class DatabaseConnection
{
    private DataSource dataSource;
    private Connection connection;
    private Statement statement;

    public void testConnection()
    {
        //get data source
        try
        {
            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            dataSource  = (DataSource) envContext.lookup("jdbc/testdb");
            connection = dataSource.getConnection();
            statement = connection.createStatement();
            System.out.println("TEST");
            ResultSet rs = statement.executeQuery("select * from temp");
            System.out.println("Done");
            while(rs.next())
            {
                System.out.println(rs.getString(0));
            }
        }
        catch (NamingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/test" docBase="test" crossContext="true"
    reloadable="true" debug="1">
    <!-- Specify a JDBC datasource -->

    <Resource name="jdbc/testdb" auth="Container" type="javax.sql.DataSource"
        username="sa" password="password" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://devmangal;databaseName=testV1.0;integratedSecurity=true"
        maxActive="10" maxIdle="4" />
</Context>

最佳答案

你好,

看来,您为 JVM 使用了错误的驱动程序版本。如果您使用 32 位 JVM(Java 虚拟机),请使用文件夹“x86”中的文件“sqljdbc_auth.dll”,尽管操作系统是 x64。如果您使用带有 x64 处理器的 x64-JVM,请使用文件夹“x64”中的文件“sqljdbc_auth.dll”。

... 无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序 ...

最好的问候 Axel Arnold Bangert - Herzogenrath 2016

关于tomcat - java.sql.SQLException : No suitable driver 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37258897/

相关文章:

java - 从 "Java Webservices: Up and Running"导入项目

java - 使用 Spring Boot 通过 jndi 查找配置两个数据源

java - Tomcat 8 启动多次 [LifecycleException]

Apache在没有tomcat的情况下访问静态html

java - 通过 JDBC 在 Oracle 数据库中创建表

DB写操作中的Java并发

java - 在 preparedStatement 中使用 oracle 的 to_date

node.js - 在 Node.js 中包装来自多个数据源的 API

mysql - 通过 SSIS,无法使用连接成功且位于 ODBC.INI 注册表中的用户 DSN 连接到 mysql DB

java - 使用 spray 服务分层静态内容