DatabaseMetaData.getTables()

342 views
Skip to first unread message

jay

unread,
Jul 20, 2007, 11:51:40 AM7/20/07
to H2 Database
I am trying to get a list of schemas and table from an arbitrary H2
database from Java Code and seem to be having trouble.

When I run the following from the H2 Console:
SELECT * FROM information_schema.tables;

I get a list of all the tables including the H2 System tables and the
tables I added to H2. When I run the following from a Java program, I
get a list of only the system tables:

try {
Statement statement = getConnection().createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM
information_schema.tables;");
while(rs.next()){
System.out.print(rs.getString(1) + " ");
System.out.print(rs.getString(2) + " ");
System.out.print(rs.getString(3) + " ");
System.out.print(rs.getString(4) + " ");
System.out.println(rs.getString(5));
}
rs.close();
getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}

public Connection getConnection(){

if(_connection == null){
try {
_connection = DriverManager.getConnection("jdbc:h2:test", "sa",
"");
} catch (SQLException e) {
e.printStackTrace();
}
}
return _connection;
}

Also, when I try the following I again get a list of only the system
tables, and not my tables:

try {
DatabaseMetaData metadata = getConnection().getMetaData();
String[] types = {"TABLE", "SYSTEM TABLE"};
ResultSet rs = metadata.getTables("TEST", "%", "%", types);
while(rs.next()){
System.out.print(rs.getString(1) + ", ");
System.out.print(rs.getString(2) + ", ");
System.out.print(rs.getString(3) + ", ");
System.out.println(rs.getString(4));
}
rs.close();
getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}

Any ideas why I can't get a list of the tables I've added to H2 from a
Java app, but I can from the Console?

When I log into the console I used the same jdbc url, username, and
password as I do from the Java app.

jay

unread,
Jul 20, 2007, 12:06:55 PM7/20/07
to H2 Database
Nevermind. I see now that my mistake was in the jdbc url. Because my
url in the Java code was:
jdbc:h2:test

H2 was creating a new database instead of connecting to the one I had
running on port 8082. Thanks.


Jay

Thomas Mueller

unread,
Jul 20, 2007, 10:20:09 PM7/20/07
to h2-da...@googlegroups.com
Hi,

In the newest version, the default database URL is

jdbc:h2:~/test

So to create databases in the user home directory. To avoid such problems.
I'm not sure if using ~ is clear from a user perspective?
Is it better than using jdbc:h2:test?

Thomas

jay

unread,
Jul 21, 2007, 11:45:35 PM7/21/07
to H2 Database
I think that is at least more clear. It was a bit confusing that
'jdbc:h2:test' meant different things depending on whether I was
writing a program or logging into the console.

Jay


On Jul 20, 10:20 pm, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages