Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting all the database names using java with MySql

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I can make a connection to MySql on my server using:

Where colinjava is the name of the database.
Then I do:

To get the connection object.
I know how to get all the table names for a particular database.
But how can I get all the database names?

This also seems to work...


So perhaps I can get the database names this way, but currently I do no know how.

Can anyone help? Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't a database command. Maybe an operating system one will do? Is there anything you can look for on the file system that tells you the names of the databases present?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use DatabaseMetaData#getCatalogs(). It returns a ResultSet with database names in 1st column.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

this works!

Seems to work wether or not I specify the database in the url when I create the Connection object.

Is what I've done the way you meant?

Thanks
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

You're welcome.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another thing...

When I want to use a different database, I believe the sql command is:

USE databaseName;

So when I do...

With the same connection object that was used to connect to first database,
It doesn't throw an exception, but it doesn't seem to do anything either,
as if its not in the databaseABC.

Is executeUpdate the correct method to call for this type of query?

Or should I do it manually, and just create a different new connection
to the desired database when the query starts with USE ?

Thanks
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colin shuker wrote:Or should I do it manually, and just create a different new connection
to the desired database when the query starts with USE ?

That's one possibility. Or you could have the user choose the database (from the list you just found out how to get) before entering any queries.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but if I'm in one database and I want to swap to another, should I handle "USE databaseName" separately to other queries, by setting up a new connection to the required database?

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic