0

I would like to make o program that would store data from excel files in databases. I have plenty of databases so in my program i have to choose in which one I will store the data. I have made the code to be able to connect mysql with my program and to show the available databases. What I would like to do now is to say in which database i would store the data. To be more specific I would like the user first of all to see tha available databases in his client and afterwards he would have the chance to say in which database the data would be stored. Could anyone help me how I would do this?

The code to see all the available databases is the below:

Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/", "root", "root");
        DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
        ResultSet res = meta.getCatalogs();
        System.out.println("List of the databases: ");
        while (res.next()){
            System.out.println (" " +res.getString(1));
    }

Thank you in advance!

dedmar
  • 401
  • 3
  • 12
  • 22
  • your question is bit unclear. database means the schema or the table? – Bhavik Shah Apr 25 '13 at 11:51
  • You can set database in this line (_after user selected one_) - `Connection con = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost:3306/SelectedDatabase", "root", "root");` – Adil Shaikh Apr 25 '13 at 11:53
  • How i would give the user the ability to choose?What i have to write that would be shown in client so that the user would type the one he want? – dedmar Apr 25 '13 at 11:56

2 Answers2

0

I hope this SO link should help you . You can get all the data from the connection object

Community
  • 1
  • 1
Arun
  • 3,440
  • 11
  • 60
  • 108
0

First do a create a simple connection

Connection con = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost:3306/", "root", "root");

Look for an example here

see how he uses ResultSet Class to access the result.

Now,

retrieve information from information_schema.SCHEMATA so you can query like

SELECT schema_name FROM information_schema.SCHEMATA S;

to get all the schemas

Next after getting choice from user(maybe from console) you can set database according to uservalue using Connection#setCatalog()

If you want table information use this query

SELECT * FROM information_schema.TABLES T;

It would list all the tables in all schemas

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40