![]() |
![]() |
Using dbKona
I. IntroductionOverview of dbKonaThe dbKona classes provide a set of high-level database connectivity objects that give Java applications and applets![]() ![]() ![]()
dbKona provides a higher level of abstraction than JDBC, which deals
with low-level details of managing data. dbKona offers objects that
allow the programmer to view and modify database data in a high-level,
vendor-independent way. A Java application that uses dbKona objects
does not need vendor-specific knowledge about DBMS dbKona in a multitier configuration
dbKona may also be used within WebLogic. In that case, you will also be
working with WebLogic JDBC, a multitier dbKona is a natural choice for writing database access programs in a multitier environment, since with its objects you may write database applications that are completely vendor independent. dbKona and WebLogic's multitier framework is particularly suited for applications that want to retrieve data from several heterogeneous databases for transparent presentation to the user.
![]() For more information on WebLogic and the WebLogic JDBC Server, check the Using WebLogic JDBC Developers Guide. How dbKona and a JDBC driver interactdbKona depends upon a JDBC driver to provide and maintain a connection to a DBMS. In order to use dbKona, you must have installed a JDBC driver.
JavaSoft's JDBC is a set of interfaces that WebLogic has implemented to create its jdbcKona drivers. WebLogic's non-ODBC, native JDBC drivers are JDBC implementations of database-specific drivers for Oracle, Sybase, and SQLServer. Using database-specific drivers with dbKona offers the programmer access to all of the functionality of each specific database, as well as improved performance. Although the underlying foundation of dbKona uses JDBC for database transactions, dbKona provides the programmer with higher-level, more convenient access to the database. How dbKona and WebLogic Events can interactThe dbKona package contains some "eventful" classes that send and receive events (within WebLogic, using WebLogic events![]() dbKona architecturedbKona uses a high level of abstraction to describe and manipulate data that resides in a database. Classes in dbKona create and manage objects that retrieve and modify data. An application can use dbKona objects in a consistent way without any knowledge of how a particular vendor stores or processes data.At the core of dbKona's architecture is the concept of a DataSet. A DataSet contains the results of a query. DataSets allow client-side management of query results. The programmer can control the entire query result rather than dealing with a single record at a time. A DataSet contains Records, and each Record contains one or more Value objects. A Record is comparable to a database row, and a Value can be compared to a database cell. Value objects "know" their internal data type as stored in the DBMS, but the programmer can treat Value objects in a consistent way without having to worry about vendor-specific internal data types. Methods from the DataSet class (and its subclasses TableDataSet and QueryDataSet) provide a high-level, flexible way to navigate through and manipulate the results of a query. Changes made to a TableDataSet can be saved to the DBMS; dbKona maintains knowledge of which records have changed and makes a selective save, which reduces network traffic and DBMS overhead. dbKona also uses other objects, like SelectStmt and KeyDef to shield the programmer from vendor-specific SQL. By using methods in these class, the programmer can have dbKona construct the appropriate SQL, which reduces syntax errors and does not require a knowledge of vendor-specific SQL. On the other hand, dbKona also allows the programmer to pass SQL to the DBMS if desired.
II. The dbKona APIdbKona API reference
Package weblogic.db.jdbc
Class java.lang.Object Class weblogic.db.jdbc.Column (implements weblogic.common.internal.Serializable) Class weblogic.db.jdbc.DataSet (implements weblogic.common.internal.Serializable) Class weblogic.db.jdbc.QueryDataSet Class weblogic.db.jdbc.TableDataSet Class weblogic.db.jdbc.EventfulTableDataSet (implements weblogic.event.actions.ActionDef) Class weblogic.db.jdbc.Enums Class weblogic.db.jdbc.KeyDef Class weblogic.db.jdbc.Record Class weblogic.db.jdbc.EventfulRecord (implements weblogic.common.internal.Serializable) Class weblogic.db.jdbc.Schema (implements weblogic.common.internal.Serializable) Class weblogic.db.jdbc.SelectStmt Class weblogic.db.jdbc.oracle.Sequence Class java.lang.Throwable Class java.lang.Exception Class weblogic.db.jdbc.DataSetException Class weblogic.db.jdbc.Value dbKona objects and their classesObjects in dbKona fall into three categories:
These broad categories of objects depend upon each other in application building. In a general way, every data object has a set of descriptive objects associated with it.
DataSetThe general container for data objects in dbKona is the DataSet. dbKona uses the concept of a DataSet to cache records retrieved from a DBMS server. It is roughly equivalent to a table in SQL. The DataSet class has two subclasses, QueryDataSet and TableDataSet.In the multitier model using the WebLogic JDBC Server, DataSets can be saved (cached) on the WebLogic Server. A T3Client can connect to a WebLogic JDBC Server, create and cache a DataSet, disconnect, and connect again and use the same DataSet.
A QueryDataSet makes the results of an SQL query available as a collection of Records that are accessible by index position (0-origined). Unlike the case with a TableDataSet, changes and additions to a QueryDataSet cannot be saved into the database. There are two functional differences between a QueryDataSet and a TableDataSet: First, changes made to a TableDataSet can be saved to a database; you can make changes to Records in a QueryDataSet, but those changes cannot be saved. Second, you can retrieve data into a QueryDataSet from more than one table.
The functional difference between a TableDataSet and a QueryDataSet is that changes made to a TableDataSet can be saved to a database. With a TableDataSet, you can update values in Records, add new Records, and mark Records for deletion; finally, you can save changes to a database, using the save() methods in either the TableDataSet class to save an entire TableDataSet, or in the Record class to save a single record. Additionally, the data retrieved into a TableDataSet is, by definition, from a single database table; you cannot perform joins on database tables to retrieve data for a TableDataSet. If you intend to save updates or deletes to a database, you must construct the TableDataSet with a KeyDef object that specifies a unique key for forming the WHERE clauses in an UPDATE or DELETE statement. A KeyDef is not necessary if only inserts take place, since an insert operation does not require a WHERE clause. The KeyDef key must not contain columns that are filled or altered by the DBMS, since dbKona must have a known value for the key column to construct a correct WHERE clause. You can also qualify a TableDataSet with an arbitrary string that is used to construct the tail of the SQL statement. When you are using dbKona with an Oracle database, for example, you can qualify the TableDataSet with the string "for UPDATE" to place a lock on the records that are retrieved by the query. A TableDataSet can be constructed with a KeyDef, a dbKona object used for setting a unique key for saving updates and deletes to the DBMS. If you are working with an Oracle database, you can set the TableDataSet's KeyDef to "ROWID," which is a unique key inherent in each table. Then construct the TableDataSet with a set of attributes that includes "ROWID."
When an EventfulRecord in an EventfulTableDataSet changes, it sends an EventMessage to the WebLogic Server with a ParamSet that contains the row that changed as well as the changed data, for the topic WEBLOGIC.[tablename], where the tablename is the name of the table associated with an EventfulTableDataSet. EventfulTableDataSet takes action on the received, evaluated event to update its own copy of the record that changed.
For example, an EventfulTableDataSet might be used by a warehouse inventory system to automagically update many views of a table. Here is how it works. Each warehouse employee's client app creates an EventfulTableDataSet from the "stock" table and displays those records in a Java application. Employees doing different jobs might have different displays, but all of the client applications are using an EventfulTableDataSet of the "stock" table. Because a TableDataSet is "eventful," each record in the data set has registered an interest in itself automatically. The WebLogic Topic Tree has a registration of interest for all the records; for each client, there is a registration of interest in each record in the TableDataSet. When a user changes a record, the DBMS is updated with the new record. At the same time, an EventMessage (embedded with the changed Record itself) is automatically sent to the WebLogic Server. Each client using an EventfulTableDataSet of the "stock" table receives an event notification that has embedded in it the changed Record. The EventfulTableDataSet for each client accepts the changed Record and updates the GUI.
Records are created as part of a DataSet. You can also construct Records manually in the context of a DataSet and its Schema, or the Schema of an SQL table known to an active Database session. Records in a TableDataSet may be saved to the database individually with the save() method in the Record class, or corporately with the save() method in the TableDataSet class.
The Record class (see weblogic.db.jdbc.Record) has methods for:
![]()
A Value object has an internal type, which is defined by the Schema of its parent DataSet. A Value object can be assigned a value with a data type other than its internal type, if the assignment is legal. A Value object can also return the value of a data type other than its internal data type, if the request is legal. The Value object acts to shield the application from the details of manipulating vendor-specific data types. The Value object "knows" its data type, but all Value objects can be manipulated within a Java application with the same methods, no matter the internal data type.
Data description objects in dbKonaData description objects contain metadata; that is, information about data structure, how data are stored on and retrieved from the DBMS, whether and how data can be updated. Some of the data description objects that dbKona uses are implementations of the JDBC interface; a brief description and how to use these is provided here.SchemaA Schema object holds metadata, such as the name, data type, size, and order of each field, for each of the attributes associated with a DataSet. A Schema object is a collection of Column objects; each Column holds the metadata for a table attribute.When you instantiate a DataSet, you implicitly create the Schema that describes it, and when you fetch its Records, its Schema is updated.
The Column class (see weblogic.db.jdbc.Column) has methods for:
The KeyDef object with no attributes is constructed in the KeyDef class. Use the addAttrib() method to build the attributes of the KeyDef, and then use the KeyDef as an argument in the constructor for a TableDataSet. Once the KeyDef is associated with a DataSet, you can't add any more attributes to it. When you are working with an Oracle database, you can add the attribute "ROWID," which is an inherently unique key associated with each table, to be used for inserts and deletes with a TableDataSet. The KeyDef class (see weblogic.db.jdbc.KeyDef) has methods for:
A SelectStmt object is constructed in the SelectStmt class. Then add clauses to the SelectStmt with methods in the SelectStmt class, and use the resulting SelectStmt object as an argument when you create a QueryDataSet. A TableDataSet also has a default SelectStmt associated with it that can be used to further refine data retrieval after the TableDataSet has been created. Methods in the SelectStmt class (see weblogic.db.jdbc.SelectStmt) correspond to the clauses in a SQL statement, which include:
Miscellaneous objects in dbKonaOther miscellaneous objects in dbKona include Exceptions and Constants.ExceptionsdbKona methods can raise the following Exceptions:
java.sql.SqlExceptions are thrown when there is a problem building an SQL statement or executing it on the DBMS server. A LicenseException is thrown when license information does not match the information encoded in your weblogic.properties file.
The Enums class contains constants for the following:
Entity relationshipsInheritance relationshipsThe following illustrations show important descendancy relationships between dbKona classes. One class is subclassed:
Other dbKona objects descend from DbObject. Most dbKona Exceptions, including DataSetException and LicenseException, are subclassed from java.lang.Exception and weblogic.db.jdbc.DataSetException. LicenseException is subclassed from RuntimeException. Possession relationshipsEach dbKona object may have other objects associated with it that further define its structure. The following illustrations show these relationships.
You will need to import any other Java classes that you will use; in
this case, we also import the Properties class from java.util, which
we'll use during the login process, and the weblogic.html
package. We'll be using htmlKona import java.sql.*; import weblogic.db.jdbc.*; import weblogic.html.*; import java.util.Properties;Note that you do not import the package for your JDBC driver. The JDBC driver is established during the connection phase. For version 2.0 and later, you do not import weblogic.db.common, weblogic.db.server, or weblogic.db.t3client.
The following code example is a method for creating the Properties object that will be used later in this tutorial to make a connection to an Oracle DBMS. Each property is set with a double-quote-enclosed string. public class tutor { public static void main(String argv[]) throws DataSetException, java.sql.SQLException, java.io.IOException, ClassNotFoundException { Properties props = new java.util.Properties(); props.put("user", "scott"); props.put("password", "tiger"); props.put("server", "DEMO"); (continued below)The Properties object will be used as an argument to create a Connection. The JDBC Connection object will become an important context for other database operations.
You can see how to create the Properties object, props, in Step 2. Class.forName("weblogic.jdbc.oci.Driver"); Connection conn = DriverManager.getConnection("jdbc:weblogic:oracle", props); conn.setAutoCommit(false);The Connection conn becomes an argument for other actions that involve the DBMS, for instance creating DataSets to hold query results. For details about connecting to a DBMS with the WebLogic two-tier JDBC drivers (jdbcKona) or WebLogic's Java-only JDBC implementation (WebLogic JDBC), see the Developers Guides (jdbcKona and WebLogic JDBC). Connections, DataSets (and, if you use them, JDBC ResultSets), and Statements should be closed with the close() method when you have finished working with them. Note in the code examples that follow that each of these is explicitly closed. Note on autoCommit: The default mode of java.sql.Connection sets autocommit to true. Oracle will perform much faster if you set autocommit to false, as shown above. If you are using jdbcKona/Sybase or jdbcKona/MSSQLServer, you should read the developers notes on Autocommit before you make any changes to this setting.
Step 1. Setting parameters for data retrievalOnce you have a connection, you are ready to work with data. There are two parts to retrieving data: setting parameters for what data should be retrieved, and then instantiating a DataSet (either a TableDataSet or a QueryDataSet) to hold the results of the query. Once you have created the DataSet, you can continue to refine your data parameters.In dbKona, there are several ways to set parameters -- to compose the SQL statement and set its scope -- for retrieving data. Here we show how dbKona can interact at a very basic level with any JDBC driver, by taking the results of a JDBC ResultSet and creating a DataSet. In this example, we use a Statement object to execute a SQL statement. A Statement object is created with a method from the JDBC Connection class, and then the ResultSet is created by executing the Statement. Statement stmt = conn.createStatement(); stmt.execute("SELECT * from empdemo"); ResultSet rs = stmt.getResultSet();You can use the results of a query executed with a Statement object to instantiate a QueryDataSet. This QueryDataSet is constructed with a JDBC ResultSet: Statement stmt = conn.createStatement(); stmt.execute("SELECT * from empdemo"); ResultSet rs = stmt.getResultSet(); QueryDataset ds = new QueryDataSet(rs);Using the results from the execution of a JDBC Statement is only one way to create a DataSet. It requires knowledge of SQL, and it doesn't give you much control over the results of your query: basically, you can iterate through the records with the JDBC next() method. With dbKona, you do not have to know much about SQL to retrieve records; you can use methods in dbKona to set up your query, and once you have created a DataSet with your records, you have a much finer control over manipulating the records.
Instead of requiring you to compose an SQL statement, dbKona lets you use methods to set certain parts of the statement. You create a DataSet (either a TableDataSet or a QueryDataSet) for the results of the query. For example, the simplest data retrieval in dbKona is into a TableDataSet. Creating a TableDataSet requires just a Connection object and the name of the DBMS table that you want to retrieve, as in this example that retrieves the Employee table (alias "empdemo"): TableDataSet tds = new TableDataSet(conn, "empdemo");A TableDataSet can be constructed with a subset of the attributes (columns) in a DBMS table. If you want to retrieve just a few columns from a very large table, specifying those columns is more efficient than retrieving the entire table. To do this, pass a list of table attributes as a string in the constructor. For example: TableDataSet tds = new TableDataSet(conn, "empdemo", "empno, dept"); Use a TableDataSet if you want to be able to save changes to the DBMS, or if you do not plan to do a join of one or more tables to retrieve data; otherwise, use a QueryDataSet. In this example, we use the QueryDataSet constructor that takes two arguments: a Connection object and a string that is the SQL: QueryDataSet qds = new QueryDataSet(conn, "select * from empdemo"); You do not actually begin receiving data until you call the fetchRecords() method in the DataSet class. After you create a DataSet, you can continue to refine its data parameters. For instance, we could refine the selection of records to be retrieved in the TableDataSet with the where() method, which adds a WHERE clause to the SQL that dbKona composes. The following retrieves just one record from the Employee table by using the where() method to create a WHERE clause. TableDataSet tds = new TableDataSet(conn, "empdemo"); tds.where("empno = 8000"); ![]() Step 3. Fetching the resultsWhen you are happy with the data parameters, call the fetchRecords() method from the DataSet class, as in this example:TableDataset tds = new TableDataSet(conn, "empdemo", "empno, dept"); tds.where("empno = 8000"); tds.fetchRecords();The fetchRecords() method can take arguments to fetch a certain number of records, or to fetch records starting with a particular record. In the following example, we fetch no more than the first 20 records and discard the rest with the clearRecords() method. TableDataSet tds = new TableDataSet(conn, "empdemo", "empno, dept"); tds.where("empno > 8000"); tds.fetchRecords(20) .clearRecords();When dealing with very large query results, you may prefer to fetch a few records at a time, process them, and then clear the DataSet before the next fetch. Use the clearRecords() method from the DataSet class to clear the TableDataSet between fetches, as illustrated here. TableDataSet tds = new TableDataSet(conn, "empdemo", "empno, dept"); tds.where("empno > 2000"); while (!tds.allRecordsRetrieved()) { tds.fetchRecords(100); // Process the hundred records . . . tds.clearRecords(); } You can also reuse a DataSet with a method that was added in release 2.5.3. This method, DataSet.releaseRecords(), closes the DataSet and releases all the Records but does not nullify them. You can reuse the DataSet to generate new records, yet any records from the first use still held by the application remain readable.
Schema sch = tds.schema(); System.out.println(sch.toString());If you use a Statement object to create a query, you should close the Statement after you have completed the query and fetched its results. stmt.close(); ![]() Step 5. Examining the data with htmlKonaAfter the DataSet has been created and populated, you can examine each record by looking at its Values. A Value corresponds to the cells in one row of a DBMS.The following example shows how you might use a htmlKona UnorderedList to examine the data. This example uses DataSet.getRecord() and Record.getValue() to examine each record in a for loop. This finds the name, ID, and salary of the employee making the most money from the records retrieved in the QueryDataSet we created in Step 2. // (Creation of Database session object and QueryDataSet qds) UnorderedList ul = new UnorderedList(); String name = ""; String id = ""; String salstr = ""; int sal = 0; for (int i = 0; i < qds.size(); i++) { // Get a record Record rec = qds.getRecord(i); int tmp = rec.getValue("Emp Salary").asInt(); // Add the salary amount to the htmlKona ListElement ul.addElement(new ListItem("$" + tmp)); // Compare this salary to the maximum salary we have found so far if (tmp > sal) { // If this salary is a new max, save away the employee's info sal = tmp; name = rec.getValue("Emp Name").asString(); id = rec.getValue("Emp ID").asString(); salstr = rec.getValue("Emp Salary").asString(); } ![]() Step 6. Displaying the results with htmlKonaYou can display results in many ways. htmlKona provides a convenient way to display dynamic data like that produced by the above example. The following example shows how you might construct a page on the fly for displaying the results of your query.HtmlPage hp = new HtmlPage(); hp.getHead() .addElement(new TitleElement("Highest Paid Employee")); hp.getBodyElement() .setAttribute(BodyElement.bgColor, HtmlColor.white); hp.getBody() .addElement(MarkupElement.HorizontalLine) .addElement(new HeadingElement("Query String: ", +2)) .addElement(stmt.toString()) .addElement(MarkupElement.HorizontalLine) .addElement("I examined the values: ") .addElement(ul) .addElement(MarkupElement.HorizontalLine) .addElement("Max salary of those employees examined is: ") .addElement(MarkupElement.Break) .addElement("Name: ") .addElement(new BoldElement(name)) .addElement(MarkupElement.Break) .addElement("ID: ") .addElement(new BoldElement(id)) .addElement(MarkupElement.Break) .addElement("Salary: ") .addElement(new BoldElement(salstr)) .addElement(MarkupElement.HorizontalLine); hp.output(); ![]() Step 7. Closing the DataSet and the ConnectionWhen you are finished working with the query results, close the DataSet with the close() method. You should always close DataSets.qds.close(); tds.close();It is also important to close the Connection to the DBMS. It's one line of code that should appear at the end of all of your database operations in a finally block, as in this example: try { // Do your work } catch (Exception mye) { // Catch and handle exceptions } finally { try {conn.close();} catch (Exception e) { // Deal with any exceptions } } ![]() Code summaryHere is a summary of code similar to that used in the previous steps. In this example, we add 10 records to the Employee table with a TableDataSet, and then use a QueryDataSet to retrieve the records in the table. We use htmlKona to view the filtered results.import java.sql.*; import weblogic.db.jdbc.*; import weblogic.html.*; import java.util.Properties; public class tutor { public static void main(String[] argv) throws java.io.IOException, DataSetException, java.sql.SQLException, HtmlException, ClassNotFoundException { try { Properties props = new java.util.Properties(); props.put("user", "scott"); props.put("password", "tiger"); props.put("server", "DEMO"); Class.forName("weblogic.jdbc.oci.Driver"); Connection conn = DriverManager.getConnection("jdbc:weblogic:oracle", props); conn.setAutoCommit(false); // Create a TableDataSet to add 10 records TableDataSet tds = new TableDataSet(conn, "empdemo"); for (int i = 0; i < 10; i++) { Record rec = tds.addRecord(); rec.setValue("empno", i) .setValue("ename", "person " + i) .setValue("esalary", 2000 + (i * 10)); } // Save the data and close the TableDataSet tds.save(); tds.close(); // Create a QueryDataSet to retrieve the additions to the table Statement stmt = conn.createStatement(); stmt.execute("SELECT * from empdemo"); QueryDataSet qds = new QueryDataSet(stmt.getResultSet()); qds.fetchRecords(); // Use the data from the QueryDataSet UnorderedList ul = new UnorderedList(); String name = ""; String id = ""; String salstr = ""; int sal = 0; for (int i = 0; i < qds.size(); i++) { Record rec = qds.getRecord(i); int tmp = rec.getValue("Emp Salary").asInt(); ul.addElement(new ListItem("$" + tmp)); if (tmp > sal) { sal = tmp; name = rec.getValue("Emp Name").asString(); id = rec.getValue("Emp ID").asString(); salstr = rec.getValue("Emp Salary").asString(); } } // Use an htmlKona page to display the data retrieved, and the // statements used to retrieve it HtmlPage hp = new HtmlPage(); hp.getHead() .addElement(new TitleElement("Highest Paid Employee")); hp.getBodyElement() .setAttribute(BodyElement.bgColor, HtmlColor.white); hp.getBody() .addElement(MarkupElement.HorizontalLine) .addElement(new HeadingElement("Query String: ", +2)) .addElement(stmt.toString()) .addElement(MarkupElement.HorizontalLine) .addElement("I examined the values: ") .addElement(ul) .addElement(MarkupElement.HorizontalLine) .addElement("Max salary of those employees examined is: ") .addElement(MarkupElement.Break) .addElement("Name: ") .addElement(new BoldElement(name)) .addElement(MarkupElement.Break) .addElement("ID: ") .addElement(new BoldElement(id)) .addElement(MarkupElement.Break) .addElement("Salary: ") .addElement(new BoldElement(salstr)) .addElement(MarkupElement.HorizontalLine); hp.output(); // Close QueryDataSet qds.close(); } catch (Exception e) { // Deal with any exceptions } finally { // Close the connection try {conn.close();} catch (Exception mye) { // Deal with any exceptions } } } }Note that we closed each Statement and DataSet after use, and that we closed the Connection in a finally block.
Step 1. Setting SelectStmt parametersYou can use a SelectStmt object to create a SQL query rather than creating the SQL statement yourself. SelectStmt objects shield you from the need to know the specifics of how a particular vendor implements SQL, and it also reduces syntax errors.When you create a TableDataSet, it is associated with an empty SelectStmt that you can then modify to form a query. In this example, we have already created a connection conn. Here is how you access a TableDataSet's SelectStmt: TableDataSet tds = new TableDataSet(conn, "empdemo"); SelectStmt sql = tds.selectStmt();Now set the parameters for the SelectStmt object. In the example, the first argument for each field is the attribute name and the second is the alias. This query will retrieve information about all employees who make less than $2000. sql.field("empno", "Emp ID") .field("ename", "Emp Name") .field("sal", "Emp Salary") .from("empdemo") .where("sal < 2000") .order("empno"); ![]() Step 2. Using QBE to refine the parametersThe SelectStmt object also gives you Query-by-example functionality. Query-by-example, or QBE, forms parameters for data retrieval using a set of phrases that follow the format column operator value. For example, "empno = 8000" is a Query-by-example phrase that can select all the rows in one or more tables where the field employee number ("empno", alias "Emp ID") equals 8000.We can further define the parameters for data selection by using the setQbe() and addQbe() methods in the SelectStmt class, as is shown here. These methods allow you to use vendor-specific QBE syntax in constructing a select statement. sql.setQbe("ename", "MURPHY") .addUnquotedQbe("empno", "8000");When you have finished, use the fetchRecords() method to populate the DataSet, as we did in the second tutorial.
As with most dbKona operations, you should begin by creating the Properties and Driver objects, and then instantiating a Connection. Refer to the first tutorial for this step.
String insert = "insert into empdemo(empno, " + "ename, job, deptno) values " + "(8000, 'MURPHY', 'SALESMAN', 10)";The second statement changes Murphy's name to Smith, and changes his job status from Salesman to Manager. String update = "update empdemo set ename = 'SMITH', " + "job = 'MANAGER' " + "where empno = 8000"; The third statement deletes this record from the database. String delete = "delete from empdemo where empno = 8000"; ![]() Step 2. Executing each SQL statementAs we execute each SQL statement, we'll save a snapshot of the table into a TableDataSet. Later we'll examine each TableDataSet to verify that the execute operation produced the expected results. Notice that TableDataSets are instantiated with the results of an executed query.Statement stmt1 = conn.createStatement(); stmt1.execute(insert); TableDataSet ds1 = new TableDataSet(conn, "emp"); ds1.where("empno = 8000"); ds1.fetchRecords();The methods associated with TableDataSet allow you to specify a SQL WHERE clause and a SQL ORDER BY clause and to set and add to a QBE statement. We use the TableDataSet in this example to requery the database table "emp" after each statement is executed to see the results of the execute() method. With the "where" clause, we narrow down the records in the table to just employee number 8000. Repeat the execute() method for the update and delete statements and capture the results into two more TableDataSets, ds2 and ds3.
ServletPage hp = new ServletPage(); hp.getHead() .addElement(new TitleElement("Modifying data with SQL")); hp.getBody() .addElement(MarkupElement.HorizontalLine) .addElement(new TableElement(tds)) .addElement(MarkupElement.HorizontalLine) .addElement(new HeadingElement("Query results afer INSERT", 2)) .addElement(new HeadingElement("SQL: ", 3)) .addElement(new LiteralElement(insert)) .addElement(new HeadingElement("Result: ", 3)) .addElement(new LiteralElement(ds1)) .addElement(MarkupElement.HorizontalLine) .addElement(new HeadingElement("Query results after UPDATE", 2)) .addElement(new HeadingElement("SQL: ", 3)) .addElement(new LiteralElement(update)) .addElement(new HeadingElement("Result: ", 3)) .addElement(new LiteralElement(ds2)) .addElement(MarkupElement.HorizontalLine) .addElement(new HeadingElement("Query results after DELETE", 2)) .addElement(new HeadingElement("SQL: ", 3)) .addElement(new LiteralElement(delete)) .addElement(new HeadingElement("Result: ", 3)) .addElement(new LiteralElement(ds3)) .addElement(MarkupElement.HorizontalLine); hp.output(); ![]() Code summaryHere is a summary of the example code we discussed in the sections on using a SelectStmt and modifying DBMS data with a SQL statement.import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.*; import weblogic.db.jdbc.*; import weblogic.html.*; public class InsertUpdateDelete extends HttpServlet { public synchronized void service(HttpServletRequest req, HttpServletResponse res) throws IOException { Connection conn = null; try { res.setStatus(HttpServletResponse.SC_OK); res.setContentType("text/html"); Properties props = new java.util.Properties(); props.put("user", "scott"); props.put("password", "tiger"); props.put("server", "DEMO"); Class.forName("weblogic.jdbc.oci.Driver"); Connection conn = DriverManager.getConnection("jdbc:weblogic:oracle", props); conn.setAutoCommit(false); // Create a TableDataSet with a SelectStmt TableDataSet tds = new TableDataSet(conn, "empdemo"); SelectStmt sql = tds.selectStmt(); sql.field("empno", "Emp ID") .field("ename", "Emp Name") .field("sal", "Emp Salary") .from("empdemo") .where("sal < 2000") .order("empno"); sql.setQbe("ename", "MURPHY") .addUnquotedQbe("empno", "8000"); tds.fetchRecords(); String insert = "insert into empdemo(empno, " + "ename, job, deptno) values " + "(8000, 'MURPHY', 'SALESMAN', 10)"; // Create a statement and execute it Statement stmt1 = conn.createStatement(); stmt1.execute(insert); stmt1.close(); // Verify results TableDataSet ds1 = new TableDataSet(conn, "empdemo"); ds1.where("empno = 8000"); ds1.fetchRecords(); // Create a statement and execute it String update = "update empdemo set ename = 'SMITH', " + "job = 'MANAGER' " + "where empno = 8000"; Statement stmt2 = conn.createStatement(); stmt2.execute(insert); stmt2.close(); // Verify results TableDataSet ds2 = new TableDataSet(conn, "empdemo"); ds2.where("empno = 8000"); ds2.fetchRecords(); // Create a statement and execute it String delete = "delete from empdemo where empno = 8000"; Statement stmt3 = conn.createStatement(); stmt3.execute(insert); stmt3.close(); // Verify results TableDataSet ds3 = new TableDataSet(conn, "empdemo"); ds3.where("empno = 8000"); ds3.fetchRecords(); // Create a servlet page to display the results ServletPage hp = new ServletPage(); hp.getHead() .addElement(new TitleElement("Modifying data with SQL")); hp.getBody() .addElement(MarkupElement.HorizontalRule) .addElement(new HeadingElement("Original table", 2)) .addElement(new TableElement(tds)) .addElement(MarkupElement.HorizontalRule) .addElement(new HeadingElement("Query results afer INSERT", 2)) .addElement(new HeadingElement("SQL: ", 3)) .addElement(new LiteralElement(insert)) .addElement(new HeadingElement("Result: ", 3)) .addElement(new LiteralElement(ds1)) .addElement(MarkupElement.HorizontalRule) .addElement(new HeadingElement("Query results after UPDATE", 2)) .addElement(new HeadingElement("SQL: ", 3)) .addElement(new LiteralElement(update)) .addElement(new HeadingElement("Result: ", 3)) .addElement(new LiteralElement(ds2)) .addElement(MarkupElement.HorizontalRule) .addElement(new HeadingElement("Query results after DELETE", 2)) .addElement(new HeadingElement("SQL: ", 3)) .addElement(new LiteralElement(delete)) .addElement(new HeadingElement("Result: ", 3)) .addElement(new LiteralElement(ds3)) .addElement(MarkupElement.HorizontalRule); hp.output(); tds.close(); ds1.close(); ds2.close(); ds3.close(); } catch (Exception e) { // Handle the exception } // Always close the connection in a finally block finally { conn.close(); } } } ![]() Modifying DBMS data with a KeyDef
The first step is to create a connection to the DBMS. In this example, we use the Connection object conn created in the first tutorial. The database table we use in this example is the Employee table ("empdemo"), with fields empno, ename, job, and deptno. The query we execute retrieves the full contents of the table "empdemo".
A KeyDef is created and built in the KeyDef class, as shown in this example. KeyDef key = new KeyDef().addAttrib("empno");If you are working with an Oracle database, you can construct the KeyDef with the attribute "ROWID," to do inserts and deletes on this Oracle key, as in this example: KeyDef key = new KeyDef().addAttrib("ROWID"); ![]() Step 2. Creating a TableDataSet with a KeyDefIn this example, we create a TableDataSet with the results of our query. We use the TableDataSet constructor that takes a Connection object, a DBMS table name, and a KeyDef as its arguments.TableDataSet tds = new TableDataSet(conn, "empdemo", key);The KeyDef becomes the reference for all changes that we will make to the data. Each time we save the TableDataSet, we change data in the database (according to the limits set on SQL UPDATE, INSERT, and DELETE operations) based on the value of the KeyDef attribute, which in this example is the employee number ("empno"). If you are working with an Oracle database and have added the attribute "ROWID" to the KeyDef, you can construct a TableDataSet for inserts and deletes like this: KeyDef key = new KeyDef().addAttrib("ROWID"); TableDataSet tds = new TableDataSet(conn, "empdemo", "ROWID, dept", key); tds.where("empno < 100"); tds.fetchRecords(); ![]() Step 3. Inserting a Record into the TableDataSetYou can create a new Record object in the context of the TableDataSet to which it is to be added with the addRecord() method from the TableDataSet class. Once you have added the record, you can set the values for each of its fields with the setValue() method from the Record class. You must set at least one value in a new Record if you intend to save it into the database: the KeyDef field.Record newrec = tds.addRecord(); newrec.setValue("empno", 8000) .setValue("ename", "MURPHY") .setValue("job", "SALESMAN") .setValue("deptno", 10); String insert = newrec.getSaveString(); tds.save();The getSaveString() method in the Record class returns the SQL string (a SQL UPDATE, DELETE, or INSERT statement) used to save a Record to the database. We have save this string into an object that we can display later to examine exactly how the insert operation was carried out.
newrec.setValue("ename", "SMITH") .setValue("job", "MANAGER"); String update = newrec.getSaveString(); tds.save(); ![]() Step 5. Deleting a Record from the TableDataSetYou can mark a record in a TableDataSet for deletion with the markToBeDeleted() method (or unmark it with the unmarkToBeDeleted() method) in the Record class. For instance, deleting the record we just created would be accomplished by marking the record for deletion, as shown here.newrec.markToBeDeleted(); String delete = newrec.getSaveString(); tds.save();Records marked for deletion are not removed from a TableDataSet until you save() it, or until you execute the removeDeletedRecords() method in the TableDataSet class. Records that have been removed from the TableDataSet but not yet deleted from the database (by the removeDeletedRecords() method) fall into a zombie state. You can determine whether a record is a zombie by testing it with the isAZombie() method in the Record class, as shown. if (!newrec.isAZombie()) { . . . } ![]() Step 6. More on saving the TableDataSetAt the end of each of the examples above for inserting, updating, and deleting records, we performed a save() operation on the TableDataSet.Saving a Record or a TableDataset will effectively save the data to the database. dbKona performs selective changes, that is, only data that has changed is saved. Inserting, updating, and deleting records in the TableDataSet affects only the data in the TableDataSet until you use the Record.save() or TableDataSet.save() method. Checking Record status before savingSeveral methods from the Record class return information about the state of a Record that you may want to know before a save() operation. Some of these are:
If you want to know whether a row will participate in a save to the DBMS, use the isClean() and the needsToBeSaved() methods. When you make modifications to a Record or TableDataSet, use the save() method from either class to save the changes to the database. In the previous steps, we saved the TableDataSet after each transaction as shown below. tds.save(); ![]() Step 7. Verifying the changesAfter making a change to a TableDataSet and saving it to the database, you can verify the changes by retrieving the database table into a QueryDataset or TableDataSet to examine and verify it. After each operation above, we could have created a new TableDataSet to examine and verify that the operations we performed succeeded.Here is the sample code for fetching just a single record, which is an efficient way to verify single-record changes. In this example, we use a TableDataSet with a query-by-example (QBE) clause to fetch just the record we're interested in. TableDataSet tds2 = new TableDataSet(conn, "empdemo"); tds2.where("empno = 8000") .fetchRecords();As a final step, we can display the query results after each step and the strings "insert", "update", and "delete" that we created after each save(). Refer to the code summary in the previous tutorial to use htmlKona for displaying the results. When you have finished with the DataSets, close each one with the close() method. tds.close(); tds2.close(); ![]() Code summaryHere a code example that uses some of the concepts covered in this topic.package tutorial.dbkona; import weblogic.db.jdbc.*; import java.sql.*; import java.util.Properties; public class rowid { public static void main(String[] argv) throws Exception { Class.forName("weblogic.jdbc.oci.Driver"); Connection conn = DriverManager.getConnection("jdbc:weblogic:oracle:DEMO", "scott", "tiger"); // Here we insert 100 records. TableDataSet ts1 = new TableDataSet(conn, "empdemo"); for (int i = 1; i <= 100; i++) { Record rec = ts1.addRecord(); rec.setValue("empid", i) .setValue("name", "Person " + i) .setValue("dept", i); } // Save new records. dbKona does selective saves, that is, // it saves only those records in the TableDataSet that have // changed to cut down on network traffic and server calls. System.out.println("Inserting " + ts1.size() + " records."); ts1.save(); // Close the DataSet now that we're finished with it. ts1.close(); // Define a KeyDef for updates and deletes. // ROWID is an Oracle specific field which can act as a // primary key for updates and deletes KeyDef key = new KeyDef().addAttrib("ROWID"); // Update the 100 records we originally added. TableDataSet ts2 = new TableDataSet(conn, "empdemo", "ROWID, dept", key); ts2.where("empid <= 100"); ts2.fetchRecords(); for (int i = 1; i <= ts2.size(); i++) { Record rec = ts2.getRecord(i); rec.setValue("dept", i + rec.getValue("dept").asInt()); } // 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(); // You should always close DataSets, ResultSets, and // Statements when you have finished working with them. ts2.close(); // Finally, make sure you close the connection. conn.close(); } } ![]() Using a JDBC PreparedStatement with dbKonaPart of the convenience of dbKona is that you do not need to know much about how to write vendor-specific SQL, since dbKona will compose syntactically correct SQL for you. In some cases, however, you may want to use a JDBC PreparedStatement object with dbKona. A JDBC PreparedStatement is used to precompile a SQL statement that will be used multiple times. You can clear the parameters for a PreparedStatement with a call to PreparedStatement.clearParameters(). A PreparedStatment object is constructed with the preparedStatement() method in the JDBC Connection class (the object used as conn in all of these examples). In this example, we create a PreparedStatement and then execute it within a loop. This statement has three IN parameters, employee id, name, and department. This will add 100 employees to the table. String inssql = "insert into empdemo(empid, " + "name, dept) values (?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(inssql); for (int i = 1; i <= 100; i++) { pstmt.setInt(1, i); pstmt.setString(2, "Person" + i); pstmt.setInt(3, i); pstmt.executeUpdate(); } pstmt.close();You should always close a Statement or PreparedStatement object when you have finished working with it. You can accomplish the same task with dbKona without worrying about the SQL. Use a KeyDef to set fields for update or delete. Check the tutorial Modifying DBMS data with a KeyDef for details.
The first step, as in any dbKona application, is to connect to the DBMS. The example code uses the same Connection object, conn, that we created in the first tutorial topic.
Statement stmtl = conn.createStatement(); stmtl.execute("CREATE OR REPLACE PROCEDURE proc_squareInt " + "(field1 IN OUT INTEGER, " + " field2 OUT INTEGER) IS " + "BEGIN field1 := field1 * field1; " + "field2 := field1 * 3; " + "END proc_squareInt;"); stmtl.close(); ![]() Step 2. Setting parametersWe use a JDBC CallableStatement in dbKona to execute a stored procedure. It allows us to set IN/OUT parameters and get return values. A CallableStatement object is constructed with the prepareCall() method in the JDBC Connection class.In this example, we use the setInt() method to set the first parameter to the integer "3". Then we register the second parameter as an OUT parameter of type java.sql.Types.INTEGER. Finally, we execute the stored procedure. CallableStatement cstmt = conn.prepareCall("BEGIN proc_squareInt(?, ?): END;"); cstmt.setInt(1, 3); cstmt.registerOutParameter(2, java.sql.Types.INTEGER); cstmt.execute();Note that Oracle does not natively support binding to "?" values in a SQL statement. Instead it uses ":1", ":2", etc. We allow you to use either in your SQL.
System.out.println(cstmt.getInt(1)); System.out.println(cstmt.getInt(2)); cstmt.close(); ![]() Using byte arrays for images and audioYou will probably also find htmlKona useful, which will make it easy to integrate database data retrieved with dbKona into an HTML environment. The example code that we use in this tutorial depends on htmlKona.
In this example, we use server-side Java running on a Netscape server posted from an htmlKona form to retrieve the name of the image that the user wants to view. With that image name, we query the contents of a database table called "imagetable" and get the first record of the results. You will notice that we use a SelectStmt object to construct a SQL query by QBE. After we retrieve the image record, we set the HTML page type to the image type and then retrieve the image data as an array of bytes (byte[]) into an htmlKona ImagePage, which will display the image in a browser. t3 = new T3Client("t3://bigbox:7005"); t3.connect(); // Get the name of the image from the user String iname = null; try { iname = (String)getFormData().get("imagename"); } catch (IOException e) {;} Properties dbprops = new java.util.Properties(); dbprops.put("user", "scott"); dbprops.put("password", "tiger"); dbprops.put("server", "DEMO"); Properties t3props = new java.util.Properties(); t3props.put("weblogic.t3", t3); t3props.put("weblogic.t3.dbprops", dbprops); t3props.put("weblogic.t3.driver", "weblogic.jdbc.oci.Driver"); t3props.put("weblogic.t3.url", "jdbc:weblogic:oracle"); t3props.put("weblogic.t3.cacheRows", "0"); Class.forName("weblogic.jdbc.t3client.Driver"); Connection conn = DriverManager.getConnection("jdbc:weblogic:t3client", t3props); if (iname != null) { // Retrieve the image from the database TableDataSet tds = new TableDataSet(conn, "imagetable"); tds.selectStmt().setQbe("name", iname); tds.fetchRecords(); Record rec = tds.getRecord(0); this.returnNormalResponse("image/" + rec.getValue("type").asString()); ImagePage hp = new ImagePage(rec.getValue("data").asBytes()); hp.output(getOutputStream()); }For the full working example, look at Displaying an image stored in a database on the htmlKona Examples page.
TableDataSet tds = new TableDataSet(conn, "imagetable"); Record rec = tds.addRecord(); rec.setValue("name", "vars") .setValue("type", "gif") .setValue("data", "c:/html/api/images/variables.gif"); rec = tds.addRecord(); rec.setValue("name", "excepts") .setValue("type", "jpeg") .setValue("data", "c:/html/api/images/exception-index.jpg"); tds.save(); tds.close(); ![]() Using dbKona for Oracle sequences
Constructing a dbKona Sequence objectYou construct a Sequence object with a JDBC Connection and the name of a sequence that already exists on an Oracle server. Here is an example:Sequence seq = new Sequence(conn, "mysequence"); ![]() Creating and destroying sequences on an Oracle server from dbKonaIf the Oracle sequence doesn't exist, you can create it from dbKona with the Sequence.create() method. That method takes four arguments: a JDBC Connection, a name for the sequence to be created, an increment interval, and a starting point. Here is an example that creates an Oracle sequence "mysequence" beginning at 1000 and increasing in increments of 1:Sequence.create(conn, "mysequence", 1, 1000);You can drop an Oracle sequence from dbKona, also, as in this example: Sequence.drop(conn, "mysequence"); ![]() Using a SequenceOnce you have created a Sequence object, you can use it to generate autoincrementing ints, for example, to set an autoincrementing key as you add records to a table. Use the nextValue() method to return an int that is the next increment in the Sequence. For example:TableDataSet tds = new TableDataSet(conn, "empdemo"); for (int i = 1; i <= 10; i++) { Record rec = tds.addRecord(); rec.setValue("empno", seq.nextValue()); } You can check the current value of a Sequence with the currentValue() method, but only after you have called the nextValue() method at least once. System.out.println("Records 1000-" + seq.currentValue() + " added."); ![]() Code summaryHere is a working code example that illustrates how to use concepts discussed in this section. First we attempt to drop a sequence named "testseq" from the Oracle server; that insures that we do not get an error when we try to create a sequence if one already exists by that name. Then we create a sequence on the server, and use its name to create a dbKona Sequence object.package tutorial.dbkona; import weblogic.db.jdbc.*; import weblogic.db.jdbc.oracle.*; import java.sql.*; import java.util.Properties; public class sequences { public static void main(String[] argv) throws Exception { Class.forName("weblogic.jdbc.oci.Driver"); Connection conn = DriverManager.getConnection("jdbc:weblogic:oracle:DEMO", "scott", "tiger"); // Drop the sequence if it already exists on the server. try {Sequence.drop(conn, "testseq");} catch (Exception e) {;} // Create a new sequence on the server. Sequence.create(conn, "testseq", 1, 1); Sequence seq = new Sequence(conn, "testseq"); // Print out the next value in the sequence in a loop. for (int i = 1; i <= 10; i++) { System.out.println(seq.nextValue()); } System.out.println(seq.currentValue()); // Drop the sequence from the server // and close the Sequence object. Sequence.drop(conn, "testseq"); seq.close(); // Finally, close the connection. conn.close(); } }
|
|
Copyright © 2000 BEA Systems, Inc. All rights reserved.
|