【问题标题】:Java DB: No Suitable Driver FoundJava DB:找不到合适的驱动程序
【发布时间】:2012-04-13 03:30:37
【问题描述】:

美好的一天!

我知道这类问题有很多帖子,但我查看了其中一些帖子,但由于我使用的是嵌入式 Derby,因此找不到我的问题的答案。

我收到此错误:

##THIS IS GENERATED BY THE METHOD printSQLException shown at the code below
----- SQLException ----- 
SQL State:  08001
Error Code: 0
Message:    No suitable driver found for jdbc:derby://localhost:1527/recordbookDB

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at recordbook.RecordBook.checkHasAccount(RecordBook.java:71)
    at recordbook.Login.<init>(Login.java:31)
    at recordbook.Login$1.run(Login.java:168)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
BUILD STOPPED (total time: 6 minutes 16 seconds)

问题出在连接数据库部分。 这是查看问题所需的部分代码(和方法):

public class RecordBook
{
    private String framework = "embedded";
    private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    private String protocol = "jdbc:derby:";
    private Connection conn;

    //this is where everything happens
    public RecordBook()
    {
        //Loading the Driver
        loadDriver();

        //Connecting to the database
        conn = null;
        try
        {
            Properties props = new Properties();
            props.put("user","root");
            props.put("password","root");
            String dbName = "//localhost:1527/recordbookDB"; 
            conn = DriverManager.getConnection(protocol + dbName, props);  //error is here

        }
        catch (SQLException sqle)
        {
            printSQLException(sqle);
        }
    }

   //BELOW ARE THE METHODS USED ABOVE    
   /**
     * CODE FROM http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
     */
    private void loadDriver()
    {
        try
        {
            Class.forName(driver).newInstance();
            System.out.println("Loaded the appropriate driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            System.err.println("\nUnable to load the JDBC driver " + driver);
            System.err.println("Please check your CLASSPATH.");
            cnfe.printStackTrace(System.err);
        }
        catch (InstantiationException ie)
        {
            System.err.println("\nUnable to instantiate the JDBC driver " + driver);
            ie.printStackTrace(System.err);
        }
        catch (IllegalAccessException iae)
        {
            System.err.println("\nNot allowed to access the JDBC driver " + driver);
            iae.printStackTrace(System.err);
        }
    }

    /**
     * CODE FROM http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
     *
     * @param e the SQLException from which to print details.
     */
    public static void printSQLException(SQLException e)
    {
        // Unwraps the entire exception chain to unveil the real cause of the exception.
        while (e != null)
        {
            System.err.println("\n----- SQLException -----");
            System.err.println("  SQL State:  " + e.getSQLState());
            System.err.println("  Error Code: " + e.getErrorCode());
            System.err.println("  Message:    " + e.getMessage());
            // for stack traces, refer to derby.log or uncomment this:
            //e.printStackTrace(System.err);
            e = e.getNextException();
        }
    }

【问题讨论】:

  • 请编辑您的帖子以包含完整的堆栈跟踪,并指出您的代码中的哪个语句引发了真正的异常。还包括该声明的来源。
  • @JimGarrison 好的。我刚刚编辑了它。谢谢
  • recordbook.RecordBook.checkHasAccount()的来源在哪里?

标签: java derby javadb


【解决方案1】:

没有合适的驱动程序”通常意味着您提供的用于连接的 JDBC URL 的语法不正确。

更多详情check out the documentation

还要检查您的类路径中是否有derby.jar。我建议将derby.jar 放置在项目的/WEB-INF/lib 目录的物理位置。然后eclipse 会处理其余的。

【讨论】:

  • 谢谢。我找到了正确的网址。问题现在在 CLASSPATH 上。它说:Unable to load the JDBC driver org.apache.jdbc.EmbeddedDriver Please check your CLASSPATH.
猜你喜欢
  • 2020-02-03
  • 2012-10-06
  • 2020-05-02
  • 2019-05-25
  • 1970-01-01
  • 2012-12-09
  • 2013-02-08
  • 1970-01-01
相关资源
最近更新 更多