sqlserver大數據插入

private boolean fillBeanDataTable(List<DWModel> 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章