JAVA SQL - 找不到适合 jdbc:derby 的驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20625040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:11:35  来源:igfitidea点击:

JAVA SQL - No suitable driver found for jdbc:derby

javasql

提问by Sylvain

I am a newbie at JAVA and SQL.

我是 JAVA 和 SQL 的新手。

In one exercise, I need to read data from a sample SQL table in NetBeans.

在一个练习中,我需要从 NetBeans 中的示例 SQL 表中读取数据。

In "services", I started the JAVA DB server. All good. The following message appeared:

在“服务”中,我启动了 JAVA DB 服务器。都好。出现以下消息:

Mon Dec 16 21:40:36 EST 2013 : Security manager installed using the Basic server security policy.
Mon Dec 16 21:40:37 EST 2013 : Apache Derby Network Server - 10.8.3.0 - (1405108) started and ready to accept connections on port 1527

But when I run this program (below), the following error occurs:

但是当我运行这个程序(如下)时,出现以下错误:

SQL Error: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/sample 0 08001

Here are the codes.. how can I make sure the right driver is found?

这是代码..我怎样才能确保找到正确的驱动程序?

import java.sql.*;

public class TableReporter {

    public static void main(String[] args) {
        String data = "jdbc:derby://localhost:1527/sample";
        try (
            Connection conn = DriverManager.getConnection(
            data, "app", "APP");
            Statement st = conn.createStatement()) {

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

            ResultSet rec =st.executeQuery(
                    "select * " +
                    "from SYS.SYSTABLES" +
                    "order by TABLENAME");
            while(rec.next()){
                System.out.println("TABLEID:\t" + rec.getString(1));
                System.out.println("TABLENAME:\t" + rec.getString(2));
                System.out.println("TABLETYPE:\t" + rec.getString(3));
                System.out.println("SCHEMAID:\t" + rec.getString(4));
                System.out.println();
            }
        st.close();
        } catch (SQLException s){
            System.out.println("SQL Error: " + s.toString() + " "
                    + s.getErrorCode() + " " + s.getSQLState());
        } catch (Exception e) {
            System.out.println("Error: " + e.toString()
                    +e.getMessage());
                }   
    }   
}

采纳答案by Aditya

The issue is with the jar file.

问题出在 jar 文件上。

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

Check,

查看,

  1. Whether it is installed or not.
  2. If it is installed, is there a need to update your .bashrc file.
  3. Or, have you imported the .jar file in your netBeans IDE.
  1. 无论是否安装。
  2. 如果已安装,是否需要更新您的 .bashrc 文件。
  3. 或者,您是否在 netBeans IDE 中导入了 .jar 文件。

If all does not works, simply write a simple JDBC program to connect to Database using notpad & run it, check what is the problem with your machine.

如果一切都不起作用,只需编写一个简单的 JDBC 程序来使用 notpad 连接到数据库并运行它,检查您的机器有什么问题。

回答by user3109541

Don't you need to load the driver before trying to create the connection?

在尝试创建连接之前不需要加载驱动程序吗?

Move this statement

移动这个语句

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

to be the first in the try block.

成为 try 块中的第一个。

回答by user3109541

Try this changes in your code

在您的代码中尝试此更改

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection(
        data, "app", "APP");
        Statement st = conn.createStatement()) {

回答by yonsong

You need load a jar of derby driver.

您需要加载一罐 derby 驱动程序。

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

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

回答by Duaa Zahi

I had the same problem although am providing

我有同样的问题,虽然我提供

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

But I discovered my properties file that I am loading the db username and the password from is not in the correct format. it should be like this:

但是我发现我正在加载 db 用户名和密码的属性文件格式不正确。它应该是这样的:

dburi=jdbc:derby://localhost:1527/dbname
user=username
pass=xxxx

and in the code:

并在代码中:

CachedRowSetImpl crs = new CachedRowSetImpl();
Properties pr = Properties();
crs.setUrl(pr.getProperty("dburi"));
crs.setUsername(pr.getProperty("user"));
crs.setPassword(pr.getProperty("pass"));