0

説明: .txt ファイルのインポート時に外部キー制約違反があった場合、JdbcSQLException のメッセージで FK 制約に違反している行の行番号を取得できないようです。例外とコードを「mysticpaste」の Web サイトにアップロードしました。ご覧ください。必要な情報は、「ファイル [ファイル アドレス] の [行番号] 行でエラー」のようなものです。ダービーはこの種の情報を提供しますが、 H2にはありません。この情報を取得する方法を知っていますか?

添付ファイル: 次のリンクをクリックして Java プロジェクトをダウンロードし、IDE にインポートして、helloworld を実行し、説明した例外を取得できるようにします。

https://rapidshare.com/files/3028771943/Test.rar

上記のプロジェクトを実行できる場合は、次のリンクを無視してください。

ソース コードと関連する例外へのリンク:

h2 の helloWorld: http://www.mysticpaste.com/view/13500

h2 に関連する例外: http://www.mysticpaste.com/view/13502

ダービーの helloWorld: http://www.mysticpaste.com/view/13499

ダービー関連の例外: http://www.mysticpaste.com/view/13503

ソースコード:

public class HelloWorld_h2 {

    /**
     * this method is to display an error message when there is foreign key constraint violation.
     *
     * @param args ignored
     */
    public static void main(String... args) throws Exception {
        // delete the database named 'test' in the user home directory
        DeleteDbFiles.execute("~", "test", true);

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/test");
        Statement stat = conn.createStatement();

        // this line would initialize the database
        // from the SQL script file 'init.sql'
        // stat.execute("runscript from 'init.sql'");

        stat.execute("create table test1(" +
                "id int primary key, " +
            "name varchar(255))");

        stat.execute("create table test2(" +
            "id int primary key, " +
            "name varchar(255)," +
            "constraint fk_test2 foreign key (name) references test1(name))");

        stat.execute("insert into test1 (select * from CSVREAD("+"'classpath:test1.txt'"+",'id,name','charset=UTF-8 fieldSeparator=,'))");
        stat.execute("insert into test2 (select * from CSVREAD("+"'classpath:test2.txt'"+",'id,name','charset=UTF-8 fieldSeparator=,'))");

        stat.close();
        conn.close();
    }
}

public class HelloWorld_derby {

    /**
     * this method is to display an error message when there is foreign key constraint violation.
     *
     * @param args ignored
     */
    public static void main(String... args) throws Exception {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.getConnection("jdbc:derby:memory:test;create=true;","","");
        Statement stat = conn.createStatement();

        // this line would initialize the database
        // from the SQL script file 'init.sql'
        // stat.execute("runscript from 'init.sql'");

        stat.execute("create table test1(" +
            "id int primary key, " +
            "name varchar(255) unique)");

        stat.execute("create table test2(" +
            "id int primary key, " +
            "name varchar(255)," +
            "constraint fk_test2 foreign key (name) references test1(name))");

        String classpath = new HelloWorld_derby().getClass().getResource("/").getFile();
        String test1_url = classpath + "test1.txt";
        String test2_url = classpath + "test2.txt";

        CallableStatement c1 = conn.prepareCall("CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE (null,'TEST1','"+test1_url+"',',','\"','UTF-8',1 )");
        c1.execute();
        CallableStatement c2 = conn.prepareCall("CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE (null,'TEST2','"+test2_url+"',',','\"','UTF-8',1 )");
        c2.execute();

        stat.close();
        conn.close();
    }
}
4

1 に答える 1

1

最も簡単な解決策は、H2 バージョン 1.3.164 (使用しているもの) から H2 バージョン 1.3.165 以降にアップグレードすることです。バージョン 1.3.165 では、変更ログで説明されているように、エラー メッセージに参照制約違反の値が含まれています。「参照制約違反に関するエラー メッセージに、値が含まれるようになりました。」

于 2012-05-04T08:27:32.193 に答える