0

说明:如果导入.txt文件时违反外键约束,在JdbcSQLException的消息中似乎无法获取违反FK约束的行号。我已经将异常和代码上传到“mysticpaste”网站上,请看一下,我想要的信息就像“文件[文件地址]的第[行号]行错误”,德比提供了这种信息但是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 回答