java 批量执行存储过程_Java调用存储过程批量更新问题

Java调用存储过程批量更新问题

在java端的代码怎样写呢 传入的参数是什么集合类型呢

传入的参数和没有批量更新时是一样的,写法上有点不同而已。

正常java是如何调用存储过程,批量更新就是如何调用。

如果仔细看了上面的代码你也会发现。

如果是想写存储过程来实现批量更新,那也没必要,因为jdbc本身的batch就可以实现。

可以。我这里是insert,你改存储过程内部的语句就好了。

sql:

SQL code

create table parent(

id number(10),

name varchar2(100),

title varchar2(10)

);

create table child(

id number(10),

parent_id number(10),

child_name varchar2(100),

child_title varchar2(10),

child_content varchar2(200),

child_time timestamp

);

create sequence seq_p_c_id

minvalue 1

maxvalue 9999999999

start with 1

increment by 1

nocache;

drop type t_child_lst_map;

drop type t_child_lst;

drop type t_parent_lst;

create or replace type t_parent as object (

name varchar2(100),

title varchar2(10)

);

/

create or replace type t_child as object (

child_name varchar2(100),

child_title varchar2(10),

child_content varchar2(200)

);

/

create or replace type t_parent_lst as table of t_parent;

/

create or replace type t_child_lst as table of t_child;

/

create or replace type t_child_lst_map as table of t_child_lst;

/

create or replace procedure proc_ins_parent_child(

i_parent_lst in t_parent_lst,        --parent列表

i_child_map_lst in t_child_lst_map,  --child列表集合,一个map元素对应一个child_lst,其下标与 parent列表的下标相同。

o_ret out number

) as

var_parent t_parent;

var_child_lst t_child_lst;

var_child t_child;

var_parent_id number;

var_child_id number;

begin

for i in 1..i_parent_lst.count loop

--取得parent各列的值

var_parent := i_parent_lst(i);

--取得parent_id;

select seq_p_c_id.nextVal into var_parent_id from dual;

--插入parent表

insert into parent(

id,

name,

title

)

values(

var_parent_id,

var_parent.name,

var_parent.title

);

--取得该parent对应的child列表

var_child_lst := i_child_map_lst(i);

for j in 1..var_child_lst.count loop

var_child := var_child_lst(j);

--取得child_id;

select seq_p_c_id.nextVal into var_child_id from dual;

--插入child表

insert into child(

id,

parent_id,

child_name,

child_title,

child_content,

child_time

)

values(

var_child_id,

var_parent_id,

var_child.child_name,

var_child.child_title,

var_child.child_content,

systimestamp

);

end loop;

end loop;

o_ret := 0;

exception when others then

begin

o_ret := -1;

raise;

end;

end proc_ins_parent_child;

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以参考以下代码实现Java读取文件并批量保存到数据库: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class BatchInsert { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; try (Connection conn = DriverManager.getConnection(url, user, password)) { String sql = "INSERT INTO mytable (name, age) VALUES (?, ?)"; PreparedStatement statement = conn.prepareStatement(sql); File file = new File("data.txt"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { String[] parts = line.split(","); String name = parts[0]; int age = Integer.parseInt(parts[1]); statement.setString(1, name); statement.setInt(2, age); statement.addBatch(); } statement.executeBatch(); System.out.println("Data inserted successfully!"); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先建立了与数据库的连接,然后定义了一个 SQL 语句,并使用 PreparedStatement 对象进行了预编译。 接着,我们使用 BufferedReader 对象读取文件的每一行数据,并将其分割为姓名和年龄两个部分。然后,我们将这些数据设置到 PreparedStatement 对象中,并使用 addBatch() 方法将其添加到批处理中。 最后,我们调用 executeBatch() 方法将批处理中的所有 SQL 语句一次性执行。 当然,您需要根据您的具体情况修改代码中的数据库连接信息、SQL 语句、数据文件路径等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值