How do I get all table names from database?

Category: java.sql, viewed: 6193 time(s).
package org.kodejava.example.sql;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GettingTableListExample {
	public static void main(String[] args) throws Exception {
		Connection connection = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String username = "kodejava";
			String password = "welcome";
			connection = DriverManager.getConnection(url, username, password);
			
			// Gets the metadata of the database
			DatabaseMetaData dbmd = connection.getMetaData();
			String[] types = {"TABLE"};
			
			ResultSet rs = dbmd.getTables(null, null, "%", types);
			while (rs.next()) {
				String tableCatalog = rs.getString(1);
				String tableSchema = rs.getString(2);
				String tableName = rs.getString(3);
				
				System.out.printf("%s - %s - %s%n", tableCatalog, tableSchema, tableName);
			}
		} catch (SQLException e) {
			if (connection != null && !connection.isClosed()) {
				connection.close();
			}
		}
	}
}

Here is the result

null - KODEJAVA - BOOKS
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats