Netbeans 与 Oracle 连接 java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1311418/
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-10-29 16:05:35  来源:igfitidea点击:

Netbeans with Oracle connection java.lang.ClassNotFoundException

javanetbeansjdbcnetbeans6.5

提问by Attilah

I use NetBeans 6.5.

我使用 NetBeans 6.5。

When I try to run the following code:

当我尝试运行以下代码时:

package com.afrikbrain.numeroteur16;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 
*/

public class NumeroteurTest {

  public NumeroteurTest() {

  }

  public void doIt() throws ClassNotFoundException{
    try {

      Class.forName("oracle.jdbc.OracleDriver");
      Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","user","pwd");
      String newNUMERO = new Numeroteur16("MATCLI", connection).numeroter();
      System.out.println("NUMERO GENERE : "+newNUMERO.toString());
    }
    catch (SQLException ex) {
            Logger.getLogger(NumeroteurTest.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
    }
    catch (NumException ex) {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }

  }

  public static void main(String[] args){
        try {
            new NumeroteurTest().doIt();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NumeroteurTest.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Driver not found.");
        }
  }
}

I get this error:

我收到此错误:

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
        at java.net.URLClassLoader.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at com.afrikbrain.numeroteur16.NumeroteurTest.doIt(NumeroteurTest.java:27)
        at com.afrikbrain.numeroteur16.NumeroteurTest.main(NumeroteurTest.java:45)
Driver not found.

How do I solve this problem?

我该如何解决这个问题?

采纳答案by beetstra

The problem: Java can't find the JDBC Driver Class.
Solution: Add the Oracle JDBC Driver to your classpath.
You can get it at http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html

问题:Java 找不到 JDBC 驱动程序类。
解决方案:将 Oracle JDBC 驱动程序添加到您的类路径中。
您可以在http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html获得它

Start java with java -classpath ojdbc14.jar ...to include the downloaded jar in your classpath.

启动 javajava -classpath ojdbc14.jar ...以将下载的 jar 包含在您的类路径中。

回答by acidb1

Add ojdbc6.jarto the project libraries. First, create a new library (NetBeans):

添加ojdbc6.jar到项目库。首先,创建一个新库 (NetBeans):

  • NetBeans -> Tools -> Libraries -> New library(Use a descriptive name eg: OracleJDBC6.)
  • Click OK, then Add JAR/Folder.
  • Type %ORACLE_HOME%\jdbc\lib\ojdbc6.jar, then confirm. On my computer, ORACLE_HOME=c:\app\admin\product\11.2.0\dbhome_1.
  • NetBeans -> 工具 -> 库 -> 新库(使用描述性名称,例如:OracleJDBC6。)
  • 单击OK,然后单击Add JAR/Folder
  • 键入%ORACLE_HOME%\jdbc\lib\ojdbc6.jar,然后确认。在我的电脑上,ORACLE_HOME=c:\app\admin\product\11.2.0\dbhome_1.

Finally, add the library to the project: Right-click on Libraries, , select Add libraryand select the library previously added.

最后,将库添加到项目中:右键单击Libraries, ,选择Add library并选择之前添加的库。

回答by Eric Darchis

Make sure that the Oracle driver is in the classpath. The thin driver is in ojdbc14.jar.

确保 Oracle 驱动程序在类路径中。瘦驱动程序位于 ojdbc14.jar 中。