package examples.dbkona; import weblogic.db.jdbc.*; import java.sql.*; import java.util.Properties; /** * This simple example shows how to connect and execute queries with * dbKona. You will need to have Oracle running the DEMO database, * with WebLogic jDriver for Oracle (or another Oracle JDBC driver). This example * is executed from the command line, and results are displayed to * standard out. For other examples of how to display query results, * check the server-side Java examples that use htmlKona and dbKona in * the examples/htmlkona folder. *
* If you don't have the Oracle DEMO database, you can * use the file examples/applets/emp.sql to set it up. *
*
* *
* *$ javac -d %CLIENT_CLASSES% query.java *
* *
* *$ java examples.dbkona.query * *
* A QueryDataSet simplifies the client-side management of * JDBC results by providing the infrastructure of a memory * cache. QueryDataSets also allow records to be fetched * incrementally. dbKona also provides methods associated * with DataSets to automatically generate SQL. * A QueryDataSet can be constructed with or without * a JDBC ResultSet, which is shown in this method. *
* After working with the DataSet, we close it, as well as * the JDBC ResultSet and the Statement used to execute the * query. */ public static void main(String argv[]) { Connection conn = null; Statement stmt = null; ResultSet rs = null; // A QueryDataSet simplifies the client-side management of result // sets by providing an the infrastructure of a memory cache. A // QueryDataSet can be constructed with or without a JDBC // ResultSet, as shown below. QueryDataSet qs = null; try { // For more information on making a connection, check the // dbKona Developers Guide. Class.forName("weblogic.jdbc.oci.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:weblogic:oracle:DEMO", "scott", "tiger"); // This a QueryDataSet can be constructed directly from a Connection qs = new QueryDataSet(conn, "select * from emp"); qs.fetchRecords(); System.out.println("Record count = " + qs.size()); // Close the DataSet. qs.close(); // QueryDataSets allow records to be fetched incrementally. qs = new QueryDataSet(conn, "select * from emp"); while (!qs.allRecordsRetrieved()) { // Fetch 2 records at a time . . . qs.fetchRecords(2); System.out.println("Record count = " + qs.size()); // Clear the records in the cache. qs.clearRecords(); } // Close the DataSet. qs.close(); stmt = conn.createStatement(); stmt.execute("select * from emp"); rs = stmt.getResultSet(); // This QueryDataSet is constructed with a JDBC ResultSet. qs = new QueryDataSet(rs); qs.fetchRecords(); System.out.println(qs.schema()); for (int i = 0; i < qs.size(); i++) { Record rec = qs.getRecord(i); System.out.println(rec.getValue("empno").asString() + " - " + rec.getValue("ename").asString() + " - " + rec.getValue("hiredate").asString()); } } catch(Exception e) { System.err.println("Trouble while executing example"); e.printStackTrace(); } finally { // You should always close DataSets, ResultSets, and Statements // when you have finished working with them. try { qs.close(); } catch (Exception e) {} try { rs.close(); } catch (Exception e) {} try { stmt.close(); } catch (Exception e) {} // Finally, close the connection. try { conn.close(); } catch(Exception e) {} } } }