连接数据库时出错:无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序

Posted

技术标签:

【中文标题】连接数据库时出错:无法为连接 URL \'null\' 创建类 \'\' 的 JDBC 驱动程序【英文标题】:Error connecting database: Cannot create JDBC driver of class '' for connect URL 'null'连接数据库时出错:无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序 【发布时间】:2014-07-21 03:08:16 【问题描述】:

使用 java 连接数据库时出现以下错误:

type Exception report

message java.lang.RuntimeException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.RuntimeException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.RuntimeException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    com.skf.support.setup.data.database.DBConnectionFactory.getConnection(DBConnectionFactory.java:32)
    com.skf.support.setup.data.user.UserDao.checkUser(UserDao.java:25)
    com.skf.support.setup.data.user.UserDao.checkUser(UserDao.java:54)
    com.skf.support.setup.data.login.LoginFormValidator.validate(LoginFormValidator.java:28)
    org.apache.jsp.modules.login.doLogin_jsp._jspService(doLogin_jsp.java:132)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL '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)
    com.skf.support.setup.data.database.DBConnectionFactory.getConnection(DBConnectionFactory.java:23)
    com.skf.support.setup.data.user.UserDao.checkUser(UserDao.java:25)
    com.skf.support.setup.data.user.UserDao.checkUser(UserDao.java:54)
    com.skf.support.setup.data.login.LoginFormValidator.validate(LoginFormValidator.java:28)
    org.apache.jsp.modules.login.doLogin_jsp._jspService(doLogin_jsp.java:132)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.sql.SQLException: No suitable driver
    java.sql.DriverManager.getDriver(DriverManager.java:315)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    com.skf.support.setup.data.database.DBConnectionFactory.getConnection(DBConnectionFactory.java:23)
    com.skf.support.setup.data.user.UserDao.checkUser(UserDao.java:25)
    com.skf.support.setup.data.user.UserDao.checkUser(UserDao.java:54)
    com.skf.support.setup.data.login.LoginFormValidator.validate(LoginFormValidator.java:28)
    org.apache.jsp.modules.login.doLogin_jsp._jspService(doLogin_jsp.java:132)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

我正在使用 phpmyadmin,我的数据库名称是:sfsupportconnectionpool

这是我的 web.xml

  <resource-ref>
    <description>Resource reference to a factory for java.sql.Connection instances 
        that may be used for talking to a particular database 
        that is configured in the Context
        configuration for the web application.</description>
    <res-ref-name>jdbc/SFSupportConnectionPool</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/SFSupportConnectionPool">
    <Resource 
        name="jdbc/SFSupportConnectionPool" 
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="100"
        maxIdle="30"
        maxWait="10000"
        username="dbadmin" password="12345"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/sf_support?autoReconnect=true" 
        connectionProperties="removeAbandoned=true; removeAbandonedTimeout=60;"/>
</Context>

连接数据库的类:DBConnection.java

package com.skf.support.setup.data.database;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnectionFactory
    private static String dbName = "SFSupportConnectionPool";

    public static Connection getConnection()
        Context initContext;
        try
            initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource) envContext.lookup("jdbc/" + DBConnectionFactory.dbName);
            Connection conn = ds.getConnection();
            return conn;
        
        catch (NamingException e)
            e.printStackTrace();
            throw new RuntimeException(e);
        
        catch (SQLException e)
            e.printStackTrace();
            throw new RuntimeException(e);
        
    

我尝试了很多方法,但对我来说不起作用。谁能建议我这里可能出了什么问题?我自己没有创建项目,我只是想将数据库连接到 jsp 应用程序。任何帮助,将不胜感激。!谢谢

【问题讨论】:

【参考方案1】:

你必须把mysql的jar文件放在tomcat服务器的lib目录下。

(例如:C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.3\lib)

【讨论】:

我以某种方式解决了这个问题。这是tomcat的问题,实际上都是删除并重新安装的事情。这些事情我都做得很好。【参考方案2】:

我测试了本教程,它完美运行http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

你必须阅读并理解它,别担心它很容易,我只花了10分钟就完成了。

这是我的测试项目,我很确定它会起作用。

https://www.dropbox.com/s/xx5i3zip5ohmls9/testt.zip

【讨论】:

也许我在使用 Eclipse 导入它时犯了一个错误,因为我的设置让我很困惑。不过感谢您的帮助。

以上是关于连接数据库时出错:无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

创建连接时出错“error null”

Cloudbees、Tomcat 和 Spring:“无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序”

Tomcat、MySQL -> 无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序

java.sql.SQLException:无法在 IntelliJ Web 应用程序中为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序

从 localhost 开发站点时更改了 WordPress 地址 (URL) 和站点地址 (URL)。现在,建立数据库连接时出错

wordpress 建立数据库连接时出错,页面无法显示