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

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

Description

execute Query

License

Open Source License

Parameter

Parameter Description
con a parameter
select a parameter
pk a parameter

Exception

Parameter Description
Exception an exception

Declaration

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

Method Source Code

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

public class Main {
    /**/*from w ww .j  a  v  a2s .  c om*/
     * 
     * @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(Connection c, String query)
  2. ExecuteQuery(Connection conn, PreparedStatement preparedStatement, PreparedStatement lockStatement, String tablename)
  3. executeQuery(Connection connection)
  4. executeQuery(Connection connection, String query)
  5. executeQuery(Connection dbConnection, String selectString)
  6. executeQuery(Statement state, String sql)
  7. ExecuteQuery(String command)
  8. executeQuery(String query)
  9. executeQuery(String sql, Connection conn, List param)

    HOME | Copyright © www.java2s.com 2016