2012-04-13 38 views
1

美好的一天!Java DB:找不到合適的驅動程序

我知道有很多這類問題的帖子,但我已經看過其中的一些,並且因爲我正在使用嵌入式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(); 
     } 
    } 
+0

請編輯您的帖子以包含COMPLETE堆棧跟蹤,並指出代碼中的哪個語句拋出了真正的異常。還包括該聲明的來源。 – 2012-04-13 03:33:37

+0

@JimGarrison好的。我剛剛編輯它。謝謝 – 2012-04-13 04:12:10

+0

'recordbook.RecordBook.checkHasAccount()'的來源在哪裏? – 2012-04-13 04:45:44

回答

3

沒有合適的驅動程序」通常意味着你已經提供給連接JDBC URL有不正確的語法

更多細節check out the documentation

也檢查你有你的classpath derby.jar。我建議在物理位置放置derby.jar/WEB-INF/lib目錄中project.Then月食會照顧的休息。

+1

謝謝。我找到了正確的網址。現在問題在CLASSPATH上。它說: '無法加載JDBC驅動程序org.apache.jdbc.EmbeddedDriver 請檢查您的CLASSPATH.' – 2012-04-13 05:03:49

+0

結帳編輯答案 – 2012-04-13 05:17:06

+0

謝謝Hardik!我終於得到了數據庫的工作。 :) – 2012-04-13 08:44:10

相關問題