java.sql.SQLException:没有合适的驱动程序

时间:2016-05-16 16:37:21

标签: tomcat jdbc datasource

我创建了一个 maven 项目。

我尝试将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

我的代码:

的web.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);
    }
}

的DatabaseConnection

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();
        }

    }
}

context.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>

2 个答案:

答案 0 :(得分:0)

<强>您好,

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

...无法为连接URL'null'...

创建类''的JDBC驱动程序

祝你好运 Axel Arnold Bangert - Herzogenrath 2016

答案 1 :(得分:0)

试试这个,

  1. 手动尝试将context.xml文件放在

    <强> $ {CATALINA_HOME} /conf/Catalina/localhost/context.xml

  2. 将此context.xml重命名为{app-name} .xml