Description:
if there is a violation of foreign key constraint when importing a .txt file, it seems I cannot get the line number of the line which violate the FK constraint in the message of the JdbcSQLException.
I’ve uploaded the exceptions and codes onto the “mysticpaste” website, Please take a look, the information I want is like “error on line [line number] of file [file address]”, the derby provides this kind of information but the H2 doesn’t, do you know how to get this information?
Attachment:
click the following link to download a java project and import to you IDE, so that you can run the helloworld and get the exception I described.
https://rapidshare.com/files/3028771943/Test.rar
if you can run the above project, ignore the following links.
Links for Source codes and related exceptions:
h2’s helloWorld:
http://www.mysticpaste.com/view/13500
h2’s related exception:
http://www.mysticpaste.com/view/13502
derby’s helloWorld:
http://www.mysticpaste.com/view/13499
derby’s related exception:
http://www.mysticpaste.com/view/13503
Source codes:
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();
}
}
The easiest solution is to upgrade from H2 version 1.3.164 (what you are using) to H2 version 1.3.165 or newer. Version 1.3.165 includes the values of a referential constraint violation in the error message, as described in the change log: “In error messages about referential constraint violation, the values are now included.”