Java DB:找不到合适的驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10134869/
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
Java DB: No Suitable Driver Found
提问by Ace Eusebio
Good day!
再会!
I know there has been lots of posts for this kind of question, but I've looked at some of them and could not find the answer for my problem since I am using an Embedded Derby.
我知道有很多关于此类问题的帖子,但是我查看了其中的一些帖子,但由于我使用的是 Embedded Derby,因此无法找到问题的答案。
I am getting this error:
我收到此错误:
##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.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.accesspublic 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(EventQueue.java:96)
at java.awt.EventQueue.run(EventQueue.java:608)
at java.awt.EventQueue.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext.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)
The problem is in the connecting to the database part. This is the part of the code (and the methods) needed to see the problem:
问题在于连接到数据库部分。这是查看问题所需的代码(和方法)的一部分:
##代码##回答by Hardik Mishra
No suitable driver" usually means that the JDBC URL you've supplied to connect has incorrect syntax.
没有合适的驱动程序”通常意味着您提供给连接的 JDBC URL 的语法不正确。
More details check out the documentation
更多细节请查看文档
Also check that you have derby.jar
in your classpath. I would suggest to place derby.jar
at physical location to /WEB-INF/lib
directory of your project.Then eclipse will take care for the rest.
还要检查derby.jar
您的类路径中是否有。我建议将derby.jar
物理位置放置到/WEB-INF/lib
您的项目目录中。然后 eclipse 将负责其余的工作。