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!

3
  • your question is bit unclear. database means the schema or the table? Commented Apr 25, 2013 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"); Commented Apr 25, 2013 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? Commented Apr 25, 2013 at 11:56

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

@efoikonom, if this answer helps you out, mark it as right answer
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

1 Comment

Could you help me with code because i didn't understand you a lot?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.