Sumedh

How to connect Java with a Sybase database using JDBC




Published - Jul 2/2007
Last Updated - Jul 2/2009
Author - Sumedh
Note - This tutorial assumes that you know how to compile and run a java program.





Introduction

This tutorial will show you step-by-step how to establish a connection from your Java programs to a Sybase database using JDBC. Before we start, make sure you have Java SDK and Sybase SQL Anywhere installed on your machine.

Download the java file, which you will need to connect to a sybase database here.



Obtaining Jconnect

Jconnect is the name of the package which contains the JDBC driver. You can use the jdbc driver provided by Sybase or you can download the driver from Sun's website. I am using Jconnect 6.05, even if the version has changed, the steps remain same. Unzip Jconnect to C:\Jdbc. Although you can unzip jconnect anywhere in your system, we will have to edit the classpath later. So make sure you make appropriate changes to your classpath incase you use a different location.



Editing Classpath

- Right click on My Computer
- Select Properties
- Click on Advanced
- Click on Environment Variables

In the System Variables section, select CLASSPATH variable and cick on Edit. If you cannot see the CLASSPATH variable you will have to create it, although if you have installed Java properly, CLASSPATH should be present.


If CLASSPATH does not exists, follow this step.

- Click on New
- Fill the two text boxes as shown in the image.

New CLasspath jconn, jdbc


If CLASSPATH already exists, follow this step.

Select the CLASSPATH variable and click on Edit.
- Copy and paste the line below in the Variable value text box.

;C:\Jdbc;C:\Jdbc\classes\jconn3.jar

Note - Do not forget the semicolon if your CLASSPATH contains additional entries. If your CLASSPATH does not contain additional entries, remove the preceding semicolon.



Please go through the steps again and make sure you get eveything right. Small mistake here will have terrible consequences later. Following steps are common for both conditions.

- Click on OK, OK, OK.
After clickin the final OK button you will get a prompt to restart the system. After restarting the system, open a command prompt window. Type 'echo %CLASSPATH%' ( without the quotes )

If your output is similar to the one shown below, you have successfully created/edited the CLASSPATH variable.

Note - Your CLASSPATH may also have other additional entires which you must have created earlier.

Jconnect class path


Start Syabse Central

Start Sybase Central, after the program is up and running, double click on its tray icon. You will see an image similar to the one shown below.

Sybase Central Port Number

Get the port number which sybase central is using.


Establishing the connection

Once you have the correct JDBC driver installed, establishing a connection from your Java program to Sybase database is pretty easy.

Before we compile the test.java file, open the file in your favorite text editor and edit the arguments of the openDatabase method and put your database name, port number, username and password.

Connection db = openDatabase("com.sybase.jdbc3.jdbc.SybDriver", "jdbc:sybase:Tds:localhost:2638?ServiceName=storage", "Test", "Dummy");



I have a table named 'test_table' on which i am executing the 'select *' sql query. Change the test_table to an appropriate table name from your database. Make similar changes with 'insert into' sql query.

get_rs = get_stmt.executeQuery("select * from test_table ");

update_stmt.executeUpdate("insert into test_table values ( 'Success', 1 )");



Just compile and run the test.java program.


javac test.java
java test



Conclusion

If everything worked properly you will see the contents of the 'test_table' table. If it did not run properly, double check your classpath, connection string. Try to find the root cause of the problem by reading the error thrown by the java program.


Sumedh K, All rights reserved.