JDBC---PreparedStatement 批处理(二)

来源:互联网 发布:mac黄皮适合口红颜色 编辑:程序博客网 时间:2024/06/05 02:58
package mysql;import jdbc.utils.Utils;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class PsPc {/*为了避免代码重复书写,可以使用PreparedStatement实现批处理 * 优点:灵活,可以使用完整的SQL语句,也可与使用带参数的不完整SQL, * 不足:对于不完整的SQL语句,其具体内容是采用?占位符形式*/private static Statement stm;private static Connection co;public static void main(String args[]){try{ Connection co=null;     PreparedStatement stm=null; co=Utils.getConnection(); String sql="INSERT INTO users(name,password,email,birthday)"+"values(?,?,?,?)"; stm=(PreparedStatement)co.prepareStatement(sql); for(int i=0;i<3;i++){stm.setString(1, "name"+i);stm.setString(2, "password"+i);stm.setString(3, "email"+i+"@qq.cn");stm.setDate(4,Date.valueOf("2014-06-09") );stm.addBatch();stm.executeBatch();}//stm.executeBatch();}catch(Exception e){e.printStackTrace();}finally{Utils.release(null,stm,co);} }}

阅读全文
0 0
原创粉丝点击