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

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

Description

Returns a PreparedStatement of the given connection, set with the given SQL query and the given parameter values.

License

Apache License

Parameter

Parameter Description
connection The Connection to create the PreparedStatement from.
sql The SQL query to construct the PreparedStatement with.
values The parameter values to be set in the created PreparedStatement.

Exception

Parameter Description
SQLException If something fails during creating thePreparedStatement.

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {
    /**//from   w  w  w  .  ja v  a2 s  .c  o  m
     * Returns a PreparedStatement of the given connection, set with the given
     * SQL query and the given parameter values.
     *
     * @param connection The Connection to create the PreparedStatement from.
     * @param sql The SQL query to construct the PreparedStatement with.
     * @param values The parameter values to be set in the created
     * PreparedStatement.
     * @return
     * @throws SQLException If something fails during creating the
     * PreparedStatement.
     */
    public static PreparedStatement prepareStatement(Connection connection, String sql, Object... values)
            throws SQLException {
        PreparedStatement statement = connection.prepareStatement(sql);
        setValues(statement, values);
        return statement;
    }

    /**
     * Set the given parameter values in the given PreparedStatement.
     *
     * @param statement
     * @param values The parameter values to be set in the created
     * PreparedStatement.
     * @throws SQLException If something fails during setting the
     * PreparedStatement values.
     */
    public static void setValues(PreparedStatement statement, Object... values) throws SQLException {
        for (int i = 0; i < values.length; i++) {
            statement.setObject(i + 1, values[i]);
        }
    }
}

Related

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

  10. HOME | Copyright © www.java2s.com 2016