Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确使用AddBatch/withBatch将xml标记值批量插入数据库表_Xml_Database_Oracle_Groovy_Soapui - Fatal编程技术网

如何正确使用AddBatch/withBatch将xml标记值批量插入数据库表

如何正确使用AddBatch/withBatch将xml标记值批量插入数据库表,xml,database,oracle,groovy,soapui,Xml,Database,Oracle,Groovy,Soapui,“innerXml”是一个包含大量xml标记的xml文件。我正在尝试获取标记值并将它们转储到数据库表中。 我已经尝试了下面的代码,它工作得很好 innerXml.Row.each { Row -> sql.execute("INSERT INTO tab1(col1,col2) VALUES (${Row.Column0.text()},${Row.Column1.text()} )") 但由于有大量的xml标记,所以逐个插入记录会导致性能问题。正如专家建议的那样,我尝试使用w

“innerXml”是一个包含大量xml标记的xml文件。我正在尝试获取标记值并将它们转储到数据库表中。 我已经尝试了下面的代码,它工作得很好

innerXml.Row.each { Row ->

sql.execute("INSERT INTO tab1(col1,col2) VALUES (${Row.Column0.text()},${Row.Column1.text()} )")    
但由于有大量的xml标记,所以逐个插入记录会导致性能问题。正如专家建议的那样,我尝试使用withBatch来提高性能。请查找我尝试使用withBatch的以下代码:

sql.withBatch(386, """  insert into tab1(col1,col2) values (?, ?) """) { ps ->   innerXml.Row.each { Row ->ps.addBatch(${Row.Column0.text()} ,${Row.Column1.text()})  }}
但我得到了以下错误:

groovy.lang.MissingMethodException: No signature of method: Script41.$() is applicable for argument types: (Script41$_run_closure2_closure3_closure4) values: [Script41$_run_closure2_closure3_closure4@23724e8d] Possible solutions: is(java.lang.Object), run(), run(), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure) error at line: 38
请帮忙

提前谢谢

您需要执行以下操作:

// 386 is an odd batch size?
sql.withBatch(386, 'insert into tab1(col1,col2) values (?, ?)') { ps ->
    innerXml.Row.each { row ->
        ps.addBatch(row.Column0.text(), row.Column1.text())
    }
}

你太棒了,蒂姆!!成功了!但是当我这样做的时候:sql.withBatch(386),“insert into tab1(col1,col2)values(?)”{ps->innerXml.Row.each{Row->ps.addBatch(1001111)…它工作了。你能澄清一下原因吗}}我不确定为什么我不能对你的答案进行投票:(@joe问题是你不需要用
${…}来包装你的值。)
类似于
${Row.Column0.text()}
当您不在模板字符串中时好的。明白了!我需要给出386吗?386是这里的行数。我已经检查过,即使我没有提到386,表也会被填充。386是批的大小,因此每次您达到386项时,批都会被处理,并创建一个新批。如果您忽略它,它会恢复为默认值(为0),因此所有项目都将放在一个批处理中。这对于您的情况很好(因为您有少量元素),但如果您有数百万个元素,则需要设置批处理大小以减少内存使用