Java SQL Query executeForResult(Connection conn, String query, String... args)

Here you can find the source of executeForResult(Connection conn, String query, String... args)

Description

execute For Result

License

Open Source License

Declaration

public static ResultSet executeForResult(Connection conn, String query, String... args) throws SQLException 

Method Source Code


//package com.java2s;
/*/* w w  w . j a v a 2  s  .  c o m*/
 * Copyright (C) 2009 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

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

public class Main {
    public static ResultSet executeForResult(Connection conn, String query, String... args) throws SQLException {
        if (args == null || args.length == 0)
            return conn.createStatement().executeQuery(query);
        else {
            PreparedStatement stmt = createPreparedStatement(conn, query, args);
            return stmt.executeQuery();
        }
    }

    private static PreparedStatement createPreparedStatement(Connection conn, String sql, String[] args)
            throws SQLException {
        PreparedStatement stmt = conn.prepareStatement(sql);

        int n = args.length;
        for (int i = 0; i < n; i++)
            stmt.setString(i + 1, args[i]);

        return stmt;
    }
}

Related

  1. createInQueryStatement(String query, long[] ids, Connection conn)
  2. executeCsvQuery(Connection csv, String csvTableName)
  3. executeDDL(String query, Connection connection)
  4. ExecuteNoneQuerys(String cmdtext, Object[] parms)
  5. executeQuery(Connection c, String query)
  6. ExecuteQuery(Connection conn, PreparedStatement preparedStatement, PreparedStatement lockStatement, String tablename)
  7. executeQuery(Connection connection)