SQLException:找不到适用于 jdbc:derby://localhost:1527 的驱动程序
我在 Netbeans 中收到此错误:
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/
这是如何引起的以及如何解决它?
I get this error in Netbeans:
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/
How is this caused and how can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您可能缺少启动 Derby 服务器。
一旦 derby 服务器启动,它就会开始侦听默认端口 1527。
启动脚本位于以下位置:
Windows:
Linux:
You may be missing to start the Derby server.
Once a derby server starts, it starts listening to default port 1527.
Start script is located as below:
Windows:
Linux:
当连接字符串的语法无效时会出现此错误。
您可以使用连接字符串,
也
可以在命令提示符中使用以下命令,以下命令成功创建一个名为
MyDbTest
的新数据库:This error occurs when the syntax of connection string is not valid.
You can use the connection string as
or
you can use the following command in the command prompt, the command below creates a new database called
MyDbTest
succesfully:我刚刚遇到这个问题,尝试了上述所有建议,但仍然失败。无需重复上面的建议,以下是我(您)可能遗漏的内容:
如果您使用 Maven,您可能会声明依赖关系,即:
请注意版本。它必须与您正在运行的服务器实例兼容。
我通过放弃 Maven 提供的依赖项并手动添加来自“%JAVA_HOME%\db\lib”(与我正在运行的服务器的源相同)的外部 jar 来解决我的情况。在本例中,我正在使用本地进行测试。
因此,如果您正在使用远程服务器实例进行测试,请查找服务器包附带的 derbyclient.jar。
I just bumped into this problem, tried all above suggestions but still failed. Without repeat what have been suggested above, here are the things I (you) may be missing:
In case you are using maven, likely you'll state the dependencies i.e:
Please be careful with the version. It must be compatible with the server instance you are running.
I solved my case by giving up on what maven dependencies provided and manually adding external jar from "%JAVA_HOME%\db\lib", the same source of my running server. In this case I'm testing using my Local.
So if you're testing with remote server instance, look for the derbyclient.jar that come with server package.
遇到了同样的问题。我正在做类似的事情:
connect 'jdbc:derby://localhost:1527/~/databases/db1'
用绝对路径替换路径解决了这个问题:
connect 'jdbc:derby //localhost:1527//Users/ayush99/databases/db1'
。总之:避免在现有数据库的路径中使用
~
或任何此类变量。Encountered the same problem. I was doing something like:
connect 'jdbc:derby://localhost:1527/~/databases/db1'
Replacing the path with the absolute path fixed this problem:
connect 'jdbc:derby://localhost:1527//Users/ayush99/databases/db1'
.In summary: Avoid using
~
or any such variables in the path of existing database.我使用了上面的答案,但没有任何效果。对我有用的是将依赖项放入项目中,就像上面所说的那样。
然后进入依赖项文件夹,右键单击依赖项 derbyshared.jar 和 derbyclient.jar 并“添加本地源”。将两个依赖项与 derby 文件夹中的本地 jar 文件连接起来并完成。也许只是 Netbeans 的问题
I used the above answers and nothing worked. What worked for me was to put the dependencies into the project, like its said above.
Then go in the dependencies folder, right click on the dependencies derbyshared.jar and derbyclient.jar and "add local sources". Connect the two dependencies with the local jar files in your derby folder and its done. Maybe only a Netbeans problem
此异常有两个原因:
在您的情况下,我希望在连接字符串的末尾看到一个数据库名称。例如(如果您希望在数据库不存在时创建数据库,请使用
create=true
):默认情况下,数据库会在网络服务器启动的目录中创建。但您也可以指定数据库位置的绝对路径:
为了以防万一,请检查 derbyclient.jar 是否位于类路径上,以及您是否正在加载适当的驱动程序
org.apache。 derby.jdbc.ClientDriver 在服务器模式下工作时。
This exception has two causes:
In your case, I'd expect to see a database name at the end of the connection string. For example (use
create=true
if you want the database to be created if it doesn't exist):Databases are created by default in the directory where the Network Server was started up. But you can also specify an absolute path to the database location:
And just in case, check that derbyclient.jar is on the class path and that you are loading the appropriate driver
org.apache.derby.jdbc.ClientDriver
when working in server mode.注意:您可以从此处下载它。
如果找不到,请
在项目选择选项卡中查找您的项目
右键单击“库”
单击“添加 JAR/文件夹...”
选择“derbyclient.jar”
单击“打开” ,然后您将在“Libraries”下看到“derbyclient.jar”
确保您的 URL、用户名、密码正确,并运行你的代码:)
Notice: you can download it from here.
If you can't find it, then
Find your project in projects selection tab
Right click "Libraries"
Click "Add JAR/Folder..."
Choose "derbyclient.jar"
Click "Open", then you will see "derbyclient.jar" under your "Libraries"
Make sure your URL, user name, password is correct, and run your code:)
对我来说
有帮助。这样,DriveManager 确实知道 derby EmbeddedDriver。也许分配一个新的 EmbeddedDriver 太重了,但另一方面,Class.forName 需要我不太喜欢的 try/catch/doSomethingIntelligentWithException 。
For me
helped. In this way, the DriveManager does know the derby EmbeddedDriver. Maybe allocating a new EmbeddedDriver is to heavy but on the other side, Class.forName needs try/catch/doSomethingIntelligentWithException that I dont like very much.
JDBC
DriverManager
无法为给定的连接 URL 找到任何合适的Driver
。连接数据库之前根本没有加载 JDBC 驱动程序,或者连接 URL 错误。由于连接 URL 看起来不错,我敢打赌驱动程序根本没有加载。在连接数据库之前,您需要在应用程序启动期间加载驱动程序。对于 Apache Derby,驱动程序类名称为 org.apache.derby.jdbc.ClientDriver。所以:The JDBC
DriverManager
can't find any suitableDriver
for the given connection URL. Either the JDBC driver isn't loaded at all before connecting the DB, or the connection URL is wrong. Since the connection URL looks fine, I bet that the driver isn't loaded at all. You need to load the driver during application's startup before connecting the DB. For Apache Derby, the driver class name isorg.apache.derby.jdbc.ClientDriver
. So:当我在 Netbeans 上编写 Java 应用程序时,我遇到了同样的问题。解决方案如下:
在项目选择选项卡中查找您的项目
右键单击“库”
单击“添加 JAR/文件夹...”
选择“derbyclient.jar”
单击“打开”,然后您将在“库”下看到“derbyclient.jar”
确保您的 URL、用户名、密码正确,然后运行您的代码:)
I had the same problem when I was writing Java application on Netbeans.Here is the solution:
Find your project in projects selection tab
Right click "Libraries"
Click "Add JAR/Folder..."
Choose "derbyclient.jar"
Click "Open", then you will see "derbyclient.jar" under your "Libraries"
Make sure your URL, user name, pass word is correct, and run your code:)
如果您使用嵌入式 Derby,则您的类路径中需要 Derby.jar。
If your using embedded Derby you need Derby.jar in your classpath.
问题已得到解答,但提供了命令行以供说明。当我尝试尽可能简单的测试连接到网络模式德比时,这对我有用。
应用程序中加载的驱动程序为:Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
连接 URL 为:“jdbc:derby://localhost:1527/myDB;create=true”
我使用以下命令运行我的应用程序:java -classpath derbyclient.jar:。 myAppClass
The question is answered but providing a command line for illustration. This worked for me when I was trying a as simple as possible test to connect to network mode derby.
Driver loaded in app with:Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
The connection URL was: "jdbc:derby://localhost:1527/myDB;create=true"
I ran my app using: java -classpath derbyclient.jar:. myAppClass
我通过将库添加到项目下面的库控制台解决了这个问题:
我的项目正在运行!
I solved this by adding library to the library console below my project:
My project is working !
我面临着同样的问题。在使用连接 URL 和用户凭据获取连接之前,我缺少 DriverManager.registerDriver() 调用。
它在 Linux 上得到了修复,如下所示:
对于 Windows:
I was facing the same issue. I was missing DriverManager.registerDriver() call, before getting the connection using the connection URL and user credentials.
It got fixed on Linux as below:
For Windows:
有相同的情况,通过运行定义 derby.jar 位置的类路径来解决。
java -cp
更准确地说:
java -cp "lib/*:." Program
其中
:.
包括当前目录。并且lib/*
不包含jar扩展名(lib/*.jar
)。Had the same, and it was solved by running with the classpath defining the derby.jar location.
java -cp <path-to-derby.jar> <Program>
To be more precise:
java -cp "lib/*:." Program
Where
:.
includes the current directory. And thelib/*
does not include the jar extension (lib/*.jar
).也有可能在 persistence.xml 中,当 jdbc url 指向 Derby 服务器时使用了 EmbeddedDriver。在这种情况下,只需将 url 更改为指向数据库路径即可。
It's also possible that in persistence.xml, EmbeddedDriver was used while the jdbc url was pointing to Derby server. In this case just change the url to pointing a path of database.
如果数据库已创建并且您已开始连接,那么您需要添加驱动程序 jar。在项目窗口中,右键单击库文件夹,转到 c:programsfiles\sun\javadb\lib\derbyclient.jar。加载文件,你应该能够运行。
一切顺利
if the database is created and you have started the connection to the, then al you need is to add the driver jar. from the project window, right click on the libraries folder, goto c:programsfiles\sun\javadb\lib\derbyclient.jar. load the file and you should be able to run.
all the best
我尝试了该线程中提到的所有内容,只有 .registerDriver() 对我有用。这就是我的代码部分现在的样子:
请注意,问题不在嵌入式 Derby 中。
I tried everything mentioned in this thread and only .registerDriver() worked for me. This is how my part of code looks now:
Notice that the problem wasn't in embedded Derby.
如果 Java DB 服务器尚未启动,您也可能会收到相同的错误。
You can also get the same error if the Java DB server has not been started.