Home   Family   Softwares   Resume   Activities  


//Display Tables Present In A Database

import java.sql.*;
import java.io.DataInputStream;
import java.awt.*;

public class Database
{
public static void main(String args[]) throws Exception
{
// Attempt to load database driver
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
}
catch (ClassNotFoundException cnfe) // driver not found
{
System.err.println ("Unable to load database driver");
System.err.println ("Details : " + cnfe);
System.exit(0);
}

// Create a URL that identifies database
// Here MySQLDSN is DSN
String url = "JDBC:ODBC:MySQLDSN";


try
{
// Now attempt to create a database connection
Connection con = DriverManager.getConnection
(url, "priya", "admin");

Choice tableNames = new Choice();
DatabaseMetaData md = con.getMetaData();
String[] types = { "TABLE" };
ResultSet mrs = md.getTables( null, "", "", types );
while ( mrs.next() )
{
tableNames.addItem( mrs.getString( 3 ) );
}
int numCols = tableNames.getItemCount();
System.out.println(" ");
System.out.println("TABLES PRESENT IN THIS DATABASE");
System.out.println(" ");
int i = 0;
for (i = 1; i <= numCols-1; i++)
{
System.out.print(tableNames.getItem(i));
System.out.println(" ");
}

mrs.close(); con.close(); }
catch(SQLException ex)
{ System.err.print("SQLException: "); System.err.println(ex.getMessage()); }

}

}

1
Hosted by www.Geocities.ws