Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List tables in a database
12-21-2010, 07:30 AM
Post: #1
List tables in a database
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();
}
}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved