Java SQL Query executeQuery2(java.sql.Connection con, String select, Object... pk)

Here you can find the source of executeQuery2(java.sql.Connection con, String select, Object... pk)

Description

execute Query

License

Open Source License

Declaration

public static String[] executeQuery2(java.sql.Connection con, String select, Object... pk) throws Exception 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.util.ArrayList;

public class Main {
    public static String[] executeQuery2(java.sql.Connection con, String select, Object... pk) throws Exception {
        con.setAutoCommit(true);/*from w w  w . ja v a2 s  .  co  m*/
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        try {
            ps = con.prepareStatement(select);
            if (pk != null) {
                for (int x = 0; x < pk.length; x++) {
                    ps.setObject(x + 1, pk[x]);
                }
            }
            rs = ps.executeQuery();
            Object obj;
            ArrayList<String> list = new ArrayList<String>();
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int numColumns = rsmd.getColumnCount();
            while (rs.next()) {
                StringBuffer str = new StringBuffer();
                for (int i = 2; i <= numColumns; i++) {
                    if (i > 2) {
                        str.trimToSize();
                        str.append(' ');
                    }
                    obj = rs.getObject(i);
                    if (obj != null) {
                        str.append(obj);
                    }
                }
                list.add(str.toString());
            }

            if (list.size() == 0) {
                return null;
            } else {
                return list.toArray(new String[list.size()]);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
    }

    /**
     * 
     * @param con
     * @param select
     * @param pk
     * @return
     * @throws Exception
     */
    public static String executeQuery(java.sql.Connection con, String select, Object pk) throws Exception {
        con.setAutoCommit(true);
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        try {
            ps = con.prepareStatement(select);
            ps.setObject(1, pk);
            rs = ps.executeQuery();
            Object obj;
            if (rs.next()) {
                StringBuffer str = new StringBuffer();
                java.sql.ResultSetMetaData rsmd = rs.getMetaData();
                int numColumns = rsmd.getColumnCount();
                for (int i = 2; i <= numColumns; i++) {
                    if (i > 1) {
                        str.trimToSize();
                        str.append(' ');
                    }
                    obj = rs.getObject(i);
                    if (obj != null) {
                        str.append(obj);
                    }
                }
                return str.toString();
            } else {
                return null;
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
    }
}

Related

  1. executeQuery(Statement state, String sql)
  2. ExecuteQuery(String command)
  3. executeQuery(String query)
  4. executeQuery(String sql, Connection conn, List param)
  5. executeQuery(String sql, String[] parameters)
  6. executeRetrievalByIDQuery(PreparedStatement theStatement, int theID)
  7. executeSafeQuery( Connection conn, String sql)
  8. executeStatement(Connection connection, String query)
  9. getSqlQuery(Statement statement, Object[] args)

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