Class.forName(“oracle.jdbc.OracleDriver”); 生成异常:java.lang.ClassNotFoundException

huangapple 未分类评论42阅读模式
英文:

Class.forName("oracle.jdbc.OracleDriver"); generates exception: java.lang.ClassNotFoundException

问题

我正在尝试创建一个连接到Oracle数据库的程序,其终极目标是为了我正在学习的课程创建一些表格并在这些表格上运行命令。我目前正在尝试使课堂上提供的示例代码工作,但是我无法使其正常工作。生成错误 "java.lang.ClassNotFoundException" 的代码是当我的主程序执行以下代码时:

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

或者

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

我已经手动将 ojdbc6.jar、ojdbc8.jar 和 ojdbc14.jar 文件添加到我“PATH”系统变量中的每个文件夹中,但我仍然得到以下异常:

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

或者

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

我不知道该如何修复这个问题或者需要进行哪些修改。毕竟这是一个课程示例... 欢迎提出任何建议。

谢谢您的时间。

英文:

I'm trying to make a program that connects to an Oracle database for the ultimate purpose of creating a few tables and running commands on them for a course I am taking. I'm currently trying to make the example given in class work but I can't get that to work. The code that generates the error "java.lang.ClassNotFoundException" is when my main hits the code:

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

or

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

I have manually added the ojdbc6.jar, ojdbc8.jar and ojdbc14.jar files to each folder in my "PATH" system variable and I'm getting the exception:

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

or 

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

I have no idea how to fix this or what to modify. It is a course example after all...
Any/All suggestions are welcome.

Thank you for your time

答案1

得分: -1

希望这对你有所帮助?

我认为在最新版本中不需要使用 Class.forName。

我会像下面这样做,并将 connection、url、user 和 password 设置为全局 final 变量,以避免重复,并在需要的地方调用该方法。

public Connection getConnection() throws SQLException {
    return connection = DriverManager.getConnection(url, user, password);
}
英文:

I hope this might help?

I don't think you need Class.forName with latest version.

I would have done something like below and set connection,url,user and password as global final variables to avoid repeating yourself and call the method where you need it.

 public Connection getConnection() throws SQLException {
    return connection = DriverManager.getConnection(url, user, password);

}

huangapple
  • 本文由 发表于 2020年4月5日 01:54:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61032365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定