Java SQL Query createInQueryStatement(String query, long[] ids, Connection conn)

Here you can find the source of createInQueryStatement(String query, long[] ids, Connection conn)

Description

Creates new PreparedStatement and fill it up with the ids.

License

Open Source License

Parameter

Parameter Description
query the query to be use.
ids the submission id.
conn the connection to be used.

Exception

Parameter Description
SQLException if error occurs during operation.

Return

the prepared statement filled with id values.

Declaration

static PreparedStatement createInQueryStatement(String query, long[] ids, Connection conn) throws SQLException 

Method Source Code


//package com.java2s;
import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.SQLException;

public class Main {
    /**// ww  w . j  a v  a  2s.com
     * Creates new PreparedStatement and fill it up with the ids.
     *
     * @param query the query to be use.
     * @param ids the submission id.
     * @param conn the connection to be used.
     * @return the prepared statement filled with id values.
     * @throws SQLException if error occurs during operation.
     */
    static PreparedStatement createInQueryStatement(String query, long[] ids, Connection conn) throws SQLException {
        String actualQuery = query + createQuestionMarks(ids.length);
        PreparedStatement pstmt = conn.prepareStatement(actualQuery);

        for (int i = 0; i < ids.length; i++) {
            pstmt.setLong(i + 1, ids[i]);
        }

        return pstmt;
    }

    /**
     * Creates the string in the pattern (?,+) where count is the number of question marks. It is used th build
     * prepared statements with IN condition.
     *
     * @param count number of question marks.
     * @return the string of question marks.
     */
    static String createQuestionMarks(int count) {
        StringBuffer buff = new StringBuffer();

        buff.append("(?");

        for (int i = 1; i < count; i++) {
            buff.append(", ?");
        }

        buff.append(")");

        return buff.toString();
    }
}

Related

  1. executeCsvQuery(Connection csv, String csvTableName)
  2. executeDDL(String query, Connection connection)
  3. executeForResult(Connection conn, String query, String... args)
  4. ExecuteNoneQuerys(String cmdtext, Object[] parms)