jdbcを使用してデータベースの基礎情報(ソースコード)を取得する


プロジェクトがデータベースの基礎情報と付き合う場合は、データベースに何個のテーブルがあるか、どのテーブルに外部キーがあるか、これらの外部キーのフィールド名が何なのか、どのテーブルがどのテーブルに参照されているのかを取得します.どうしようかな?jdbcでは、Connectionでデータベースメタ情報オブジェクトDatabaseMetaDataを取得すれば、データベースの基礎情報を簡単に取得できます.次のコードの一部を示します.
(注:本人は直接自分のプロジェクトでユニットテストの方式を使って試験したので、読者がこの例を走る必要がある場合は、自分でConnectionを取得してください)、
package com.pay.jdbc;

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

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pay.core.Dao;

/**
 *
 */
@SuppressWarnings("unchecked")
public class TestTest
{
    private Connection conn;
    private static BeanFactory fac;
    private static Dao dao;

    @Before
    public void init()
    {
        try
        {
            fac = new ClassPathXmlApplicationContext(
                    "\\conf\\spring\\applicationContext*.xml");
            dao = (Dao) fac.getBean("dao");
            conn = dao.getJdbcTemplate().getDataSource().getConnection();
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }

    @Test
    public void testGetConnection()
    {
        System.out.println("dao: " + dao);
        System.out.println("conn: " + conn);
    }

    
    @Test
    public void testGetForeignKey() throws SQLException
    {
        List tableList=getAllTableName(conn);
        for (Object object : tableList)
        {
            System.out.println("tb: "+object);
        }
    }
    
    @Test
    public void getDatabaseMetaData() throws SQLException
    {
        DatabaseMetaData dbMetaData = conn.getMetaData();
        System.out.println("getUserName: "+dbMetaData.getUserName());
        System.out.println("getDatabaseProductName: "+dbMetaData.getDatabaseProductName());
        System.out.println("getDatabaseProductVersion: "+dbMetaData.getDatabaseProductVersion());
        System.out.println("getDriverMajorVersion: "+dbMetaData.getDriverMajorVersion());
        System.out.println("getDriverName: "+dbMetaData.getDriverName());
        System.out.println("getDriverVersion: "+dbMetaData.getDriverVersion());
        System.out.println("getCatalogTerm: "+dbMetaData.getCatalogTerm());//database
        
        ResultSet rs=dbMetaData.getPrimaryKeys(null, null, "admin_t");
        while (rs.next())
        {
            String PK_NAME=rs.getString("PK_NAME");
            String COLUMN_NAME=rs.getString("COLUMN_NAME");
            System.out.println("getPrimaryKeys: PK_NAME: "+PK_NAME);
            System.out.println("getPrimaryKeys: COLUMN_NAME: "+COLUMN_NAME);
        }
        
        rs=dbMetaData.getExportedKeys(null, null, "test_dept");
        while (rs.next())
        {
            System.out.println("

====getExportedKeys: "); String PKTABLE_CAT=rs.getString("PKTABLE_CAT");// String PKTABLE_SCHEM=rs.getString("PKTABLE_SCHEM"); String PKTABLE_NAME=rs.getString("PKTABLE_NAME"); String PKCOLUMN_NAME=rs.getString("PKCOLUMN_NAME"); String FKTABLE_NAME=rs.getString("FKTABLE_NAME"); String FKCOLUMN_NAME=rs.getString("FKCOLUMN_NAME"); String KEY_SEQ=rs.getString("KEY_SEQ"); String UPDATE_RULE=rs.getString("UPDATE_RULE"); String FK_NAME=rs.getString("FK_NAME"); String PK_NAME=rs.getString("PK_NAME"); String DEFERRABILITY=rs.getString("DEFERRABILITY"); System.out.println("getExportedKeys: PKTABLE_CAT: "+PKTABLE_CAT); System.out.println("getExportedKeys: PKTABLE_SCHEM: "+PKTABLE_SCHEM); System.out.println("getExportedKeys: PKTABLE_NAME: "+PKTABLE_NAME); System.out.println("getExportedKeys: PKCOLUMN_NAME: "+PKCOLUMN_NAME); System.out.println("getExportedKeys: FKTABLE_NAME: "+FKTABLE_NAME); System.out.println("getExportedKeys: FKCOLUMN_NAME: "+FKCOLUMN_NAME); System.out.println("getExportedKeys: KEY_SEQ: "+KEY_SEQ); System.out.println("getExportedKeys: UPDATE_RULE: "+UPDATE_RULE); System.out.println("getExportedKeys: FK_NAME: "+FK_NAME); System.out.println("getExportedKeys: PK_NAME: "+PK_NAME); System.out.println("getExportedKeys: DEFERRABILITY: "+DEFERRABILITY); } rs=dbMetaData.getImportedKeys(null, null, "test_person"); while (rs.next()) { System.out.println("

====getImportedKeys: "); String PKTABLE_NAME=rs.getString("PKTABLE_NAME"); String PKCOLUMN_NAME=rs.getString("PKCOLUMN_NAME"); String FKTABLE_NAME=rs.getString("FKTABLE_NAME"); String FKCOLUMN_NAME=rs.getString("FKCOLUMN_NAME"); String KEY_SEQ=rs.getString("KEY_SEQ"); System.out.println("getImportedKeys: PKTABLE_NAME: "+PKTABLE_NAME); System.out.println("getImportedKeys: PKCOLUMN_NAME: "+PKCOLUMN_NAME); System.out.println("getImportedKeys: FKTABLE_NAME: "+FKTABLE_NAME); System.out.println("getImportedKeys: FKCOLUMN_NAME: "+FKCOLUMN_NAME); System.out.println("getExportedKeys: KEY_SEQ: "+KEY_SEQ); } } /** * * @param cnn * @return * @throws SQLException */ private List getAllTableName(Connection cnn) throws SQLException { List tables = new ArrayList(); DatabaseMetaData dbMetaData = cnn.getMetaData(); // :"TABLE", "VIEW", "SYSTEM TABLE", // "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" String[] types = { "TABLE" }; ResultSet tabs = dbMetaData.getTables(null, null, null, types); while (tabs.next()) { tables.add(tabs.getObject("TABLE_NAME")); } return tables; } }