Ben Calder
Ben Calder

Reputation: 61

How to access a java Derby database? I could really do with a few helpful pointers

I am new to using Derby and databses in eclipse, and I have become a tad lost and in need to a bit of help. I have established a database connection, created a new database, and a new schema, within which I have some tables containing some test data. I don't have any problem with the sql queries to select the relevant data. The problem I have is getting to a point where I can use queries. I am trying to create a class which connects to the database, and for testing purposes, uses a simple query to select some data. This is what I have so far:

public void getExerciseInfo() {
    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
        connect = DriverManager.getConnection("jdbc:derby://localhost/c:/TestDatabase");
        PreparedStatement statement = connect.prepareStatement("SELECT * from TESTSCHEMA.TESTTABLE");

        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            String name= resultSet.getString("NAME");
            String type = resultSet.getString("TYPE");
            System.out.println(name);
            System.out.println(type);
        }

    } catch (Exception e) {

    } finally {
        close();
    }
}

All I am trying to do is output the data in the table to the console, but I cant even do this simple task :( Im guessing my connection url is invalid, is it supposed to be the file path to the database folder in my eclipse workspace?

Anyhow, I am very lost, and any help would be greatly appreciated.

Upvotes: 1

Views: 3745

Answers (2)

trashgod
trashgod

Reputation: 205785

If you are not running the Derby server, you can establish an embedded database connection or use an EmbeddedDataSource, shown here.

Upvotes: 0

Vlad
Vlad

Reputation: 10780

Did you take a look over: http://db.apache.org/derby/integrate/plugin_help/derby_app.html ? You seem to be using the network server but your db URL is wrong.

Upvotes: 1

Related Questions