2010-12-09 77 views

回答

0

正如注:Kunal說,你需要使用模式和表現場檢查需要嘗試檢查架構類

5

您可以使用JDBC元數據,以獲得有關此類事情的信息。

以下是您可以使用的示例片段。

儘管Oracle JDBC驅動程序用於測試,但它的供應商無關。 您可以使用其他數據庫供應商的代碼。

import java.sql.Connection; 
import java.sql.DatabaseMetaData; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

public class Test { 

    public static void main(String[] args) throws Throwable { 
     Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521/service.name", "username", "password"); 
     DatabaseMetaData meta = conn.getMetaData(); 
     String schemaName = "SCHEMA_NAME"; 
     List<String> tables = listAllTableNames(meta, schemaName); 
     printPrimaryKeys(schemaName, tables.iterator(), meta); 
     printUniqueIndexes(schemaName, tables.iterator(), meta); 
    } 

    private static List<String> listAllTableNames(DatabaseMetaData meta, String schemaName) throws SQLException { 
     List<String> list = new ArrayList<String>(); 
     ResultSet rs = meta.getTables(null, schemaName, null, new String[] { "TABLE" }); 
     while(rs.next()) { 
      String tableName = rs.getString("TABLE_NAME"); 
      list.add(tableName); 
     } 
     return list; 
    } 

    private static void printPrimaryKeys(String schemaName, Iterator<String> iter, DatabaseMetaData meta) throws SQLException { 
     while(iter.hasNext()) { 
      String tableName = iter.next(); 
      ResultSet rs = meta.getPrimaryKeys(null, schemaName, tableName); 
      while(rs.next()) { 
       String pkName = rs.getString("PK_NAME"); 
       String table = rs.getString("TABLE_NAME"); 
       String schema = rs.getString("TABLE_SCHEM"); 
       String columnName = rs.getString("COLUMN_NAME"); 
       System.out.println("----------------------------------------"); 
       System.out.println("Table: " + schema + "." + table); 
       System.out.println("Key Name: " + pkName); 
       System.out.println("Column(s): " + columnName); 
      } 
     }  
    } 

    private static void printUniqueIndexes(String schemaName, Iterator<String> iter, DatabaseMetaData meta) throws SQLException { 
     while(iter.hasNext()) { 
      String tableName = iter.next(); 
      boolean listUniqueIndex = true; 
      ResultSet rs = meta.getIndexInfo(null, schemaName, tableName, listUniqueIndex, true); 
      while(rs.next()) { 
       String indexName = rs.getString("INDEX_NAME"); 
       String table = rs.getString("TABLE_NAME"); 
       String schema = rs.getString("TABLE_SCHEM"); 
       String columnName = rs.getString("COLUMN_NAME"); 
       if(indexName == null) { 
        continue; 
       } 
       System.out.println("****************************************"); 
       System.out.println("Table: " + schema + "." + table); 
       System.out.println("Index Name: " + indexName); 
       System.out.println("Column Name: " + columnName); 
      } 
     } 
    } 
} 
0

同時檢查sourceforge上的DbTamer,包括源代碼。 http://sourceforge.net/projects/dbtamer/ 它旨在對數據庫進行反向工程,使其更容易在不同的RDBMS之間傳輸,但其核心是用於迭代表格並將表格元數據提取到可重用對象模型中的代碼。

相關問題