Java SQL Execute execute(Connection conn, String sql, Object[] params)

Here you can find the source of execute(Connection conn, String sql, Object[] params)

Description

execute

License

Open Source License

Declaration

public static List<Object[]> execute(Connection conn, String sql, Object[] params) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Object[]> execute(Connection conn, String sql, Object[] params) {
        List<Object[]> result = new ArrayList<Object[]>();
        PreparedStatement ps = null;
        ResultSet rs = null;//from  ww  w . ja  v a  2  s  .  co  m
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                Object obj = params[i];
                ps.setObject(i + 1, obj);
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            while (rs.next()) {
                int columnNum = metaData.getColumnCount();
                Object[] values = new Object[columnNum];
                for (int i = 0; i < values.length; i++) {
                    values[i] = rs.getObject(i + 1);
                }
                result.add(values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. execSql(String sql, Connection conn)
  2. execStatement(Connection con, String strStatement)
  3. execte(Connection conn, String sql)
  4. execute(Connection conn, String SQL)
  5. execute(Connection conn, String sql, Object[] args)
  6. execute(Connection conn, String string)
  7. execute(Connection connection, String sql)
  8. execute(Connection connection, String sql, boolean closeConn)
  9. execute(final String url, final String username, final String password, final String sql, final Consumer consumer)