2009. 10. 14. 00:04
2009. 10. 14. 00:04 in
Information Technology
Running jConnect sample programs and code
jConnect includes several sample programs that illustrate many of the topics covered in this chapter and that are intended 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.
Sample applications
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 sample2 subdirectory under your jConnect installation directory. The file index.html in the 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.
Running the sample 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:
Running the sample programs with Adaptive Server Anywhere
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 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 sample2 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%\sample2\pubs2_any.sql -c go
For UNIX, enter:
java IsqlApp -U dba -P password
-S jdbc:sybase:Tds:[hostname]:[port]
-I $JDBC_HOME/sample2/pubs2_any.sql -c go
Sample code
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.
*/
DriverManager.registerDriver(
(Driver) Class.forName(
"com.sybase.jdbc3.jdbc.SybDriver").newInstance());
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);
}
rs.close()
stmt.close()
con.close()
}//end try
/*
* Exception handling.
*/
catch (SQLException sqe)
{
System.out.println("Unexpected exception : " +
sqe.toString() + ", sqlstate = " +
sqe.getSQLState());
System.exit(1);
}//end catch
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}//end catch
System.exit(0);
}
}
Posted by 지에또일