- 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
How to get all table names from a database using JDBC?
You can get the list of tables in the current database in MySQL using the SHOW TABLES query.
Show tables;
Following JDBC program retrieves the list of tables in the database by executing the show tables query.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ListingTables { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Retrieving the data ResultSet rs = stmt.executeQuery("Show tables"); System.out.println("Tables in the current database: "); while(rs.next()) { System.out.print(rs.getString(1)); System.out.println(); } } }
Output
Connection established...... cricketers_data customers dispatches_data employee_data myplayers sales test tutorials_data
Or, You can use the getTables() method of the DatabaseMetaData interface.
Example
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RS_getTables { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData(); String[] types = {"TABLE"}; //Retrieving the columns in the database ResultSet tables = metaData.getTables(null, null, "%", types); while (tables.next()) { System.out.println(tables.getString("TABLE_NAME")); } } }
Output
Connection established...... cricketers_data customers dispatches_data employee_data myplayers sales test tutorials_data
- Related Articles
- How to get all the column names from a ResultSet using JDBC
- How to drop a table from a database using JDBC API?
- How to drop a table from Oracle database using JDBC API?
- How to retrieve table names from a database in MySQL?
- How to create a table in a database using JDBC API?
- How to retrieve a record from an existing table in a database using JDBC API?
- How to remove a record from an existing table in a database using JDBC API?
- How to delete a column from an existing table in a database using JDBC API?
- How to get the current value of a particular row from a database using JDBC?
- How to delete all records from a table in Oracle using JDBC API?
- How to retrieve a record from an existing table in oracle database using JDBC API?
- How to remove a record from an existing table in oracle database using JDBC API?
- How to drop a table from JavaDB using JDBC?
- How to insert a record into a table in a database using JDBC API?
- How to get username using ID from another table in MySQL database?

Advertisements