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

编程入门 行业动态 更新时间:2024-10-13 18:24:24
本文介绍了如何正确使用AddBatch / withBatch将XML标记值批量插入到数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 'innerXml'是一个xml文件,包含大量的xml标签。我试图获取标签值并将它们转储到数据库表中。 我已经尝试了下面的代码,它工作正常。

innerXml.Row.each {Row - > $ b $ sql.execute(INSERT INTO tab1(col1,col2)VALUES($ {Row.Column0.text()},$ {Row.Column1.text()}))

但是,由于存在大量xml标签,导致性能问题逐一插入记录。正如专家建议我尝试使用withBatch来提高性能。请找到下面的代码,我尝试与批次:

sql.withBatch (386,插入到tab1(col1,col2)值(?,?)){ps-> innerXml.Row.each {Row - > ps.addBatch($ {Row.Column0.text()},$ {Row.Column1.text()})}}

但是我得到的错误是:

groovy。 lang.MissingMethodException:没有方法签名:Script41。$()适用于参数类型:(Script41 $ _run_closure2_closure3_closure4)values:[Script41 $ _run_closure2_closure3_closure4 @ 23724e8d]可能的解决方案:is(java.lang.Object),run(), 38行$($ b $ / code>

)上的run(),any(),use([Ljava.lang.Object;),任何(groovy.lang.Closure)

请帮忙

在此先感谢!!

解决方案

您需要做的:

// 386是一个奇数批量大小? sql.withBatch(386,'insert into tab1(col1,col2)values(?,?)'){ps - > innerXml.Row.each {row - > ps.addBatch(row.Column0.text(),row.Column1.text())} }

'innerXml' is a xml file with huge number of xml tags . I am trying fetch the tag values and dump them into database table . I have tried below code and it is working fine .

innerXml.Row.each { Row -> sql.execute("INSERT INTO tab1(col1,col2) VALUES (${Row.Column0.text()},${Row.Column1.text()} )")

But as there is huge number of xml tags its causing performance issue to insert record one by one . As the experts suggested I tried using withBatch to improve performance.Please find the below code which I tried with withBatch :

sql.withBatch(386, """ insert into tab1(col1,col2) values (?, ?) """) { ps -> innerXml.Row.each { Row ->ps.addBatch(${Row.Column0.text()} ,${Row.Column1.text()}) }}

But I am getting below error :

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

Please help

Thanks in Advance!!

解决方案

You need to do:

// 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()) } }

更多推荐

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

本文发布于:2023-10-28 04:38:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批量   标记   如何正确   数据库   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!