(JDBC)25_使用JDBC的批处理功能

public class TxTest {
    public static void main(String[] args) throws SQLException {
        test();
    }

    static void  insert(){
        Connection con=get_con();
        String sql="insert into usertr (name,birthday,money) values (?,?,?)";
        try {
            PreparedStatement pstmt=con.prepareStatement(sql);
            for(int i=0;i<10;i++){
                pstmt.setString(1, "liuliu");
                pstmt.setDate(2, new Date(System.currentTimeMillis()));
                pstmt.setInt(3, 500);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }


    static Connection get_con() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/new_schema", "root", "zxy123456");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }

}

 

posted @ 2017-07-19 12:38  z_dominic  阅读(79)  评论(0编辑  收藏  举报