logback中批量插入数据库的参考代码

时间:2023-03-09 07:55:42
logback中批量插入数据库的参考代码
protected void insertProperties(Map<String, String> mergedMap, Connection connection, long eventId) throws SQLException {
        Set<String> propertiesKeys = mergedMap.keySet();
        if (propertiesKeys.size() > 0) {
            PreparedStatement insertPropertiesStatement = null;
            try {
                insertPropertiesStatement = connection.prepareStatement(insertPropertiesSQL);

                for (String key : propertiesKeys) {
                    String value = mergedMap.get(key);

                    insertPropertiesStatement.setLong(1, eventId);
                    insertPropertiesStatement.setString(2, key);
                    insertPropertiesStatement.setString(3, value);

                    if (cnxSupportsBatchUpdates) {
                        insertPropertiesStatement.addBatch();
                    } else {
                        insertPropertiesStatement.execute();
                    }
                }

                if (cnxSupportsBatchUpdates) {
                    insertPropertiesStatement.executeBatch();
                }
            } finally {
                closeStatement(insertPropertiesStatement);
            }
        }
    }