开发者

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜