This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Getting Table Name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Getting Table Name" Watch "Getting Table Name" New topic

Getting Table Name

Giri Thava
Ranch Hand

Joined: Jun 22, 2006
Posts: 38
How can I get the table name(s) from the database (Microsoft Access).

Assume a situation in which I don't know the table name, but I know the
database name (Sample.mdb).

I tried the following two methods.

[Method 1]
Connection connection = DriverManager.getConnection("jdbcdbc:Sample","","");
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = meta.getTables("",null,"",null);

When I run the above code fragment it throws the following Exception.
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

[Method 2]
Connection connection = DriverManager.getConnection("jdbcdbc:Sample","","");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT Name,Address,Total FROM student" );
ResultSetMetaData metaData = resultSet.getMetaData();

Above code fragment returns the table name, but I should have included
the table name in the SQL query.

Advance thanks.
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203

use the above code in place of first method.
[ July 26, 2007: Message edited by: subodh gupta ]
I agree. Here's the link:
subject: Getting Table Name
It's not a secret anymore!