Discuss / Java / batch

batch

Topic source

fadefresh

#1 Created at ... [Delete] [Delete and Lock User]
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO students (name, gender, grade, score) VALUES (?, ?, ?, ?)")) {
    // 对同一个PreparedStatement反复设置参数并调用addBatch():
    for (String name : names) {
        ps.setString(1, name);
        ps.setBoolean(2, gender);
        ps.setInt(3, grade);
        ps.setInt(4, score);
        ps.addBatch(); // 添加到batch
    }
    // 执行batch:
    int[] ns = ps.executeBatch();
    for (int n : ns) {
        System.out.println(n + " inserted."); // batch中每个SQL执行的结果数量
    }
}

for循环里的name,gender,grade,score应该来自某种数据结构吧,但这四个参数肯定不是都来自于names吧?这里应该说清楚的

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

给你换了一种写法

🌙

#3 Created at ... [Delete] [Delete and Lock User]

哈哈,有点较真,

🌙

#4 Created at ... [Delete] [Delete and Lock User]

不过,确实有点跳。。。

好好怎么参数就变成对象了。。。

不过都学到这里了,不至于看不懂吧。。。

感觉好多人,这一章都不看吧。直接学框架去了。


  • 1

Reply