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
Updated on: 2019-07-30T22:30:26+05:30

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements