0

I'm using the H2 database in my Java project (embedded mode). On my computer at home everything works, the connection can be established, but on all other computers I always receive the following error:

org.h2.jdbc.JdbcSQLException: Table "CUSTOMERS" not found; SQL statement: SELECT * FROM CUSTOMERS [42102-162]

I'm sure, that within the DB everything is alright, it should be something with the connection. But even if I import the h2-1.3.162.jar file, the error still remains.

String dbClass = "org.h2.Driver";
String dbDriver = "jdbc:h2:~/cc";
String user = "user1";
String pass = "test1";
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;

public void connect() {
    boolean done = false;
    //load driver
    try {
        Class.forName(dbClass).newInstance();
        System.out.println("driver loaded"); // This is shown in the Compiler
    } catch (Exception ex) {
        System.out.println("error while loading driver");
        System.err.println(ex);
    }
    // Connection
    try {
        conn = DriverManager.getConnection(dbDriver, user, pass);
        System.out.println("connected"); // This is shown in the Compiler
        done = true;
    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    }
}

public Vector select() {
    data = new Vector();
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * FROM CUSTOMERS");
        while (rs.next()) {
            Vector row = new Vector();
            row.add(rs.getInt("id"));
            row.add(rs.getString("fname"));
            row.add(rs.getString("lname"));
            row.add(rs.getString("street"));
            row.add(rs.getString("city"));
            row.add(rs.getString("zip"));
            row.add(rs.getString("state"));
            row.add(rs.getString("phone"));
            row.add(rs.getString("birthday"));
            row.add(rs.getString("email"));
            row.add(rs.getInt("code"));
            data.add(row);
        }
        rs.close();
        stmt.close();
    } catch (SQLException ex) {
        System.out.println("error while selecting"); // I receive this error
        System.err.println(ex);
    }
    return data;
}
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181

3 Answers3

4

The problem isn't with your connection as you'd receive an exception well before then if it was failing to connect to the database. The exception is pretty clear about what the issue is, as well - it can't find the CUSTOMERS table. That could be because the table doesn't exist at all, or the connection is pointing at the wrong database; try putting in the full schema information of the table, rather than just its name, and see if that works.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
3

I'm sure, that within the DB everything is alright, it should be something with the connection. But even if I import the h2-1.3.162.jar file, the error still remains.

Check your assumptions. This one is incorrect.

There's nothing in the message to suggest that you couldn't connect. Either you connected to the wrong database OR the one you did connect to didn't CREATE TABLE CUSTOMERS. (Should be named CUSTOMER, not plural.)

You'll fix your error faster if you stop assuming that everything you did is correct. You should be assuming that everything is wrong.

I'd print the stack trace when you catch that exception. It'll give you more information.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I'm just trying exactly the same files at home on my own computer and it works! I'm really confused... – Evgenij Reznik Jan 11 '12 at 13:57
  • What files do I have to "take with me"? Is it enough to declare those `String dbClass = "org.h2.Driver"; String dbDriver = "jdbc:h2:~/cc";` and put the h2.jar file into the _lib_ folder within the main folder? – Evgenij Reznik Jan 11 '12 at 16:30
  • 1
    Well, you need to take the database file with you. The file is `~/cc.h2.db` (where ~ means the home directory of the user running the application). – Thomas Mueller Jan 11 '12 at 20:34
  • Where can I find this database file? And where to copy it? Netbeans put only _h2-1.3.162.jar_ in the lib folder of my application. – Evgenij Reznik Jan 12 '12 at 01:41
  • Somebody has to create that table. Did you do it? If not, why don't you read up on how to do DDL SQL in Hypersonic and run it on this other machine? http://hsqldb.org/doc/guide/ch09.html – duffymo Jan 12 '12 at 01:51
  • Yes, I did it with Netbeans. So where I have to copy this *.db file if I want to export the whole project? – Evgenij Reznik Jan 12 '12 at 12:47
  • Can you open Hypersonic with a client and see the table? If not, I wouldn't believe that it's there. Can a client other than your Java code connect to the database, query and modify the table? – duffymo Jan 12 '12 at 13:02
  • Yes, Firefox. H2 comes with something like a manager using the browser. There I can connect and modify my database and its tables. I know there is a *.db file, I only need to know where to put it if I want to run the whole application on another computer. – Evgenij Reznik Jan 12 '12 at 13:06
3

Finally I figured it out!

It had nothing to do with my tables, the database couldn't be found. When trying to connect to a database which can't be found with String dbDriver = "jdbc:h2:~/cc";, a new database with the name cc (in my case) will be created (of course an empty one with no tables) and the connection is established. That's why I haven't received any connection errors.
In the next step I tried to retrieve some data from the new created empty database and therefore received the error, that my table doesn't exist.

So I changed this line: String dbDriver = "jdbc:h2:file:lib/cc"; and copied into the lib directory of my application my old database cc.h2.db.

That's all!

PS: Here is a similiar problem: h2 (embedded mode ) database files problem

Community
  • 1
  • 1
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181