Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

DatabaseMetaData question

  Asked By: Bryant    Date: Apr 26    Category: Java    Views: 587
  

is there a way using DatabaseMetaData to get all tables and
columns for a catalog and schema in one query?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Cheri Garcia     Answered On: Apr 26

shows the metadata in sql server2000 with
driver: - jdbcodbcdriver(sun.jdbc.odbc.JdbcOdbcDriver)
dsn name "test" and the
database pubs
(to create dsn go to settings>administrative
tools>datasources)
the code: -

---------------------------------------------------

/*
* name.java
*
* Created on January 5, 2002, 8:03 AM
*/


/**
*
*shows the metadata in sql server2000 with
*driver: -
jdbcodbcdriver(sun.jdbc.odbc.JdbcOdbcDriver)
*dsn name "test" and the
*database pubs
*(to create dsn go to settings>administrative
tools>datasources
* @author Pradyut Bhattacharya
* @version
*/
import java.sql.*;
import java.util.*;
public class name extends java.lang.Object {

/** Creates new name */
public name() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:odbc:test");
DatabaseMetaData m1 = con.getMetaData();

//getting schemas
ResultSet set = m1.getSchemas();
ResultSetMetaData s1 = set.getMetaData();
int j = s1.getColumnCount();

while(set.next())
{
System.out.println("The schema  is:
"+set.getString(1));
}

//getting catalogs
ResultSet cat = m1.getCatalogs();
ResultSetMetaData s2 = cat.getMetaData();
int j1 = s2.getColumnCount();

while(cat.next())
{
System.out.println("The catalog is:
"+cat.getString(1));

}
//getting column privileges
ResultSet col = m1.getColumnPrivileges("pubs",
"dbo", "authors", "phone");
ResultSetMetaData s3 = col.getMetaData();
int j2 = s3.getColumnCount();
//System.out.println("the column count is:
"+j2);
for (int j4=1; j4<=j2; ++j4)
{
if (j4<j2)

System.out.print(s3.getColumnName(j4)+" | ");
else

System.out.println(s3.getColumnName(j4));
}
while (col.next())
{
for (int j3 =1; j3<=j2; ++j3)
{
if (j3<j2)

System.out.print(col.getString(j3)+" | ");
else

System.out.println(col.getString(j3).trim());
}
}

//getting exported keys
ResultSet exp = m1.getExportedKeys("pubs",
"dbo", "authors");
ResultSetMetaData s4 = exp.getMetaData();
int y = s4.getColumnCount();


for (int j4=1; j4<=y; ++j4)
{
if (j4<y)

System.out.print(s4.getColumnName(j4)+" | ");
else

System.out.println(s4.getColumnName(j4));
}
while (exp.next())
{
for (int j3 =1; j3<=y; ++j3)
{
if (j3<y)

System.out.print(exp.getString(j3)+" | ");
else

System.out.println(exp.getString(j3).trim());
}
}


//getting cross reference
ResultSet cross = m1.getCrossReference("pubs",
"dbo", "authors", "pubs", "dbo", "employee");

ResultSetMetaData s5 = cross.getMetaData();
int z = s5.getColumnCount();
System.out.println("the no of columns are:
"+z);
for (int j4=1; j4<=z; ++j4)
{
if (j4<z)

System.out.print(s5.getColumnName(j4)+" | ");
else

System.out.println(s5.getColumnName(j4));
}
while (cross.next())
{
for (int j3 =1; j3<=y; ++j3)
{
if (j3<z)

System.out.print(cross.getString(j3)+" | ");
else

System.out.println(cross.getString(j3).trim());
}
}






System.out.println("Database:
"+m1.getDatabaseProductName());
System.out.println("version:
"+m1.getDatabaseProductVersion());
System.out.println("User Name:
"+m1.getUserName());
System.out.println("Catalog Seperator
"+m1.getCatalogSeparator());
System.out.println("Transaction Commit:
"+m1.dataDefinitionCausesTransactionCommit());
System.out.println("Procedure callable:
"+m1.allProceduresAreCallable());
System.out.println("Table Selectable:
"+m1.allTablesAreSelectable());
System.out.println("Major driver version:
"+m1.getDriverMajorVersion());
System.out.println("Minor driver version:
"+m1.getDriverMinorVersion());
System.out.println("Catalog term:
"+m1.getCatalogTerm());
System.out.println("Schema Term:
"+m1.getSchemaTerm());
System.out.println("Catalogs:
"+m1.getCatalogs());


}
catch(Exception e)
{
System.out.println(e);
}
}

/**
* @param args the command line arguments
*/
public static void main (String args[]) {

new name();
}

}

 
Didn't find what you were looking for? Find more on DatabaseMetaData question Or get search suggestion and latest updates.