Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Zubayer
- */
- public class Schemas_Tables_Columns_Finder_MySQL {
- static {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Schemas_Tables_Columns_Finder_MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static Connection con;
- public static Connection getConnection() throws SQLException {
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "1234");
- return con;
- }
- public static void main(String[] args) {
- //show All Schema names
- try {
- DatabaseMetaData metadata = getConnection().getMetaData();
- ResultSet rs = metadata.getCatalogs();
- while (rs.next()) {
- System.out.println("TABLE_CAT = " + rs.getString("TABLE_CAT"));
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- //show all table Names under one schema
- String[] types = {"TABLE"};
- try {
- Connection con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/fastfood", "root", "1234");
- ResultSet rs = con2.getMetaData().getTables(null, null, null, types);
- String table_name = "";
- System.out.println("\nBegin table names under specific schema");
- while (rs.next()) {
- table_name = rs.getString("TABLE_NAME");
- System.out.println("Table Name: " + table_name);
- //show all columns name nder one table
- String columnName = "";
- ResultSet rs2 = con2.getMetaData().getColumns(null, null, table_name, null);
- System.out.println("columns in " + table_name + " table");
- while(rs2.next()){
- columnName = rs2.getString("COLUMN_NAME");
- System.out.println("----- " + columnName);
- }
- System.out.println();
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement