![]() | ![]() |
Home |
|
|
jConnect for JDBC Programmer's Reference |
|
| Appendix B jConnect Sample Programs |
Appendix B
This appendix is a guide to jConnect sample programs.
IsqlApp allows you to issue isql commands from the command line, and run jConnect sample programs.
The syntax for IsqlApp is:
IsqlApp [-U username] [-P password]
[-S servername]
[-G gateway]
[-p {http|https}]
[-D debug-class-list]
[-v]
[-I input-command-file]
[-c command_terminator]
[-C charset] [-L language]
[-T sessionID] [-V <version {2,3,4,5}>]Parameter | Description |
-U | The login ID with which you want to connect to a server. |
-P | The password for the specified login ID. |
-S | The name of the server to which you want to connect. |
-G | Gateway address. For the HTTP protocol, the URL is: http://host:port. To use the HTTPS protocol that supports encryption, the URL is https://host:port/servlet_alias. |
-p | Specifies whether you want to use the HTTP protocol or the HTTPS protocol that supports encryption. |
-D | Turns on debugging for all classes or for just the ones you specify, separated by a comma. For example, -D ALL displays debugging output for all classes. -D SybConnection, Tds displays debugging output only for the SybConnection and Tds classes. |
-v | Turns on verbose output for display or printing. |
-I | Causes IsqlApp to take commands from a file instead of the keyboard. After the parameter, you specify the name of the file to use for the IsqlApp input. The file must contain command terminators ("go" by default). |
-c | Lets you specify a keyword (for example, "go") that, when entered on a line by itself, terminates the command. This lets you enter multiline commands before using the terminator keyword. If you do not specify a command terminator, each new line terminates a command. |
-C | Specifies the character set for strings passed through TDS. If you do not specify a character set, IsqlApp uses the server's default charset. |
-L | The language in which to display error messages returned from the server and for jConnect messages. |
-T | When this parameter is set, jConnect assumes that an application is trying to resume communication on an existing TDS session held open by the TDS-tunnelling gateway. jConnect skips the login negotiations and forwards all requests from the application to the specified session ID. |
-V | Enables the use version-specific characteristics. See "JCONNECT_VERSION connection property". |
You must enter a space after each option flag.
To obtain a full description of the command-line options, enter:
java IsqlApp -help
The following example shows how to connect to a database on a host named "myserver" through port "3756" and run an isql script named "myscript":
java IsqlApp -U sa -P sapassword -S jdbc:sybase:Tds:myserver:3756 -I $JDBC_HOME/sp/myscript -c run
An applet that provides GUI access to isql commands is available as: $JDBC_HOME/sample2/gateway.html (UNIX) %JDBC_HOME%\sample2\gateway.html (Windows)
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 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 appletsUsing 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:
Enter:
http://localhost:8000/sample2/index.html
Running the sample programs with Adaptive Server AnywhereAll 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
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.jdbc2.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);
}
}
|
|