java 找不到适用于 Oracle 数据库连接的驱动程序

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

No suitable driver found for Oracle Database connection

javajdbcoracle11gdriversqlexception

提问by Ravi

I have small Java Application, which execute every day and checks for data in database using Cronj Schedular and everything works fine, but recently I have observed that, it is failing due to

我有一个小的 Java 应用程序,它每天执行并使用 Cronj Schedular 检查数据库中的数据,一切正常,但最近我观察到,由于

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.110.xx.xxx:1521/test

At the same time, when I run my test code to check Database connectivity that works fine without above exception. I'm unable to figure it out. Although, there was just slight code change, but that was nowhere related to Database or Database connection. Anyone, help me on this ?

同时,当我运行我的测试代码来检查数据库连接时,没有上述异常就可以正常工作。我无法弄清楚。虽然,只是轻微的代码更改,但这与数据库或数据库连接无关。任何人,帮我解决这个问题?

dbconf.java

数据库配置文件

public class dbconf {

    private Connection connect;
    private String connstr;

    public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

        try {
                String uname = "scott";
                String pass = "tiger";
                Class.forName("oracle.jdbc.OracleDriver").newInstance();
                connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }

            return connect;
    }
}

I'm using ojdbc6.jarand Oracle11g

我正在使用ojdbc6.jarOracle11g

Edited - Application Log file

编辑 - 应用程序日志文件

Wed Jul 01 09:25:17 IST 2015:------- Initializing -------------------
Wed Jul 01 09:25:17 IST 2015:------- Scheduling Jobs ----------------
Wed Jul 01 09:25:17 IST 2015:------- Job Started Running ----------------
Thu Jul 02 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test
Sat Jul 04 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
Sun Jul 05 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test

So, you can see, It failed on 3rd and 6th July. But, in between it ran fine.

所以,你可以看到,它在 7 月 3 日和 6 日失败了。但是,在两者之间它运行良好。

==Update 1==

==更新1==

It seems, nobody is reading my question properly, I have clearly mentioned that, It is running fine for someday, but someday it is failing. If it was classpath issue, then It shouldn't have ran any day.

似乎没有人正确阅读我的问题,我已经清楚地提到过,有一天它运行良好,但有一天它会失败。如果是类路径问题,那么它不应该在任何一天运行。

===Update 2===

===更新2===

Many of below answers were pointless, but few were having some logical view. I have used printStracktraceand tried to debug each point and finally I got some clue. 3 Days back, I deployed new version of application on the same server (included printStackTrace and SysOut), First 2 days It ran fine, today it Failed with following error.

下面的许多答案都是毫无意义的,但很少有人有一些合乎逻辑的观点。我已经使用printStracktrace并尝试调试每个点,最后我得到了一些线索。3 天前,我在同一台服务器上部署了新版本的应用程序(包括 printStackTrace 和 SysOut),前 2 天它运行良好,今天它因以下错误而失败。

INFO: Illegal access: this web application instance has been stopped already.  Could not load com.schedular.job.BirthdayJob.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.quartz.simpl.LoadingLoaderClassLoadHelper.loadClass(LoadingLoaderClassLoadHelper.java:59)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:99)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:138)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.selectJobDetail(StdJDBCDelegate.java:852)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTrigger(JobStoreSupport.java:2816)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.execute(JobStoreSupport.java:2759)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.execute(JobStoreSupport.java:2757)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3787)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTriggers(JobStoreSupport.java:2756)
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:272)

Jul 13, 2015 6:00:00 AM org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already.  Could not load com.schedular.job.BirthdayJob.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.quartz.simpl.LoadingLoaderClassLoadHelper.loadClass(LoadingLoaderClassLoadHelper.java:59)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:99)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:138)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.selectJobDetail(StdJDBCDelegate.java:852)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.retrieveJob(JobStoreSupport.java:1385)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.triggerFired(JobStoreSupport.java:2964)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.execute(JobStoreSupport.java:2908)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.execute(JobStoreSupport.java:2901)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3787)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.triggersFired(JobStoreSupport.java:2900)
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:336)

采纳答案by Ravi

As someone approached me for the solution of this issue. I'm posting it now.

当有人找我解决这个问题时。我现在发布它。

  1. I undeployed the application, which was having this issue and cleaned up all related files from the server.
  2. Then, I restarted the tomcat server. So that, it will flush up all temporary files and cache.
  3. Then, I deployed the same application and it started working without any issue.
  1. 我取消部署了存在此问题的应用程序并清除了服务器中的所有相关文件。
  2. 然后,我重新启动了 tomcat 服务器。这样,它将刷新所有临时文件和缓存。
  3. 然后,我部署了相同的应用程序,它开始工作,没有任何问题。

回答by jwenting

The correct format for your JDBC URL is not what you wrote:

您的 JDBC URL 的正确格式不是您所写的:

connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

but either

但要么

connstr = "jdbc:oracle:thin:@//160.110.xx.xxx:1521/test";

connstr = "jdbc:oracle:thin:@//160.110.xx.xxx:1521/test";

or

或者

connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521:test";

connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521:test";

depending on whether 'test' is a service or a SID.

The log fragment you showed doesn't show that the getConnection method worked on the 4th! It only showed there was no error thrown by it. This may just have meant that the method was never called (so no connection was attempted).

取决于“测试”是服务还是 SID。

您显示的日志片段并未显示 getConnection 方法在 4 日有效!它只表明它没有抛出任何错误。这可能只是意味着从未调用该方法(因此未尝试连接)。

回答by Yann TM

Not sure if it helps, but this is the code I have to do the same thing,

不确定它是否有帮助,但这是我必须做同样事情的代码,

    try { 
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
    } catch (ClassNotFoundException e) { 
        System.out.println("Could not load the driver"); 
    } 

    Connection conn = DriverManager.getConnection                                     ("jdbc:oracle:thin:@ten10:1521:acdb", user, pass); 

So, not quite the same Class.forName, but same form for protocol.

所以,不是完全相同的 Class.forName,而是相同的协议形式。

The class for name is essential, it ensures that the class loader has loaded the Oracle jdbc driver.

name 类是必不可少的,它确保类加载器已经加载了 Oracle jdbc 驱动程序。

What could be happening is some connectivity problem on the machine where the code is running, so that the location that contains the actual ojdbc6.jar (as pointed to on the classpath) is not always accessible (if it's not on a local disk ?).

可能发生的是运行代码的机器上的一些连接问题,因此包含实际 ojdbc6.jar 的位置(如类路径上所指)并不总是可访问的(如果它不在本地磁盘上?) .

回答by sriharichander

It seems,The "ojdbc6.jar" is not in the CLASSPATH of your application server.

看来,“ojdbc6.jar”不在您的应用程序服务器的 CLASSPATH 中。

回答by alizelzele

When it says it can not find class it can not find the class.

当它说找不到班级时,它找不到班级。

From my experience, this sort of problems that sometimes work and sometime not are thread related. My assumption is ClassLoaderis loading your class async, so calling connection just after loading it may be the problem. did u try loading oracle class in a static part? something like:

根据我的经验,这种有时有效有时无效的问题与线程有关。我的假设是ClassLoader异步加载您的类,因此在加载后调用连接可能是问题所在。你有没有尝试在静态部分加载 oracle 类?就像是:

public class dbconf {

static {
  Class.forName("oracle.jdbc.OracleDriver");
}

public Connection getConnection() throws SQLException {
    String connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";
    try {
        String uname = "scott";
        String pass = "tiger";
        return DriverManager.getConnection(connstr, uname, pass);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
}

Another issue: Is your Code getting Compiled every day(By Continuous Delivery or ...)?

另一个问题:您的代码是否每天都在编译(通过持续交付或...)?

回答by Dharamendra Prajapati

It seems issue is with your jar try to replace with ojdbc14.jar and add it to Class-path if you are using Eclipse follow below steps:- Eclipse --> (Select the Project)Go to Properties --> Java Build Path--> Choose Add Jar or Add External Jar.

如果您使用 Eclipse,似乎问题在于您的 jar 尝试替换为 ojdbc14.jar 并将其添加到 Class-path,请按照以下步骤操作:- Eclipse -->(选择项目)转到 Properties --> Java Build Path- -> 选择添加 Jar 或添加外部 Jar。

回答by Amit Parashar

If possible, view the println for DriverManager.getConnection () method. You may be getting a null connection object from DB without any exception, during failures.

如果可能,查看 println for DriverManager.getConnection() 方法。在失败期间,您可能会毫无例外地从 DB 获取空连接对象。

SQLException reason = null;
for(DriverInfo aDriver : registeredDrivers) {
    if(isDriverAllowed(aDriver.driver, callerCL)) {
        try {
            println("    trying " + aDriver.driver.getClass().getName());
            Connection con = aDriver.driver.connect(url, info);
            if (con != null) {
                println("getConnection returning " + aDriver.driver.getClass().getName());
                return (con);
            }
        } catch (SQLException ex) {
            if (reason == null) {
                reason = ex;
            }
        }

    } else {
        println("    skipping: " + aDriver.getClass().getName());
    }

}
if (reason != null)    {
    println("getConnection failed: " + reason);
    throw reason;
}
println("getConnection: no suitable driver found for "+ url);
throw new SQLException("No suitable driver found for "+ url, "08001");

回答by Grigoris Loukidis

A possible solution is : Go to Event Viewer -> Windows Archives and delete Application Events and System Events.(DON'T delete Security Events!).After that restart your pc and you will be ok.

一个可能的解决方案是:转到事件查看器 -> Windows 存档并删除应用程序事件和系统事件。(不要删除安全事件!)。然后重新启动你的电脑,你会没事的。

回答by Ryan

I'm unfamiliar with "schedular" but your latest update suggests that you have threads that weren't cleanly stopped from a previous undeploy/redeploy. There is a JavaSpecialists newsletter about how to shutdown threads cleanly.

我不熟悉“schedular”,但您的最新更新表明您的线程没有从之前的取消部署/重新部署中完全停止。有一个关于如何干净地关闭线程JavaSpecialists 通讯

I wonder if perhaps your servlet shutdown code unregisters the database driver? From your stacktrace it looks like you are running in Tomcat. Even if your code doesn't directly de-register the driver I believe Tomcat 7 and up will unregister drivers as part of Tomcat's memory leak detection/mitigation.

我想知道您的 servlet 关闭代码是否取消了数据库驱动程序的注册?从您的堆栈跟踪来看,您似乎在 Tomcat 中运行。即使您的代码没有直接取消注册驱动程序,我相信 Tomcat 7 及更高版本也会取消注册驱动程序,作为 Tomcat 内存泄漏检测/缓解的一部分。

That might explain why the driver is sometimes present and sometimes not.

这或许可以解释为什么司机有时在场,有时不在。

回答by Ram72119

Don't keep the driver name in static way. Use the JDBC + Java API to get the driver class name like the following :

不要以静态方式保留驱动程序名称。使用 JDBC + Java API 获取驱动程序类名称,如下所示:

public class dbconf {

    private Connection connect;
    private String connstr;

    public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

        try {
                String uname = "scott";
                String pass = "tiger";
                Class.forName(OracleDriver.class.getClass().getName().toString()).newInstance();
                connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }

            return connect;
    }
}

It is better, if you made any typo or and you can check whether the ojdbc6.jar is set in the build path in a good way..

最好是,如果你打错了,或者你可以检查 ojdbc6.jar 是否以一种好的方式设置在构建路径中..

Hope this information helps...

希望这些信息能帮助...