![]() |
Cloudscape Database Applications
|
|
Working with the JDBC InterfaceThe preceding section discussed the different ways of deploying the Cloudscape products. Despite their differences, these deployment options have something in common: namely, the JDBC interface. All Cloudscape applications use the JDBC interface to interact with Cloudscape, whether it is embedded or a server. NOTE: Application servers may provide a connectivity option other than JDBC to their client applications. For example, BEA WebLogic's application server provides a dbKona layer above JDBC that client applications can use instead of JDBC. A servlet application enables Web browsers to be non-JDBC client applications. However, the application server itself must use JDBC for interacting with Cloudscape. Throughout the Cloudscape documentation, the term Cloudscape application refers to an application that interacts with Cloudscape using the JDBC interface. A Java application talks to a database using the classes and methods of the java.sql package. Sun offers a tutorial for learning the JDBC interface: JDBC Database Access with Java, Hamilton, Cattell, and Fisher. Addison Wesley: 1997 (http://java.sun.com/docs/books/jdbc/summary.html). This section does not go into great detail about the JDBC interface. Instead, it offers a brief overview and teaches you some highlights. To develop Cloudscape applications successfully, you will need to learn the JDBC interface as well as SQL-J. Applications and Queries: Statements and ResultSetsWhen you worked with Cloudview, you used its SQL window to send SQL-J statements to Cloudscape. A Java application typically uses a Statement object or a PreparedStatement object to send SQL-J statements to Cloudscape. A Statement object lets you execute a static SQL-J statement and obtain its results. You create a Statement object using the createStatement method of the Connection class: Statement stmt = conn.createStatement(); After you create a Statement object, invoke one of its execute methods to send an SQL-J statement to Cloudscape. When you invoke it, supply an SQL-J statement as a string argument to the method. If the SQL-J statement is a query (returns data) and does not modify a schema or data, use the executeQuery method; if it modifies schema or data, use the executeUpdate method. (Use execute for those situations in which you don't know in advance what the statement does.) Queries return results; when executing queries, you assign the results to a ResultSet object. Run and Examine HelloWorldApp.javaHelloWorldApp.java is a simple Java application that interacts with HelloWorldDB--it queries a table and prints the results.
The program loads the Cloudscape JDBC driver and gets a connection to HelloWorldDB. (You learned how to do this in Lesson 2, "Cloudscape Basics and the Sample Database".) Then the program creates a Statement object against the connection. The program executes the Statement, assigning the results to a ResultSet object. The application then steps through the rows in the ResultSet. When in a row, it accesses the contents of a particular column using the getString method and specifying the column number (the order in which the column appears in the query) as an argument. JDBC begins counting at 1, not 0. There are different methods for different data types--an integer data type would require a getInt method, and so on. The SAYING column appears first in the query, so getString(1) retrieves the value in the SAYING column for each row. 1 refers to the first column in the result. |
|
![]() Cloudscape Version 3.6 For information about Cloudscape technical support, go to: www.cloudscape.com/support/.Copyright © 1998, 1999, 2000 Informix Software, Inc. All rights reserved. |