Java SQL错误,找不到合适的驱动程序

编程入门 行业动态 更新时间:2024-10-26 16:24:12
本文介绍了Java SQL错误,找不到合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图编译这小段代码,帮助我连接到我的数据库,并检索一些信息来测试它。我在Windows 7 x64机器上使用Netbeans。这是代码:

package passwordprotector; import java.sql。*; public class PasswordProtector { / ** * @param参数命令行参数 * / public static void main String [] args){ String host =jdbc:derby:// localhost:1527 / PasswordProtector DB; String dbUsername =john; String dbPassword =arsenal; / * try { Class.forName(org.apache.derby.jdbc.EmbeddedDriver); } catch(ClassNotFoundException e){ System.out.println(e); } * / try { Connection con = DriverManager.getConnection(host,dbUsername,dbPassword); 语句stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(SELECT * FROM APP.PERSON); while(rs.next()){ String uName = rs.getString(uname); String uPass = rs.getString(upass); System.out.println(Username:+ uName +/ n+Password:+ uPass); } } catch(SQLException e){ System.err.println(e); } } }

当我编译并运行I收到此错误:

java.sql.SQLException:没有找到适合的驱动程序jdbc:derby:// localhost:1527 / PasswordProtector DB BUILD SUCCESSFUL(总时间:0秒)

当我右击我的db并选择属性我可以看到它的位置,像这样:

数据库URL:jdbc:derby:// localhost:1527 / PasswordProtector

我已经检查过发布过此信息的其他人,似乎他们有一个不正确的URL,因为问题,但我看不到任何其他网址,我可以使用除了一个发布。

我试过没有结束'DB'的字符串主机,无效。

我也已经从这里,仍然无法弄清楚为什么URl不正确:

解决方案

p>不确定数据库URL连接的问题,而是在使用正确的驱动程序。如果数据库是嵌入式的,您应该使用您的帖子中注释的驱动程序,例如我的 answer ,还有嵌入式德比教程。 / p>

如果没有,请使用

Class.forName(org.apache .derby.jdbc.ClientDriver);

这是一个不同的驱动程序,连接到运行独立的数据库。在这种情况下,请参阅 derby网络客户端文档如何配置和运行derby网络客户端。

I'm trying to compile this small piece of code, to help me connect to my db and retrieve some information to test it. I am using Netbeans on a Windows 7 x64 machine. This is the code:

package passwordprotector; import java.sql.*; public class PasswordProtector { /** * @param args the command line arguments */ public static void main(String[] args) { String host = "jdbc:derby://localhost:1527/PasswordProtector DB"; String dbUsername = "john"; String dbPassword = "arsenal"; /*try{ Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); }catch(ClassNotFoundException e){ System.out.println(e); }*/ try{ Connection con = DriverManager.getConnection(host, dbUsername, dbPassword); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM APP.PERSON"); while (rs.next()) { String uName = rs.getString("uname"); String uPass = rs.getString("upass"); System.out.println("Username: " + uName + "/n" + "Password: " + uPass); } }catch(SQLException e){ System.err.println(e); } } }

When I compile and run I receive this error:

java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/PasswordProtector DB BUILD SUCCESSFUL (total time: 0 seconds)

When I right click on my db and select properties I can see it's location, like so:

Database URL: jdbc:derby://localhost:1527/PasswordProtector

I've checked with others who have posted about this and it seems they had an incorrect URL as the issue, but I can't see any other URL which I can use apart from the one posted.

I've tried with and without the ending ' DB' for the String host, neither works.

I've also already read from here and still couldn't figure out why the URl is incorrect:

解决方案

Not sure the problem with the database URL connection, but in the usage of the correct driver. If the database is embedded you should use the driver commented in your post and example from my answer, there's also tutorial embedded derby.

if not then use

Class.forName("org.apache.derby.jdbc.ClientDriver");

It's a different driver to connect to the database running standalone. In this case see derby network client documentation how to configure and run derby network client.

更多推荐

Java SQL错误,找不到合适的驱动程序

本文发布于:2023-10-18 23:45:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   驱动程序   合适   错误   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!