JDBC-批量插入sql语句

/***
 * 创建数据库测试批量插入数据
 *
 * 在数据库xzit创建数据库表格person:
 * create person(
 * id varchar(255),
 * name vaaechar(255),
 * sex varchar(2),
 * age int
 * );
 *
 */
public class PersonDao {
    /**
     * 测试jdbc批处理插入数据
     *
     * @param persons
     */
    public void testButchInsert ( List<Person> persons ) {
        String sql = "insert into person values (?,?,?,?)";
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            //获取数据库连接
            conn = JDBCUtil.getConnection();

            //获取数据库操作对象
            ps = conn.prepareStatement(sql);

            for (Person p : persons) {
                ps.setString(1 , p.getId());
                ps.setString(2 , p.getName());
                ps.setString(3 , p.getSex());
                ps.setInt(4,p.getAge());
                /**将以上设置添加到命令列表中*/
                ps.addBatch();
            }
            //执行批处理命令,提交数据库执行sql操作
            int[] res = ps.executeBatch();
            for (int i = 0; i < res.length; i++) {
                System.out.println("" + (i + 1) + "条sql数据,影响数据行数是:" + res[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
                conn.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Person {
    private String id;
    private String name;
    private String sex;
    private int age;

    /**
     * 无参构造,系统虽然默认提供,但是一旦写了有参构造方法这个就被覆盖。
     */
    public Person(){}

    /**
     * 有参数构造方法
     * @param id
     * @param name
     * @param sex
     * @param age
     */
    public Person(String id,String name,String sex,int age){
        this.id=id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getId () {
        return id;
    }

    public void setId ( String id ) {
        this.id = id;
    }

    public String getName () {
        return name;
    }

    public void setName ( String name ) {
        this.name = name;
    }

    public String getSex () {
        return sex;
    }

    public void setSex ( String sex ) {
        this.sex = sex;
    }

    public int getAge () {
        return age;
    }

    public void setAge ( int age ) {
        this.age = age;
    }
}
public class TestButch {
    public static void main ( String[] args ) {
        Person person1= new Person(UUID.randomUUID().toString(),
                "任我行","",99);
        Person person2 = new Person(UUID.randomUUID().toString(),
                "令狐冲","",99);
        Person person3= new Person(UUID.randomUUID().toString(),
                "东方不败","",99);
        Person person4 = new Person(UUID.randomUUID().toString(),
                "阳顶天","",99);
        Person person5 = new Person(UUID.randomUUID().toString(),
                "任盈盈","",99);
        List<Person> personList = new ArrayList<>();
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        personList.add(person5);
        new PersonDao().testButchInsert(personList);
    }
}

 

posted @ 2022-04-17 22:17  280887072  阅读(214)  评论(0编辑  收藏  举报