Tuesday, November 11, 2014

Batch Insert In MySQL using Java.



SQL Injection Safe Batch


import java.sql.Connection;
import java.sql.PreparedStatement;
String sql = "insert into student (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
for (Student stud: studentDB) {
    ps.setString(1, stud.getName());
    ps.setString(2, stud.getCity());
    ps.setString(3, stud.getPhone());
    ps.addBatch();
}
ps.executeBatch();
ps.close();
connection.close();        




No comments:

Post a Comment