简体   繁体   中英

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. Everything went swimmingly until I got to the chapter on SQL. I am working in NetBeans with the sample database, but I can't get the database to connect with the program.

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. I'm running a Windows 8 laptop, and I have the telnet client turned on. 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). The jar is called derbyclient.jar.

The easiest way to solve this:

  1. locate derbyclient.jar eg with Explorer
  2. in Netbeans, rightclick on the project node in the Projects pane.
  3. Select Properties, and there select libraries
  4. Click "Add JAR/Folder", navigate to derbyclient.jar

That effectively adds the jar to your project. 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. 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). This needs to occur before you attempt to establish a Connection through the 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.

The Derby docs have an example: http://db.apache.org/derby/integrate/plugin_help/derby_app.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM