java JDBC 驱动程序 - ClassNotFoundException、NetBeans

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25120200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:23:12  来源:igfitidea点击:

JDBC driver - ClassNotFoundException, NetBeans

javanetbeansjdbcdatabase-connection

提问by kiedysktos

I've searched a lot and spent many time trying register JDBC driver.

我搜索了很多并花了很多时间尝试注册 JDBC 驱动程序。

First, I copied my ojdbc7.jar file (downloaded from Oracle) into directory shown below:

首先,我将我的 ojdbc7.jar 文件(从 Oracle 下载)复制到如下所示的目录中:

Driver File(s): /Users/Kamil/glassfish4/jdk7/jre/lib/ext/ojdbc7.jar
Driver Class: oracle.jdbc.OracleDriver
// this is copied from Services/Databases/Drivers/ojdbc

Then, I tried following code:

然后,我尝试了以下代码:

try {
    Driver myDriver = new oracle.jdbc.driver.OracleDriver();
    DriverManager.registerDriver(myDriver);
} catch (ClassNotFoundException ex) {
    System.out.println("Error: unable to load driver class!");
    System.exit(1);
}

... and this one:

... 还有这个:

try {
    Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException ex) {
    System.out.println("Error: unable to load driver class!");
    System.exit(1);
}

... and with this line instead:

...而用这一行代替:

Class.forName("oracle.jdbc.driver.OracleDriver");

I always get ClassNotFoundException :(

我总是得到 ClassNotFoundException :(

Here is code I try to run:

这是我尝试运行的代码:

Connection DBconn;
String USER = "root";
String PASS = "root";
System.out.println("Connecting to database...");

DBconn = DriverManager.getConnection("mysql://localhost:3306/RestToolDatabase", USER, PASS);

System.out.println("Creating statement...");
Statement stmt = DBconn.createStatement();
String sql;
sql = "select surname, id, age\n"
        + "from customers \n"
        + "where name = \"maria\" \n"
        + "order by id;";
ResultSet rs = stmt.executeQuery(sql);

I've also read about setting the classpath as described here:

我也看到了有关所描述的设置类路径的位置

  • Right-click your Project.
  • Select Properties.
  • On the left-hand side click Libraries.
  • Under Compile tab - click Add Jar/Folder button.

    but there is no "Properties/Libraries" option in NetBeans...

  • 右键单击您的项目。
  • 选择属性。
  • 在左侧单击库。
  • 在 Compile 选项卡下 - 单击 Add Jar/Folder 按钮。

    但是 NetBeans 中没有“属性/库”选项...

I use Maven and there is following dependency added by some library:

我使用 Maven,并且某些库添加了以下依赖项:

<dependency>
    <groupId>ojdbc</groupId>
    <artifactId>ojdbc</artifactId>
    <version>14</version>
    <type>pom</type>
</dependency>

Maybe there is some workaround, or other way to add it automatically? It SHOULD be simple but I'm unexperienced and wasted many time on this. Please help.

也许有一些解决方法,或其他方式来自动添加它?它应该很简单,但我没有经验并且在这上面浪费了很多时间。请帮忙。

EDIT: Thank you for replies, yes, I use MySQL Server at localhost:3306 [root]. I have MySQL JDBC connector installed here:

编辑:感谢您的回复,是的,我在 localhost:3306 [root] 上使用 MySQL 服务器。我在这里安装了 MySQL JDBC 连接器:

/Applications/NetBeans/NetBeans 8.0.app/Contents/Resources/NetBeans/ide/modules/ext/mysql-connector-java-5.1.23-bin.jar

When I go to "Services" --> "Drivers" --> "MySQL (Connector/J driver)" there is Driver Class path exactly as you suggested, so I use Class.forName("com.mysql.jdbc.Driver") now. I right-clicked on "MySQL (Connector/J driver)" driver and went to "Connect Using..." --> "localhost, port 3306, user, password". And it is connected now, I see new connection. But still I get ClassNotFoundException.

当我转到“服务”-->“驱动程序”-->“MySQL(连接器/J 驱动程序)”时,驱动程序类路径与您建议的完全相同,因此我使用 Class.forName("com.mysql.jdbc.Driver “) 现在。我右键单击“MySQL(连接器/J 驱动程序)”驱动程序并转到“使用连接...”-->“本地主机,端口 3306,用户,密码”。现在它已连接,我看到新的连接。但我仍然得到 ClassNotFoundException。

EDIT 2 - this solution worked for me:

编辑 2 - 这个解决方案对我有用:

I added following to dependencies in pom.xml:

我在 pom.xml 中的依赖项中添加了以下内容:

    </dependency>
        <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.23</version>
</dependency>

... and builded application; the driver was downloaded and installed. As simple as that... I spent many time on this... It works - yeah! :)

...和构建的应用程序;驱动程序已下载并安装。就这么简单......我花了很多时间在这上面......它有效 - 是的!:)

回答by Zaid Malhis

You are using an Oracle JDBC driver on a MySQL database, you should use

您在 MySQL 数据库上使用 Oracle JDBC 驱动程序,您应该使用

    Class.forName("com.mysql.jdbc.Driver");

Edit: Thanks to the comments from @duffymo and @Mark-Rotteveel who noticed that the URL of the connection is also wrong, the correct connection is:

编辑:感谢@duffymo 和@Mark-Rotteveel 的评论,他们注意到连接的 URL 也是错误的,正确的连接是:

    Connection DBconn;
    String USER = "root";
    String PASS = "root";
    DBconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/RestToolDatabase?" + "user="+USER+"&password="+PASS);

回答by Fhamtaom

Setting the classpath :

设置类路径:

  1. Download the JAR JDBC Connector/Driver
  2. Copy file in subfolder of project (ex: libs) (not 'src')
  3. Project->Properties->Libraries->Add Jar/Folder->Select file
  1. 下载 JAR JDBC 连接器/驱动程序
  2. 复制项目子文件夹中的文件(例如:libs)(不是“src”)
  3. 项目->属性->库->添加Jar/文件夹->选择文件

Andpackaging dependent librairies with relative path in 'dist' :

在 'dist' 中使用相对路径打包依赖库:

  1. Project->Build->Packaging->Check "Copy Dependent Librairies"
  1. 项目->构建->打包->勾选“复制依赖库”

回答by Jimmy Oku

</dependency>
    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>

This worked for me. Since there's no Property and Library in Netbeans apache. Good luck.

这对我有用。由于 Netbeans apache 中没有属性和库。祝你好运。

回答by duffymo

I don't think the JDBC JAR belongs in that directory. The CNF exception backs me up.

我认为 JDBC JAR 不属于该目录。CNF 异常支持我。

I would recommend putting that JAR in your project WEB-INF/lib, repackaging the WAR, and trying again.

我建议将该 JAR 放在您的项目 WEB-INF/lib 中,重新打包 WAR,然后重试。

This line is correct:

这一行是正确的:

Class.forName("oracle.jdbc.driver.OracleDriver");

The full class name has to match that in the JAR. This is the only one that does.

完整的类名必须与 JAR 中的匹配。这是唯一一个这样做的。

You have to use the driver that matches your database: Oracle for Oracle, MySQL for MySQL. What database are you using?

您必须使用与您的数据库匹配的驱动程序:Oracle for Oracle、MySQL for MySQL。你用的是什么数据库?