- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements