sqlserver大数据插入

private boolean fillBeanDataTable(List inputList, String tableName, String synvField, String sqlConn, String jobName) {

    boolean returnFlag = false;

    //准入条件判断
    if (inputList == null || inputList.size() == 0 || tableName.isEmpty()
            || sqlConn.isEmpty()) {
        returnFlag = false;
    } else {
        log.info("确定导入数据个数" + inputList.size());
    }
    String[] con = sqlConn.split("=|;");
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://" + con[1] + ":1433;database=" + con[3];
        String user = con[7];
        String password = con[9];
        Connection connection = DriverManager.getConnection(url, user, password);

        //大数据插入方式
        String sql1 = "";
        String sql2 = "";
        connection.setAutoCommit(false);
        sql1 = "LogTime," +
                "RecommendNo," +
                "ChineseName," +
                "CardNo," +
                "TerminalNo," +
                "BeginTime," +
                "EndTime," +
                "ExchangeType," +
                "ExchangeMoney," +
                "CommitType," +
                "StructID," +
                "ScanFlag";
        sql2 = "? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ";
        String sql = "insert into " + tableName + "(" + sql1 + ") values(" + sql2 + ")";
        PreparedStatement ps = connection.prepareStatement(sql);

        for (DWModel dwModel : inputList) {
            //int i = 12;
            ps.setObject(1, dwModel.getLogTime());
            ps.setObject(2, dwModel.getRecommendNo());
            ps.setObject(3, dwModel.getChineseName());
            ps.setObject(4, dwModel.getCardNo());
            ps.setObject(5, dwModel.getTerminalNo());
            ps.setObject(6, dwModel.getBeginTime());
            ps.setObject(7, dwModel.getEndTime());
            ps.setObject(8, dwModel.getExchangeType());
            ps.setObject(9, dwModel.getExchangeMoney());
            ps.setObject(10, dwModel.getCommitType());
            ps.setObject(11, dwModel.getStructID());
            ps.setObject(12, dwModel.getScanFlag());
            ps.addBatch();
           /* if (i >= 10000) {
                ps.executeBatch();
                connection.commit();
                ps.clearParameters();
            }*/
        }
        ps.executeBatch();
        connection.commit();
        returnFlag = true;
        //插入完成后休眠
        //log.info(jobName + "批量插入数据库完成,总插入" + inputList.size());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (ClassNotFoundException e) {
        log.error(jobName + "插入数据库时发生异常" + e.getMessage() + e.getStackTrace());
        returnFlag = false;
        e.printStackTrace();
    } catch (SQLException e) {
        log.error(jobName + "插入数据库时发生异常" + e.getMessage() + e.getSQLState());
        returnFlag = false;
        e.printStackTrace();
    } catch (Exception e) {
        log.error(jobName + "插入数据库时发生异常" + e.getMessage() + e.getStackTrace());
        returnFlag = false;
        e.printStackTrace();
    }
    return returnFlag;
}

你可能感兴趣的:(sqlserver大数据插入)