Java SQL PreparedStatement writeMap(Connection connection, PreparedStatement preparedStatement, int keyColumn, int valueColumn, Class keyType, Class valueType, Map map)

Here you can find the source of writeMap(Connection connection, PreparedStatement preparedStatement, int keyColumn, int valueColumn, Class keyType, Class valueType, Map map)

Description

write Map

License

Open Source License

Declaration

public static <K, V> void writeMap(Connection connection, PreparedStatement preparedStatement, int keyColumn,
            int valueColumn, Class<K> keyType, Class<V> valueType, Map<K, V> map) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    public static <K, V> void writeMap(Connection connection, PreparedStatement preparedStatement, int keyColumn,
            int valueColumn, Class<K> keyType, Class<V> valueType, Map<K, V> map) throws SQLException {
        List<K> keys = new ArrayList<>(map.keySet());
        List<V> values = new ArrayList<>();
        for (K key : keys) {
            values.add(map.get(key));/*from w  w  w  .  ja  v a2 s . c  om*/
        }
        @SuppressWarnings("unchecked")
        K[] keyArray = (K[]) keys.toArray();
        @SuppressWarnings("unchecked")
        V[] valueArray = (V[]) values.toArray();
        writeArray(connection, preparedStatement, keyColumn, keyType, keyArray);
        writeArray(connection, preparedStatement, valueColumn, valueType, valueArray);
    }

    public static <T> void writeArray(Connection connection, PreparedStatement preparedStatement, int column,
            Class<T> type, T[] array) throws SQLException {
        if (type == String.class) {
            java.sql.Array sqlArray = connection.createArrayOf("VARCHAR", array);
            preparedStatement.setArray(column, sqlArray);
        } else {
            throw new IllegalArgumentException("Type '" + type.getName() + "' is not supported.");
        }
    }
}

Related

  1. setValues(PreparedStatement statement, Object... values)
  2. setValues(PreparedStatement statement, Object[] values)
  3. substitute(PreparedStatement stmt, Object[] params)
  4. writeArray(Connection connection, PreparedStatement preparedStatement, int column, Class type, T[] array)
  5. writeList(Connection connection, PreparedStatement preparedStatement, int column, Class type, List list)
  6. writeProperties(Connection connection, PreparedStatement preparedStatement, int keyColumn, int valueColumn, Properties properties)