Java SQL PreparedStatement prepareStatement(String parameterizedSQL, List values, Connection conn)

Here you can find the source of prepareStatement(String parameterizedSQL, List values, Connection conn)

Description

prepare Statement

License

Mozilla Public License

Parameter

Parameter Description
parameterizedSQL a parameter
values a parameter
conn a parameter

Exception

Parameter Description
SQLException an exception

Return

PreparedStatement

Declaration

public static PreparedStatement prepareStatement(String parameterizedSQL, List<?> values, Connection conn)
        throws SQLException 

Method Source Code


//package com.java2s;
/*/*from   w  w  w  .j  av a  2 s.c o m*/
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Content Registry 2.0.
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency.  Portions created by Tieto Eesti are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 * Jaanus Heinlaid, Tieto Eesti
 */

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;

public class Main {
    /**
     *
     * @param parameterizedSQL
     * @param values
     * @param conn
     * @return PreparedStatement
     * @throws SQLException
     */
    public static PreparedStatement prepareStatement(String parameterizedSQL, List<?> values, Connection conn)
            throws SQLException {

        PreparedStatement pstmt = conn.prepareStatement(parameterizedSQL);
        populateStatement(pstmt, values);

        return pstmt;
    }

    /**
     *
     * @param parameterizedSQL
     * @param values
     * @param conn
     * @param autoGeneratedKeys
     * @return PreparedStatement
     * @throws SQLException
     */
    public static PreparedStatement prepareStatement(String parameterizedSQL, List<?> values, Connection conn,
            boolean autoGeneratedKeys) throws SQLException {

        PreparedStatement pstmt = conn.prepareStatement(parameterizedSQL,
                autoGeneratedKeys ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS);
        populateStatement(pstmt, values);
        return pstmt;
    }

    /**
     *
     * @param pstmt
     * @param values
     * @throws SQLException
     */
    private static void populateStatement(PreparedStatement pstmt, List<?> values) throws SQLException {

        if (values == null || values.isEmpty()) {
            return;
        }

        int valueCount = values.size();
        for (int i = 0; i < valueCount; i++) {

            int paramIndex = i + 1;
            Object value = values.get(i);

            if (value == null) {
                pstmt.setNull(paramIndex, Types.NULL);
            } else if (value instanceof Boolean) {
                pstmt.setBoolean(paramIndex, ((Boolean) value).booleanValue());
            } else if (value instanceof Byte) {
                pstmt.setByte(paramIndex, ((Byte) value).byteValue());
            } else if (value instanceof Double) {
                pstmt.setDouble(paramIndex, ((Double) value).doubleValue());
            } else if (value instanceof Float) {
                pstmt.setFloat(paramIndex, ((Float) value).floatValue());
            } else if (value instanceof Integer) {
                pstmt.setInt(paramIndex, ((Integer) value).intValue());
            } else if (value instanceof Long) {
                pstmt.setLong(paramIndex, ((Long) value).longValue());
            } else if (value instanceof String) {
                pstmt.setString(paramIndex, (String) value);
            } else if (value instanceof BigDecimal) {
                pstmt.setBigDecimal(paramIndex, (BigDecimal) value);
            } else if (value instanceof java.sql.Date) {
                pstmt.setDate(paramIndex, (java.sql.Date) value);
            } else if (value instanceof java.sql.Timestamp) {
                pstmt.setTimestamp(paramIndex, (java.sql.Timestamp) value);
            } else if (value instanceof java.sql.Time) {
                pstmt.setTime(paramIndex, (java.sql.Time) value);
            } else if (value instanceof java.util.Date) {
                pstmt.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) value).getTime()));
            } else {
                pstmt.setObject(paramIndex, value);
            }
        }
    }
}

Related

  1. prepareStatement(Connection conn, String sql, boolean isCallable)
  2. prepareStatement(Connection conn, String sql, Object... values)
  3. prepareStatement(Connection connection, String line)
  4. prepareStatement(Connection connection, String sql, boolean returnKeys, Object... values)
  5. prepareStatement(Connection connection, String sql, Object... values)
  6. prepareStatement(String parameterizedSQL, List values, Connection conn)
  7. prepareStatement(String query)
  8. prepareStatementForwardReadOnly(Connection conn, String name, String sql)
  9. putArgsToStatement(PreparedStatement stmt, List args)

    HOME | Copyright © www.java2s.com 2016