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

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

问题描述

我有一个非常相似的问题,但就我而言,我没有我的构建路径中没有任何重复的 jar,因此该解决方案对我不起作用.我已经在谷歌上搜索了几个小时,但我在那里找到的解决方案都没有真正解决我的问题.我正在为家庭作业创建一个具有一些数据库连接的网站.我正在使用 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 FilesMySQLmysql-connector-java-5.1.12mysql-connector-java-5.1.12-bin.jar.
  3. 在 Eclipse 中打开项目属性.
  4. 将外部 Jar 添加到我的构建路径,然后我选择了 mysql-connector-java-5.1.12-bin.jar.

每次我尝试使用 servlet 时,我都会遇到相同的错误,无论我在那里有 jar 还是没有.你能帮我解决这个问题吗?

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?

推荐答案

至于每一个3rd-party"由 webapp 使用的 JAR 文件风格的库,只需将物理 JAR 文件复制/放到 webapp 的 /WEB-INF/lib 中.然后它将在 webapp 的默认类路径中可用.此外,Eclipse 足够聪明,可以注意到这一点.无需麻烦构建路径.但是,请确保删除您之前添加的所有不必要的引用,否则可能会发生冲突.

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.

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

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