/** * JDBC Simple Client * * This simple Java application loads a JDBC Driver, prompts the * user for connection information, creates the Driver URL, makes * the connection to the database, and sends user provided SQL * statements to the database. * * If the result of the statement is a result set, it is printed * out. * * This code and information is provided "as is" without warranty * of any kind, either expressed or implied. * */import java.io.*;import java.net.URL;import java.sql.*;class SimpleClient { private static Connection con = null; private static Statement stmt = null; /** * Command: java SimpleClient */ public static void main(String args[]) { /* User input fields */ String hostName = getString("Host Name: "); String userName = getString("User Name: "); String userPassword = getString("User Password: "); String dbName = getString("Database Name: "); /* Create the URL based on the user input */ String url = "jdbc:allbase://" + hostName + "/" + dbName + "?"; /* Try to make the connection to the database */ try { /* Load the jdbc driver */ Class.forName("com.hp.jdbc.allbase.JdbcDriver"); /* Attempt to get a connection */ con = DriverManager.getConnection(url, userName, userPassword); } catch (SQLException ex) { System.out.println("SQLException caught during connection:"); printException(ex); System.exit(0); } catch (Exception e) { System.out.println("Exception caught during connection:"); e.printStackTrace(); System.exit(0); } /* Main loop */ String sql = null; ResultSet rs = null; while (true) { /* Ask user for an sql statement */ System.out.println(""); sql = getString("SQL -> "); System.out.println(""); /* If empty string, then exit */ if (sql == null || sql.length() == 0) break; /* Submit the sql statement */ try { stmt = con.createStatement(); /* If execute returns true, there is a result set */ if (stmt.execute(sql)) { rs = stmt.getResultSet(); displayResultSet(rs); } /* Otherwise, there is an update count */ else System.out.println(stmt.getUpdateCount() + " rows changed."); } catch (SQLException ex) { printException(ex); } catch (java.lang.Exception ex) { ex.printStackTrace(); } try { rs.close(); stmt.close(); rs = null; stmt = null; } catch (Exception e) {} } /* End of main loop */ try { con.close(); } catch (Exception e) {} System.exit(0); } /** * Print out SQLException information. */ private static void printException(SQLException e) { SQLException ex = e; System.out.println("SQLException:"); while (ex != null) { System.out.println(" SQLState : " + ex.getSQLState()); System.out.println(" Vendor code: " + ex.getErrorCode()); System.out.println(" Message : " + ex.getMessage()); ex = ex.getNextException(); System.out.println(); } return; } /** * Displays all columns and rows in the given result set */ private static void displayResultSet (ResultSet rs) throws SQLException { int i; /* Get the ResultSetMetaData. This will be used for ** the column headings. */ ResultSetMetaData rsmd = rs.getMetaData(); /* Get the number of columns in the result set */ int numCols = rsmd.getColumnCount(); /* For each column, print out the column names, separated by ** commas */ for (i=1; i <= numCols; i++) { System.out.print(rsmd.getColumnName(i)); /* Print separator */ if (i < numCols) System.out.print(", "); } System.out.println("\n"); /* Print out the data, fetching until end of the result set */ String colValue; while (rs.next()) { /* For each column, print out the data as String */ for (i=1; i<=numCols; i++) { colValue = rs.getString(i); /* If data was null, use the null string instead */ if (rs.wasNull()) colValue = "<NULL>"; System.out.print(colValue); /* Print separator */ if (i < numCols) System.out.print(", "); } System.out.println(); /* Fetch the next result set row */ } System.out.println(); } /** * This captures console input until a CR is hit and * then returns a Java String with the input. */ private static String getString(String prompt) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); if (prompt != null) System.out.print(prompt); String rString = ""; try { rString = in.readLine(); } catch (Exception e) { rString = ""; } return rString; } } |