SQL批量同步数据的解决方案
1、实例:
public class Demo{
//定义mysql的 数据库驱动程序,
public static final String Driver =“com.mysql.jdbc.Driver”;
//定义数据库的连接地址,数据库中的user表格
public static final String url="jdbc:mysql://localhost:3306/user";

2、//数据库的连接用户名
public static final String user =“root”;
//数据库的连接密码
public static final password=“123456”;
//这些都是连接数据库所用到的我们也可以把这些写入properties配置文件中,用的时候直接调用,在之前的经验中可以找到;

3、public static void main(String args[])throws Exception{
//数据库连接
Connection con=null;
//数据库操作
PreparedStatement pstm=null;
//此处使用预处理操作
String sql="insert into user(name,password,age,sex,birthday) values(?,?,?,?,?)"

4、//加载驱动程序
Class.forName(Driver);
//使用mysql数据库时,要写上连接的用户名和密码
con=DriverManger.getConnection(url,user,password);
//实例化对象
pstm=con.preparedStatement(sql);

5、//加入批处理等待执行
for(int i=0;i<10;i++){
pstm.setString(1,"A-"+i);
pstm.setSting(2,"a-"+i);
pstm.setInt(3,20+i);
pstm.setString(4,"男");
pstm.setDate(5,new Date());
pstm.addBatch();
}}

6、//批量执行
pstm.executeBatch();
//数据库中的连接消耗资源,用过记得关闭
con.close();
}
