Computer >> Computer tutorials >  >> Programming >> MySQL

Retrieving MySQL Database structure information from Java?


Use DatabaseMetaData class to retrieve MySQL database structure. In this example, we will display all the table names of database “web” using Java with the help of getMetaData().

Following is the Java code −

Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.jdbc.DatabaseMetaData;

public class getDatabaseInformationDemo {
   public static void main(String[] args) {
      Connection con = null;
      try {
         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false", "root", "123456");
         DatabaseMetaData information = (DatabaseMetaData) con.getMetaData();
         String allTableName[] = {
            "TABLE"
         };
         ResultSet r = information.getTables(null, null, null, allTableName);
         while (r.next()) {
            System.out.println(r.getString(3));
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
}

This will produce the following output −

Output

demotable211
demotable212
demotable213
demotable214
demotable215
demotable216
name
select