Advertisement
zubayer007

MySQL Schema Tables Column Finder

Sep 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DatabaseMetaData;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. /*
  11.  * To change this license header, choose License Headers in Project Properties.
  12.  * To change this template file, choose Tools | Templates
  13.  * and open the template in the editor.
  14.  */
  15. /**
  16.  *
  17.  * @author Zubayer
  18.  */
  19. public class Schemas_Tables_Columns_Finder_MySQL {
  20.  
  21.     static {
  22.         try {
  23.             Class.forName("com.mysql.jdbc.Driver");
  24.         } catch (ClassNotFoundException ex) {
  25.             Logger.getLogger(Schemas_Tables_Columns_Finder_MySQL.class.getName()).log(Level.SEVERE, null, ex);
  26.         }
  27.     }
  28.  
  29.     public static Connection con;
  30.  
  31.     public static Connection getConnection() throws SQLException {
  32.         con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "1234");
  33.         return con;
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.         //show All Schema names
  38.         try {
  39.             DatabaseMetaData metadata = getConnection().getMetaData();
  40.             ResultSet rs = metadata.getCatalogs();
  41.             while (rs.next()) {
  42.                 System.out.println("TABLE_CAT = " + rs.getString("TABLE_CAT"));
  43.             }
  44.         } catch (SQLException ex) {
  45.             ex.printStackTrace();
  46.         }
  47.  
  48.         //show all table Names under one schema
  49.         String[] types = {"TABLE"};
  50.         try {
  51.             Connection con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/fastfood", "root", "1234");
  52.             ResultSet rs = con2.getMetaData().getTables(null, null, null, types);
  53.             String table_name = "";
  54.             System.out.println("\nBegin table names under specific schema");
  55.             while (rs.next()) {
  56.                 table_name = rs.getString("TABLE_NAME");
  57.                 System.out.println("Table Name: " + table_name);
  58.                
  59.                 //show all columns name nder one table
  60.                 String columnName = "";
  61.                 ResultSet rs2 = con2.getMetaData().getColumns(null, null, table_name, null);
  62.                 System.out.println("columns in " + table_name + " table");
  63.                 while(rs2.next()){
  64.                     columnName = rs2.getString("COLUMN_NAME");
  65.                     System.out.println("-----  " + columnName);
  66.                 }
  67.                 System.out.println();
  68.             }
  69.         } catch (SQLException ex) {
  70.             ex.printStackTrace();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement