Share this page 

List tables in a databaseTag(s): JDBC


import java.net.URL;
import java.sql.*;

class JDBCapp  {
  static MyConnection theConn;

  public static void main (String args[]) {
    new JDBCapp().doit();
    } 
    
  public void doit() {
    theConn = new MyConnection();
    theConn.connect("myDSN", "user", "password");
    try {
     ResultSet rs1;
     ResultSet rs2;
     String ss, tblnames = "";
     DatabaseMetaData dmd = theConn.dbConn.getMetaData();
     rs1 = dmd.getSchemas();
     while(rs1.next()) {
       ss = rs1.getString(1);
       rs2 = dmd.getTables(null,ss, "%",null);
       while(rs2.next()) 
          tblnames += rs2.getString(3) + " " + rs2.getString(4) + "\n\r";
       }
     System.out.println("Tables :");     
     System.out.println(tblnames);
     }
    catch (Exception e) { e.printStackTrace(); }
   }
  }

class MyConnection {
  Connection dbConn = null;
  void connect(String db, String user, String passw) {
    try {
      Driver d = 
        (Driver)Class.forName
          ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
      // URL corresponding to the ODBC DSN
      String URL = "jdbc:odbc:" + db;
      // DB logon
      dbConn =
         DriverManager.getConnection(URL, user, passw); 
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }

  void disconnect() {
    try { 
      dbConn.close(); 
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }
  }