Appendix B jConnect Sample Programs
jConnect includes several sample programs that illustrate many of the topics covered in this chapter, and to help you understand how jConnect works with various JDBC classes and methods. In addition, this section includes a sample code fragment for your reference.
When you install jConnect, you can also the install sample programs. These samples include the source code so that you can review how jConnect implements various JDBC classes and methods. See the jConnect for JDBC Installation Guide for complete instructions for installing the sample programs.
The jConnect sample programs are intended for demonstration
purposes only.
The sample programs are installed in the sample subdirectory (jConnect 4.x) or sample2 subdirectory (jConnect 5.x) under your jConnect installation directory. The file index.html in the sample or sample2 subdirectory contains a complete list of the samples that are available along with a description of each sample. index.html also lets you view and run the sample programs as applets.
Using your Web browser, you can run some of the sample programs as applets. This enables you to view the source code while viewing the output results.
To run the samples as applets, you need to start the Web server gateway.
Use your Web browser to open index.html:
For jConnect 4.x, enter:
http://localhost:8000/sample/index.html
For jConnect 5.x, enter:
http://localhost:8000/sample2/index.html
All of the sample programs are compatible with Adaptive Server, but only a limited number are compatible with Adaptive Server Anywhere. Refer to index.html in the sample or sample2 subdirectory for a current list of the sample programs that are compatible with Adaptive Server Anywhere.
To run the sample programs that are available for Adaptive Server Anywhere, you must install the pubs2_any.sql script on your Adaptive Server Anywhere server. This script is located in the sample (jConnect 4.1) or sample2 (jConnect 5.0) subdirectory.
For Windows, go to DOS command window and enter:
java IsqlApp -U dba -P password -S jdbc:sybase:Tds:[hostname]:[port] -I %JDBC_HOME%\sample\pubs2_any.sql -c go
For UNIX, enter:
java IsqlApp -U dba -P password -S jdbc:sybase:Tds:[hostname]:[port] -I $JDBC_HOME/sample/pubs2_any.sql -c go
The following sample code illustrates how to invoke the jConnect driver, make a connection, issue a SQL statement, and process results.
import java.io.*; import java.sql.*; public class SampleCode { public static void main(String args[]) { try { /* * Open the connection. May throw a SQLException. */ Connection con = DriverManager.getConnection( "jdbc:sybase:Tds:myserver:3767", "sa", ""); /* * Create a statement object, the container for the SQL * statement. May throw a SQLException. */ Statement stmt = con.createStatement(); /* * Create a result set object by executing the query. * May throw a SQLException. */ ResultSet rs = stmt.executeQuery("Select 1"); /* * Process the result set. */ if (rs.next()) { int value = rs.getInt(1); System.out.println("Fetched value " + value); } } /* * Exception handling. */ catch (SQLException sqe) { System.out.println("Unexpected exception : " + sqe.toString() + ", sqlstate = " + sqe.getSQLState()); System.exit(1); } System.exit(0); } }
Copyright © 2001 Sybase, Inc. All rights reserved. |