连接数据库时出错:无法创建类''用于连接网址' null'

时间:2014-05-31 12:33:39

标签: java mysql jsp jdbc

使用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>

连接到db的类: 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应用程序。任何帮助,将不胜感激。!感谢

2 个答案:

答案 0 :(得分:3)

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

你必须阅读并理解它,不要担心它很容易,我只需要10分钟。

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

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

答案 1 :(得分:2)

您必须将mysql jar文件放在tomcat服务器的lib目录中。

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