⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metadata.java

📁 SearchPathServer
💻 JAVA
字号:
package testsuite;

import java.sql.*;


/**
 * @author Administrator
 */
public class Metadata
{
    //~ Instance/static variables .............................................

    static String DBUrl = "jdbc:mysql:///test";

    //~ Methods ...............................................................

    /**
     * DOCUMENT ME!
     * 
     * @param args DOCUMENT ME!
     * @throws Exception DOCUMENT ME!
     */
    public static void main(String[] args)
                     throws Exception
    {
        Connection conn = null;
        Statement stmt = null;
 
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(DBUrl);
            stmt = conn.createStatement();

            try {
                stmt.executeUpdate("DROP TABLE parent");
                stmt.executeUpdate("DROP TABLE child");
            } catch (SQLException sqlEx) {
            }

            stmt.executeUpdate("CREATE TABLE parent(id INT NOT NULL, PRIMARY KEY (id)) TYPE=INNODB");
            stmt.executeUpdate("CREATE TABLE child(id INT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id)) TYPE=INNODB");

            DatabaseMetaData dbmd = conn.getMetaData();
            ResultSet rs = dbmd.getImportedKeys(null, null, "child");

            while (rs.next()) {
                System.out.println(rs.getString("PKCOLUMN_NAME") + " -> " + 
                                   rs.getString("FKCOLUMN_NAME"));
            }

            rs.close();
            rs = dbmd.getExportedKeys(null, null, "parent");

            while (rs.next()) {
                System.out.println(rs.getString("PKCOLUMN_NAME") + " -> " + 
                                   rs.getString("FKCOLUMN_NAME"));
            }

            rs.close();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlEx) { /* ignore */
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException sqlEx) { /* ignore */
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -