我有一个包含SQL“insert”语句的文件。我打开这个文件,读取它然后尝试将其插入数据库(表已经存在)。陈述的形式如下:
Insert into table1 Select 1,....
Insert into table1 Select 2,....
Insert into table1 Select 3,....
;
';'在一堆线之后出现。
这些语句在实际的DB2服务器中运行良好,但在H2中不起作用。我也尝试过MODE = DB2,但仍然无法正常工作。
为了使它工作,我插入';'在每一行之后。所以,陈述成了:
Insert into table1 select 1,....;
Insert into table1 Select 2,....;
Insert into table1 Select 3,....;
;
当我尝试在H2控制台中插入这些语句时,它可以正常工作。但是在java程序中,由于连续两个';'而导致错误。
针对此问题的任何解决方案?
答案 0 :(得分:1)
我并不完全相信我理解你的“选择”之后会发生什么,因为我习惯于“值(...)”或引用另一个表的select语句,这些表通常可以制作成一个使用类似ROW_NUMBER()的语句显示的值。
如果您有大量插入,请尝试使用PreparedStatement的addBatch()方法。它正确实现时速度快,非常安全,对数据库资源的影响最小(如语句缓存)。
con.setAutoCommit(false);
PreparedStatement pstmt = con.prepareStatement("INSERT INTO table1 VALUES (?,?)");
pstmt.setInt(1, 1);
pstmt.setString(2, "example");
pstmt.addBatch();
pstmt.setInt(1, 2);
pstmt.setString(2, "I should really loop through an array here...");
pstmt.addBatch();
int[] updCnt = pstmt.executeBatch();
con.commit();