简体   繁体   English

Tomcat找不到MySQL驱动程序

[英]Tomcat can't find MySQL Driver

working on a JSF application and accessing a page which uses a bean to check some credentials against a database to authenticate a user. 在JSF应用程序上工作并访问一个页面,该页面使用Bean根据数据库检查一些凭证以认证用户。

I'm getting a Tomcat error when the page is loaded. 加载页面时出现Tomcat错误。

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:188)
DB.init(DB.java:27)
Login.<init>(Login.java:19)

Full log here 完整日志在这里

I've downloaded the mysql-connector-java-5.1.24-bin.jar and added it to the build path. 我已经下载了mysql-connector-java-5.1.24-bin.jar并将其添加到构建路径中。 I'm loading my details in form a properties file that looks like this: 我正在以如下所示的属性文件的形式加载我的详细信息:

jdbc.url=jdbc:mysql://127.0.0.1:3306
jdbc.user=root
jdbc.password=
jdbc.driver=com.mysql.jdbc.Driver

I can do an import com.mysql.jdbc.Driver in my Login or DB file with no problems, so I don't understand why the web app is having trouble loading the class. 我可以在我的登录名或数据库文件中完成import com.mysql.jdbc.Driver ,而没有任何问题,因此我不理解为什么Web应用程序无法加载类。

Here's the DB.java file: 这是DB.java文件:

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DB {

private static String url;
private static String username;
private static String password;

public static void init() throws IOException, ClassNotFoundException{

    Properties props = new Properties();
    FileInputStream in = new FileInputStream("/home/dan/workspace/DeutschAkademie/WebContent/db.prop");
    props.load(in);

    String driver = props.getProperty("jdbc.driver");
    url = props.getProperty("jdbc.url");
    username = props.getProperty("jdbc.username");
    password = props.getProperty("jdbc.password");

    if(username == null) username = "";
    if(password == null) password = "";
    Class.forName(driver);

}

public static Connection getConnection () throws SQLException {
    return DriverManager.getConnection(url, username, password);
}

}

My project directory: 我的项目目录:

 .
├── build
│   └── classes
│       ├── DB.class
│       ├── Login.class
│       └── User.class
├── database.sql
├── mysql-connector-java-5.1.24-bin.jar
├── src
│   ├── DB.java
│   ├── Login.java
│   └── User.java
└── WebContent
    ├── add-user.xhtml
    ├── css
    │   ├── bootstrap.min.css
    │   ├── button.css
    │   ├── font-awesome.css
    │   ├── font-awesome-ie7.min.css
    │   ├── font-awesome.min.css
    │   ├── global.css
    │   └── typography.css
    ├── css.xhtml
    ├── db.prop
    ├── font
    ├── history.xhtml
    ├── home.xhtml
    ├── login.xhtml
    ├── META-INF
    │   └── MANIFEST.MF
    ├── navbar.xhtml
    ├── test.xhtml
    ├── WEB-INF
    │   ├── faces-config.xml
    │   ├── inc
    │   ├── lib
    │   │   ├── javax.faces-2.1.9.jar
    │   │   └── mysql-connector-java-5.1.24-bin.jar
    │   └── web.xml
    └── words.xhtml

If you're confident the driver's jar file is already placed on $TOMCAT_HOME/lib or myapp/WEB-INF/lib , other cause of the problem is timing issue. 如果您确定驱动程序的jar文件已经放置在$TOMCAT_HOME/libmyapp/WEB-INF/lib ,则问题的其他原因是计时问题。 Maybe when your init() method runs not all of the classes have been loaded. 也许当您的init()方法运行时,并非所有类都已加载。

To proof this point, can you test loading the driver class at some other time (eg: create a dummy test servlet that loads the driver class). 为了证明这一点,您是否可以在其他时间测试加载驱动程序类(例如:创建一个加载驱动程序类的虚拟测试servlet)。 If it works then it is a timing issue 如果有效,则是时间问题

See this answer: How to configure Tomcat to connect with MySQL 看到这个答案: 如何配置Tomcat与MySQL连接

You are adding the driver to the build path, which is needed for building, but wont help when it comes to runtime. 您正在将驱动程序添加到构建路径中,这是构建所需的,但是对于运行时却无济于事。

You can put it in Tomcat/lib or YourApp/WEB-INF/lib 您可以将其放在Tomcat/libYourApp/WEB-INF/lib

I was also facing the similar issue but on copying the MySql Connector jar file to my lib folder things worked for me. 我也面临类似的问题,但是将MySql Connector jar文件复制到我的lib文件夹后,对我来说一切正常。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM