简体   繁体   中英

How to execute MySQL query, “show tables;” inside a Java Program..?

I am a newbie to Java and MySQL. Please pardon me if my question seems silly, I want to know the answer... I have gone through many articles and questions asked in forums, but I didn't see a relevant answer for my question... That is, I have made a switch statement in Java and I want to show the list of available tables in a database if I press 1( that is go into the case 1 and execute a query "show tables;" ) In MySQL Console, it is easy to check for available tables using the same query. But I want to know whether "show tables;" query or similar queries can be executed inside a Java Program... Here's a sample snippet of my code,

Connection con=null;
String url = "jdbc:mysql://localhost:3306/giftson";
String dbName = "giftson";
String userName = "root";
String password = "password";
con=DriverManager.getConnection(url,userName,password);
Statement st=con.createStatement();
//String query;
Statement st=con.createStatement();
System.out.println("\tDatabase Connection for Various Operations");
System.out.println("\n1. Show list of tables\n2. Show contents of Table\n3. Create New Table\n4. Insert into table\n5. Update Table\n6. Delete From Table\n7. Exit\n");
System.out.println("Enter your option Number  ");
DataInputStream dis=new DataInputStream(System.in);
int ch=Integer.parseInt(dis.readLine());
switch(ch)
{
    case 1:
        System.out.println(" You have selected to Show list of available tables");
        //ResultSet rs=st.executeQuery("Show tables");
        //while(rs.next())
        //{
        //    System.out.println("List of Tables\n" +rs.getString("?????"));
        //}
        break;
}

from the above piece of code, If I execute the query in ResultSet, How do I print the values inside the while loop..? In rs.getString(); we can only pass either the column index or the column label as argument, but how do I get the list of tables... what do I enter in place of "?????" inside print statement...? please do help me, keeping in mind that you are explaining for a beginner... Thanks in advance...!

We can use the console commands using,

DatabaseMetaData meta=getMetaData();

In the below code, it is shown that there are many ways (but I came to know two ways) of getting the list of tables

        DatabaseMetaData meta = con.getMetaData();
        ResultSet rs1 = meta.getTables(null, null, null,new String[] {"TABLE"});
        ResultSet rs2 = meta.getTables(null, null,"%", null);
        System.out.println("One way of Listing Tables");
        while (rs1.next())
        {
        System.out.println(rs1.getString("TABLE_NAME"));
        }
        System.out.println("Another way of Listing Tables");
        while(rs2.next())
        {
        System.out.println(rs2.getString(3));                
        }

A small example would be

String tableNamePattern  = "%_Assessment_" + session + "_" + year;
DatabaseMetaData databaseMetaData = conn.getMetaData();
ResultSet rs = databaseMetaData.getTables(null, null, tableNamePattern, 
                                      null);
while(rs.next()) {
String tableName = rs.getString("TABLE_NAME");
...
}

Check the source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM