现在的位置: 首页 > 操作系统 > 正文

Java实现批量插入、修改数据到数据库中

2020年02月10日 操作系统 ⁄ 共 1064字 ⁄ 字号 评论关闭

在Java中使用JDBC实现批处理的对象一般是使用PrepareStatement对象。

如何使用:

Class.forName("Oracle.jdbc.driver.OracleDriver");Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:demo" , "scott" , "tiger");PreparedStatement ps = conn.prepareStatement("insert into dept2 values(? , ? , ?)");

for(Dept dept-> depts){ ps.setInt(1, dept.Id); ps.setString(2, dept.Name); ps.setString(3, dept.Description); ps.addBatch();}

ps.executeBatch();ps.close();

conn.close();

同样在批量更新时,也是使用这个PrepareStatement对象来处理的。

Class.forName("Oracle.jdbc.driver.OracleDriver");Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:demo" , "scott" , "tiger");PreparedStatement ps = conn.prepareStatement("update dept2 set name=? , description=? where id=?)");

for(Dept dept-> depts){ ps.setString(1, dept.Name); ps.setString(2, dept.Description); ps.setInt(3, dept.Id); ps.addBatch();}

ps.executeBatch();ps.close();

conn.close();

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2017-01/139915.htm

以上就上有关Java实现批量插入、修改数据到数据库中的相关介绍,要了解更多Java实现批量插入,Java实现批量插入、修改数据到数据库中,编程,Linux编程,Linux Shell,Android,Android教程,JAVA,C语言,Python,HTML5内容请登录学步园。

抱歉!评论已关闭.