如何在Eclipse Web项目中安装JDBC驱动程序,而不会面临java.lang.ClassNotFoundexception [英] How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception

查看:246
本文介绍了如何在Eclipse Web项目中安装JDBC驱动程序,而不会面临java.lang.ClassNotFoundexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常相似的问题,但在我的情况下,我不我的构建路径中有任何重复的jar,所以解决方案对我来说不起作用。我已经搜索google了几个小时了,但是我找到的解决方案都没有解决我的问题。我正在创建一个具有数据库连接功能的网站进行功课。我正在使用MySQL数据库,在Eclipse中开发并在Windows上运行。

There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.

我不断获得 java.lang.ClassNotFoundException:com.mysql。 jdbc.Driver 使用以下代码:

I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:

import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
    Connection connection = null;
    PreparedStatement query = null;
    try {

        out.println("Create the driver instance.<br>");
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        out.println("Get the connection.<br>");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
        query = connection.prepareStatement( "SELECT * FROM customers");

        //...
    } catch (Exception e)
    {
        out.println(e.toString()+"<br>");
    }
}
//...

当我运行上面的代码我得到以下输出:

When I run the above code I get the following output:

Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

它不通过 Class.forName ... 行,我不知道为什么!这是我做的:

It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:


  1. 下载mysql-connector。

  2. 将其放在我的MySQL文件夹中 C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java -5.1.12-bin.jar

  3. 在Eclipse中打开项目属性。

  4. 将外部Jar添加到我的构建路径,我选择了 mysql-connector-java-5.1.12-bin.jar li>
  1. Download mysql-connector.
  2. Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
  3. Opened the project properties in Eclipse.
  4. Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.

每当我尝试使用servlet,我都会得到相同的错误,无论我在那里有罐子还是我没有。可以帮我弄清楚吗?

Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?

推荐答案

至于JAR文件的每个第三方要由webapp使用,只需在webapp的 / WEB-INF / lib 中复制/删除物理JAR文件。然后,它将在webapp的默认类路径中可用。此外,Eclipse很聪明,可以注意到。不需要用buildpath来麻烦。但是,请确保删除以前添加的所有不必要的引用,否则可能会发生冲突。

As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.

另一种方法是将物理JAR文件放在服务器本身中服务器自己的 / lib 文件夹。当您使用服务器提供的JDBC连接池数据源,而又需要MySQL JDBC驱动程序时,这是必需的。

An alternative is to install it in the server itself by dropping the physical JAR file in server's own /lib folder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.

  • How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
  • How should I connect to JDBC database / datasource in a servlet based application?
  • Where do I have to place the JDBC driver for Tomcat's connection pool?
  • JDBC CLASSPATH Not Working

这篇关于如何在Eclipse Web项目中安装JDBC驱动程序,而不会面临java.lang.ClassNotFoundexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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