Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Table Name

 
Giri Thava
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbcdbc:Sample","","");
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = meta.getTables("",null,"",null);
System.out.println(rs.getObject(1));

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

[Method 2]
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbcdbc:Sample","","");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT Name,Address,Total FROM student" );
ResultSetMetaData metaData = resultSet.getMetaData();
System.out.println(metaData.getTableName(1));

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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


use the above code in place of first method.
[ July 26, 2007: Message edited by: subodh gupta ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic