package examples.dbkona; import weblogic.db.jdbc.*; import java.sql.*; import java.util.Properties; /** * This example constructs a dbKona TableDataSet for inserting, updating, * and deleting records from an Oracle database. To run this example, * you must have the Oracle DEMO database running. The connection to * the database is made with WebLogic jDriver for Oracle. This example is run * 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 folders. *

* If you don't have the Oracle DEMO database, you can * use the file examples/applets/emp.sql to set it up. *

To set up this example:

*
    *
  1. Set up your development shell as described in *Setting up your environment. *

    *

  2. Change connection parameters to correspond to your Oracle configuration. * If you need more help, check the section on connecting *to a database in the Developers Guide, Using WebLogic jDriver for Oracle. *

    * *

  3. Compile this example by executing the following command in your development shell: * *

    * *$ javac -d %CLIENT_CLASSES% records.java *

    * *

  4. Run this example by executing the following command in your development shell: * *

    * *$ java examples.dbkona.records * *

* * @author Copyright (c) 1996-2000 by BEA Systems, Inc. All Rights Reserved. */ public class records { /** * First the JDBC connection is set up and created. Using * the JDBC connection object, a TableDataSet is constructed * without using SQL -- by supplying the Connection object * and the name of the Oracle table. In this example, there * is code for inserting, updating, and deleting records * with SQL that is automatically generated. *

* Creating a TableDataSet with a KeyDef is also illustrated * in this sample code, which is required for * updates and deletes to the DBMS data. You can save * at either the record level or the table level with * dbKona. */ public static void main(String argv[]) { Connection conn = null; TableDataSet ts1 = null; TableDataSet ts2 = 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"); // A TableDataSet simplifies the task of inserting, updating, and // deleting records by automatically generating the DML SQL. // Here we insert 100 records. ts1 = new TableDataSet(conn, "emp"); for (int i = 0; i < 100; i++) { Record rec = ts1.addRecord(); rec.setValue("empno", i) .setValue("ename", "Person " + i) .setValue("deptno", i); } // Save new records. dbKona does selective saves, that is, // it saves only those records in the TableDataSet. System.out.println("Inserting " + ts1.size() + " records."); ts1.save(); // Define a KeyDef for updates and deletes. KeyDef key = new KeyDef().addAttrib("empno"); // Update the 100 records that we originally added. ts2 = new TableDataSet(conn, "emp", key); ts2.where("empno < 100"); ts2.fetchRecords(); for (int i = 0; i < ts2.size(); i++) { Record rec = ts2.getRecord(i); rec.setValue("ename", "Worker " + i); } // Save the updated records. System.out.println("Update " + ts2.size() + " records."); ts2.save(); // Delete the same 100 records. ts2.reset(); ts2.fetchRecords(); for (int i = 0; i < ts2.size(); i++) { Record rec = ts2.getRecord(i); rec.markToBeDeleted(); } // Delete records from server. System.out.println("Delete " + ts2.size() + " records."); ts2.save(); } 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 { ts1.close(); } catch (Exception e) {} try { ts2.close(); } catch (Exception e) {} // Finally, close the connections. try { conn.close(); } catch(Exception e) {} } } }