Java SQL PreparedStatement writeProperties(Connection connection, PreparedStatement preparedStatement, int keyColumn, int valueColumn, Properties properties)

Here you can find the source of writeProperties(Connection connection, PreparedStatement preparedStatement, int keyColumn, int valueColumn, Properties properties)

Description

write Properties

License

Open Source License

Declaration

public static void writeProperties(Connection connection, PreparedStatement preparedStatement, int keyColumn,
            int valueColumn, Properties properties) 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.Properties;

public class Main {
    public static void writeProperties(Connection connection, PreparedStatement preparedStatement, int keyColumn,
            int valueColumn, Properties properties) throws SQLException {
        List<Object> keys = new ArrayList<>(properties.keySet());
        List<Object> values = new ArrayList<>();
        for (Object key : keys) {
            values.add(properties.get(key));
        }/*from  w  w w . j  ava  2  s  .com*/
        writeArray(connection, preparedStatement, keyColumn, Object.class, keys.toArray());
        writeArray(connection, preparedStatement, valueColumn, Object.class, values.toArray());
    }

    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. substitute(PreparedStatement stmt, Object[] params)
  3. writeArray(Connection connection, PreparedStatement preparedStatement, int column, Class type, T[] array)
  4. writeList(Connection connection, PreparedStatement preparedStatement, int column, Class type, List list)
  5. writeMap(Connection connection, PreparedStatement preparedStatement, int keyColumn, int valueColumn, Class keyType, Class valueType, Map map)