Java SQL PreparedStatement prepareStatement(Connection conn, String sql, Object... values)

Here you can find the source of prepareStatement(Connection conn, String sql, Object... values)

Description

prepare Statement

License

Open Source License

Declaration

public static PreparedStatement prepareStatement(Connection conn, String sql, Object... values)
            throws SQLException 

Method Source Code

//package com.java2s;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Timestamp;

public class Main {
    public static PreparedStatement prepareStatement(Connection conn, String sql, Object... values)
            throws SQLException {
        PreparedStatement stmt = conn.prepareStatement(sql);
        setPreparedStatementValues(stmt, values);
        return stmt;
    }/*from www .j a  v a2 s .  c o  m*/

    /**
     * Sets the given <code>values</code> on the prepared statement <code>st</code>. The values
     * are set on the statement starting at index 1 (the first index).
     *
     * @param values The values to set on the statement. May be <code>null</code> or empty.
     * @throws SQLException ff there is an error setting any prepared statement value.
     * @throws IllegalArgumentException if one of the objects in <code>values</code> is not
     * a recognized value type. The recognized types are instances of {@link String},
     * {@link Boolean}, {@link Byte}, {@link Short}, {@link Integer}, {@link Long},
     * {@link Float}, {@link Double}, {@link java.sql.Date}, {@link java.util.Date},
     * {@link java.sql.Timestamp}, and {@link Enum}.
     */
    @SuppressWarnings("unchecked")
    public static void setPreparedStatementValues(PreparedStatement st, Object... values) throws SQLException {
        if (values != null) {
            int index = 1;
            for (Object value : values) {
                if (value instanceof String) {
                    st.setString(index++, (String) value);
                } else if (value instanceof Boolean) {
                    st.setBoolean(index++, (Boolean) value);
                } else if (value instanceof Byte) {
                    st.setByte(index++, (Byte) value);
                } else if (value instanceof Short) {
                    st.setShort(index++, (Short) value);
                } else if (value instanceof Integer) {
                    st.setInt(index++, (Integer) value);
                } else if (value instanceof Long) {
                    st.setLong(index++, (Long) value);
                } else if (value instanceof Float) {
                    st.setFloat(index++, (Float) value);
                } else if (value instanceof Double) {
                    st.setDouble(index++, (Double) value);
                } else if (value instanceof Date) {
                    st.setDate(index++, (Date) value);
                } else if (value instanceof Timestamp) {
                    st.setTimestamp(index++, (Timestamp) value);
                } else if (value instanceof java.util.Date) {
                    st.setTimestamp(index++, new Timestamp(((java.util.Date) value).getTime()));
                } else if (value instanceof Enum) {
                    st.setString(index++, value.toString());
                } else {
                    throw new IllegalArgumentException(
                            "cannot handle object of type " + value.getClass().getName());
                }
            }
        }
    }
}

Related

  1. prepareStatement(Connection con,String sql,Object...params)
  2. prepareStatement(Connection conn, Map psMap, String key, String sql)
  3. prepareStatement(Connection conn, String name, String sql)
  4. prepareStatement(Connection conn, String sql)
  5. prepareStatement(Connection conn, String sql, boolean isCallable)
  6. prepareStatement(Connection connection, String line)
  7. prepareStatement(Connection connection, String sql, boolean returnKeys, Object... values)
  8. prepareStatement(Connection connection, String sql, Object... values)
  9. prepareStatement(String parameterizedSQL, List values, Connection conn)