JDBC实现MySQL插入大量数据(批量操作+事务提交)

public int jdbcInsert(String tabletarget, List> list, JSONArray jsonArray, String url, String username,
			String password) throws ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		url += "&rewriteBatchedStatements=true";
		String sqlinsert = " insert into " + tabletarget + " (";
		if (jsonArray != null && !"".equals(jsonArray)) {
			for (int i = 0; i < jsonArray.size(); i++) {
				Object o = jsonArray.get(i);
				JSONObject jsonObject = JSONObject.fromObject(o);
				String name = jsonObject.get("colName").toString();
				if (i == 0) {
					sqlinsert += name;
				} else {
					sqlinsert += "," + name;
				}
			}
			sqlinsert += ")values (";
			for (int i = 0; i < jsonArray.size(); i++) {
				if (i == 0) {
					sqlinsert += "?";
				} else {
					sqlinsert += ",?";
				}
			}
			sqlinsert += ")";
		}

		JDBCMySql jdbc = new JDBCMySql();
		Connection conn = jdbc.operateMySqlLocal(url, username, password);
		conn.setAutoCommit(false);
		PreparedStatement pstm = null;
		try {
			pstm = conn.prepareStatement(sqlinsert);
			
			int count=list.get(0).size();
			for (int i = 0; i < list.size(); i++) {
				for (int j = 0; j < count; j++) {
					//区分字段类型,set
					//pstm.setInt(j, 1);
					pstm.setObject(j+1,list.get(i).get(j));
				}
				pstm.addBatch();
			}
			pstm.executeBatch();
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}

		return 0;
	}

参考文档:

http://www.cnblogs.com/fnz0/p/5713102.html

private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true";
    private String user = "root";
    private String password = "123456";
    @Test
    public void Test(){
        Connection conn = null;
        PreparedStatement pstm =null;
        ResultSet rt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);        
            String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
            pstm = conn.prepareStatement(sql);
            conn.setAutoCommit(false);
            Long startTime = System.currentTimeMillis();
            Random rand = new Random();
            int a,b,c,d;
            for (int i = 1; i <= 100000; i++) {
                    pstm.setInt(1, i);
                    pstm.setInt(2, i);
                    a = rand.nextInt(10);
                    b = rand.nextInt(10);
                    c = rand.nextInt(10);
                    d = rand.nextInt(10);
                    pstm.setString(3, "188"+a+"88"+b+c+"66"+d);
                    pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);
                    pstm.addBatch();
            }
            pstm.executeBatch();
            conn.commit();
            Long endTime = System.currentTimeMillis();
            System.out.println("OK,用时:" + (endTime - startTime)); 
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            if(pstm!=null){
                try {
                    pstm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
    }

你可能感兴趣的:(JDBC实现MySQL插入大量数据(批量操作+事务提交))