java.sql.SQLException:没有合适的驱动程序 [英] java.sql.SQLException: No suitable driver

查看:89
本文介绍了java.sql.SQLException:没有合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 maven 项目.

I have create one maven project.

我尝试将MSSQL(Microsoft SQL Server 2014)与我的J2EE应用程序连接. 但它引发以下异常

I am try to connect MSSQL (Microsoft SQL Server 2014) with my J2EE application. But it throws following exception

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

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

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>

推荐答案

尝试一下,

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

  1. Manually try to place your context.xml file under

$ {CATALINA_HOME}/conf/Catalina/localhost/context.xml

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

Rename this context.xml to {app-name}.xml

这篇关于java.sql.SQLException:没有合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆