/* * execQuery.java * * Copyright ©2008 by StarQuest Ventures, Inc., * PO Box 146, Inverness, Ca. 94937, U.S.A. * All rights reserved. * */ import java.util.*; import java.sql.*; import java.lang.reflect.*; import java.io.*; public class execQuery { public static void doQuery(String sqlString, String db){ // REGISTER STARSQL FOR JAVA DRIVER try { Driver d = (Driver)Class.forName("com.starsql.jdbc.SQDriver").newInstance(); } catch (Exception e) { System.out.println(e); } // GET CONNECTION Connection con = null; String userName = "DB2USER"; String password = "PASSWORD"; try{ if ( db.equals("Production") ) con = DriverManager.getConnection("jdbc:StarSQL_JDBC://host:2846/server",userName,password); else con = DriverManager.getConnection("jdbc:StarSQL_JDBC://host:2846/server",userName,password); }catch(Exception e){ System.out.println(e); } // CREATE STATEMENT Statement stmt = null; try { stmt = con.createStatement(); } catch (Exception e){ System.out.println(e); } // EXECUTE QUERY ResultSet results = null; try { results = stmt.executeQuery(sqlString); } catch (Exception e){ System.out.println(e); } // GET ALL RESULTS StringBuffer buf = new StringBuffer(); try { ResultSetMetaData rsmd = results.getMetaData(); int numCols = rsmd.getColumnCount(); int i, rowcount = 0; while (results.next()){ for (i=1; i <= numCols; i++) { if (i > 1) buf.append(","); buf.append(results.getString(i)); } buf.append("\n"); rowcount++; } System.out.println(buf); System.out.flush(); results.close(); } catch (Exception e) { System.out.println(e); } // CLOSE RESULT SET try { results.close(); } catch (Exception e){ System.out.println(e); } // CLOSE STATEMENT try { stmt.close(); } catch (Exception e) { System.out.println(e); } // CLOSE CONNECTION try { con.close(); } catch (Exception e) { System.out.println(e); } } }