Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tables reflection with JDBC

 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all! Does anyone know if is there a method by which, given a jdbc url and driver, the system would returns me the list of tables available for that database?
Example for MSSQLServer:
Driver: "com.microsoft.jdbc.sqlserver.SQLServerDriver"
url:"jdbc:microsoft:sqlserver://localhost:1433;databaseName=myweb"
I have a list of tables here. How could I get such a list? I have already tried with getCatalogs() and getTables() methods of DatabaseMetaData interface.
Thanks
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why won't DatabaseMetaData.getTables() work for you? Are you getting an exception? Are you getting no results back?
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, it should work unless not supported by the driver you are using.
But you have the option to query the catalog table yourself and get a ResultSet just like getTables() would give you. The catch is you loose portability...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Worthy",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all! Actually the getMethods() method worked but it was requireing a catalog as parameter. So, first I should get a list of catalog by means of getCatalogs() method, and then pass the catalog name I am interested in to the getTables() method. I tried and it worked. Thanks to everybody anyway(I changed my nick name as suggested by the web master).
Marco.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco,
you can use null in place of catalog/schema parameters to return all tables regardless of catalog/schema
Might save you a few steps if you can just ignore these.
Jamie
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jamie Robertson:
Marco,
you can use null in place of catalog/schema parameters to return all tables regardless of catalog/schema
Might save you a few steps if you can just ignore these.
Jamie

Yes, but i needed to extract only the tables belonging to a specific catalog, so passing the catalog to the getTables() method worked fine. Thank You for Your reply.
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marco Tedone:

Yes, but i needed to extract only the tables belonging to a specific catalog, so passing the catalog to the getTables() method worked fine. Thank You for Your reply.

Would you be interested in posting the code snippet here. ?? if so, please.Actaully i tried the same but it only returns 1 table name out of 10
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic