通过Java向Oracle数据库添加10W条数据

public void add() throws SQLException {
	Connection conn = DBUtil.getConnection();
	conn.setAutoCommit(false);  //关闭自动提交
	String sql = "" +
		 " insert into pf_bookinfo " +
		 " (bookname,author,publisher,language,genre,ISBN,floor,room,shelf,islent) " +
		 " values( " +
		 " ?,?,?,'中文','小说',?,2,'02','B','可借') ";

	PreparedStatement pstmt = conn.prepareStatement(sql);

	for(int i=1; i<=100000; i++) {
		pstmt.setString(1, Integer.toString(i));
		pstmt.setString(2, Integer.toString(i));
		pstmt.setString(3, Integer.toString(i));
		pstmt.setString(4, Integer.toString(i));
		pstmt.addBatch();  //把一条数据存入batch中

		if(i%5000==0){
			pstmt.executeBatch();
			conn.commit();  //batch中每满5000条数据提交一次
			pstmt.clearBatch();
		}
	}

	pstmt.executeBatch();
	conn.commit();  //提交最后不满5000条的数据
	pstmt.clearBatch();

	pstmt.close();
	conn.close();
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注