简体   繁体   English

首次连接到Netbeans SQL数据库 - java.sql.SQLException:找不到合适的驱动程序0 08001

[英]Connecting to Netbeans SQL Database for the first time - java.sql.SQLException: No suitable driver found 0 08001

I am fairly new to Java, so I bought a book to learn from. 我对Java很新,所以我买了一本书来学习。 Everything went swimmingly until I got to the chapter on SQL. 一切顺利,直到我进入关于SQL的章节。 I am working in NetBeans with the sample database, but I can't get the database to connect with the program. 我在NetBeans中使用示例数据库,但我无法让数据库与程序连接。

Here's the code I faithfully copied from the book: 这是我忠实地从书中复制的代码:

import java.sql.*;

public class SysTableReporter {
    public static void main(String[] arguments) {
        String data = "jdbc:derby://localhost:1527/sample";
        try (
            Connection conn = DriverManager.getConnection(
                    data, "app", "APP");
            Statement st = conn.createStatement()) {
                System.out.println("TABLEID:\t" );

            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());
        }
    }
}

Here's what my Services Panel looks like: 这是我的服务面板的样子:

Click to view Image 点击查看图片

Here's my output: 这是我的输出:

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

At first I figured I just had some typo, but looking over it carefully I can't find it. 起初我觉得我只是有一些错字,但仔细看它我找不到它。 I tried installing a new JDBC driver, but that didn't seem to help either. 我尝试安装新的JDBC驱动程序,但这似乎也没有帮助。 I'm running a Windows 8 laptop, and I have the telnet client turned on. 我正在运行Windows 8笔记本电脑,我打开了telnet客户端。 My best guess at this point is that I somehow missed a step in the initial setup, but I can't find it to go back and fix it. 我在这一点上最好的猜测是,我在某种程度上错过了初始设置中的一个步骤,但我找不到要回去修复它。 Any help would be great. 任何帮助都会很棒。

You are probably just missing the Derby JDBC driver jar in your project's library section (I'm assuming that you created a standard Netbeans project, not a Maven type project). 您可能只是缺少项目库部分中的Derby JDBC驱动程序jar(我假设您创建了一个标准的Netbeans项目,而不是Maven类型的项目)。 The jar is called derbyclient.jar. jar被称为derbyclient.jar。

The easiest way to solve this: 解决这个问题最简单的方法:

  1. locate derbyclient.jar eg with Explorer 找到derbyclient.jar,例如用Explorer
  2. in Netbeans, rightclick on the project node in the Projects pane. 在Netbeans中,右键单击“项目”窗格中的项目节点。
  3. Select Properties, and there select libraries 选择“属性”,然后选择库
  4. Click "Add JAR/Folder", navigate to derbyclient.jar 单击“添加JAR /文件夹”,导航到derbyclient.jar

That effectively adds the jar to your project. 这有效地将jar添加到您的项目中。 Just recompile, run and everything should work as intended. 只需重新编译,运行,一切都应按预期工作。

EDIT: aside from @BobKuhar's find, another problem with the given code is that it doesn't use one of Java's more powerful debugging mechanisms, the stacktrace. 编辑:除了@BobKuhar的发现之外,给定代码的另一个问题是它没有使用Java更强大的调试机制之一,堆栈跟踪。 At its most basic form, showing them on screen is simple, not more than 在最基本的形式,在屏幕上显示它们很简单,不仅仅是

    } catch (SQLException s) {
        System.out.println("SQL Error: " + s.toString() + " " 
                + s.getErrorCode() + " " + s.getSQLState());
        // and show us the stacktrace
        s.printStackTrace();
    } catch (Exception e) {
        System.out.println("Error: " + e.toString() + e.getMessage());
        // and show us the stacktrace
        e.printStackTrace();
    }

the stack trace will not only show you the exact line at which the error occurred, but also the trajectory to the exception (how the program got there), invaluable in more complicated programs. 堆栈跟踪不仅会显示错误发生的确切行,还会显示异常的轨迹(程序如何到达),在更复杂的程序中非常有用。 Definitely something you want to learn to use! 绝对是你想要学习使用的东西!

Lots of info on stack traces here: What is a stack trace, and how can I use it to debug my application errors? 这里有很多关于堆栈跟踪的信息: 什么是堆栈跟踪,我如何使用它来调试我的应用程序错误?

I think what you really have is just a sequencing problem. 我认为你真正拥有的只是一个排序问题。 The Class.forName call registers the driver with the DriverManager (I think). Class.forName调用使用DriverManager注册驱动程序(我认为)。 This needs to occur before you attempt to establish a Connection through the DriverManager. 这需要在尝试通过DriverManager建立连接之前发生。

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

If this gives you some "ClassNotFound" exception, then fvu's assertion that you don't have the Derby JDBC Jar on the class path is your next issue. 如果这给你一些“ClassNotFound”异常,那么fvu断言你在类路径上没有Derby JDBC Jar是你的下一个问题。

The Derby docs have an example: http://db.apache.org/derby/integrate/plugin_help/derby_app.html Derby文档有一个例子: http//db.apache.org/derby/integrate/plugin_help/derby_app.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.sql.SQLException:未找到适合于netbeans的驱动程序 - java.sql.SQLException: No suitable driver found for netbeans java.sql.SQLException:在Netbeans中找不到合适的驱动程序 - java.sql.SQLException: No suitable driver found in Netbeans java.sql.SQLException:找不到合适的驱动程序 - java.sql.SQLException: No suitable driver found java.sql.SQLException:找不到适合的驱动程序 - java.sql.SQLException: No suitable driver found for java.sql.SQLException:找不到适合的驱动程序 - java.sql.SQLException: No suitable driver found for SEVERE: null in java netbeans 14 java.sql.SQLException: No suitable driver found for error - SEVERE: null in java netbeans 14 java.sql.SQLException: No suitable driver found for error java.sql.SQLException找不到合适的驱动程序,但可以在Netbeans中完美连接 - java.sql.SQLException no suitable driver found but can connect perfectly in Netbeans Java + Jersey + Gradle-java.sql.SQLException:找不到合适的驱动程序 - Java + Jersey + Gradle - java.sql.SQLException: No suitable driver found java.sql.SQLException:找不到适用于jdbc:sqlite的驱动程序 - java.sql.SQLException: No suitable driver found for jdbc:sqlite java.sql.SQLException:找不到适用于jdbc:microsoft的驱动程序 - java.sql.SQLException: No suitable driver found for jdbc:microsoft
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM