Java JDBC Reds Connection query(String sql, Object... args)

Here you can find the source of query(String sql, Object... args)

Description

query, because need to manually close the resource, so not recommended for use it

License

Apache License

Parameter

Parameter Description
sql a parameter
args a parameter

Return

ResultSet

Declaration

@Deprecated
public static ResultSet query(String sql, Object... args) 

Method Source Code

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

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

import java.sql.SQLException;

public class Main {
    public static String URL;
    public static String USER_NAME;
    public static String PASSWORD;

    /**/* w w  w.j  av  a 2  s  . c o m*/
     * query, because need to manually close the resource, so not recommended
     * for use it
     *
     * @param sql
     * @param args
     * @return ResultSet
     */
    @Deprecated
    public static ResultSet query(String sql, Object... args) {
        ResultSet result = null;
        Connection con = getconnnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    ps.setObject((i + 1), args[i]);
                }
            }
            result = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Get connection
     *
     * @return
     */
    public static Connection getconnnection() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
}

Related

  1. checkConnection()
  2. createRedshiftTable(String redshiftURL, Properties loginProperties, String tableName, List fields)
  3. getRow(String username, String password, String url, String driver, String query, String[] params)
  4. getUserFace(String sUserName)
  5. getUserFromHost(String host)
  6. queryForList(String sql, Object... args)