A simple example of connecting to the Sybase ASE database using Java

Source: Internet
Author: User

This example shows how to connect to the Sybase ASE database in java. A simple java program is used for testing.

Step 1: Obtain the jdbc for sybase driver
Download JDBC driver: http://www.sybase.com/detail? Id = 1009726

Can also download to CSDN: http://download.csdn.net/source/2697077
There are two types: jConnect6.05 and jConnect5.5. JConnect6.05 contains the jconn3.jar package, and jConnect5.5 contains the jconn2.jar package. We recommend that you use the latest Sybase driver jconn3.jar. However, my example is very simple and can be used in both cases.

Download: jconn3.jar, download: jconn2.jar

Step 2: Set the environment variable CLASSPATH

Put the downloaded jconn2.jar or jconn3.jar to the Directory D: \ sybase \ Shared \ lib. Set the environment variable CLASSPATH to D: \ sybase \ Shared \ lib \ jconn2.jar.

Set CLASSPATH =.; d: \ sybase \ shared \ lib \ jconn2.jar; (Note: the first "." indicates the current directory .)

Step 3: write a simple java program.

The following program uses jconn2.jar to connect to the Sybase ASE15.0.3 database and extract the ID and name columns of the User table in the master database.

The IP address of the Sybase ASE database is 192.168.0.168 and port: 5000. The logon sa password is sybase.

Run the select id, name, crdate from dbo. sysobjects where type = 'U' statement to return to the current database (this is specified in the url !) The id and name columns of the User table in.

Import java. SQL .*;
Import java. util .*;

Public class Conn2ASE {
Public static void main (String [] args ){
Try {
Class. forName ("com. sybase. jdbc2.jdbc. SybDriver"). newInstance ();
String url = "jdbc: sybase: Tds: 192.168.0.168: 5000/master"; // Database Name
Properties sysProps = System. getProperties ();
SysProps. put ("user", "sa"); // set the database access Username
SysProps. put ("password", "sybase"); // password
Connection conn = DriverManager. getConnection (url, sysProps );
Statement stmt = conn
. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE,
ResultSet. CONCUR_UPDATABLE );
String SQL = "select id, name, crdate from dbo. sysobjects where type = 'U'"; // table
ResultSet rs = stmt.exe cuteQuery (SQL );
While (rs. next ()){
System. out. println ("oject_id:" + rs. getString (1) + ", oject_name:" + rs. getString (2); // obtain the value of the second column
}
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
}
}
 

Save the above Code to: d: \ sybase \ shared \ demo \ Conn2ASE. java

Download: Conn2ASE. java

Step 4: Compile the java source file as a class file

Javac.exe Conn2ASE. java

C: \ Documents ents and Settings \ Administrator> cd/d: \ sybase \ shared \ demo

D: \ sybase \ Shared \ demo> set CLASSPATH =.; d: \ sybase \ shared \ lib \ jconn2.jar;

D: \ sybase \ Shared \ demo> D: \ sybase \ WorkSpace \ JDK1.5.0 _ 15 \ bin \ javac.exe Conn2ASE. jav
A

D: \ sybase \ Shared \ demo> dir
The volume in drive D has no labels.
The serial number of the volume is 24C3-2A76.

D: \ sybase \ Shared \ demo directory

<DIR>.
<DIR> ..
1,683 Conn2ASE. class
1,107 Conn2ASE. java
2 files in 2,790 bytes
2 directories, 18,468,270,080 available bytes

Step 5: execute a java program to connect to the Sybase ASE database and display the query results

Java Conn2ASE

D: \ sybase \ Shared \ demo> java Conn2ASE
Oject_id: 960003420, oject_name: spt_values
Oject_id: 992003534, oject_name: spt_monitor
Oject_id: 1040003705, oject_name: spt_limit_types
Oject_id: 1088003876, oject_name: syblicenseslog
Oject_id: 1120003990, oject_name: spt_ijdbc_table_types
Oject_id: 1152004104, oject_name: spt_ijdbc_mda
Oject_id: 1200004275, oject_name: spt_ijdbc_conversion
Oject_id: 1232004389, oject_name: ijdbc_function_escapes
Oject_id: 1440005130, oject_name: monTables
Oject_id: 1472005244, oject_name: monTableParameters
Oject_id: 1504005358, oject_name: monTableColumns
Oject_id: 1536005472, oject_name: monState
Oject_id: 1568005586, oject_name: monEngine
Oject_id: 1600005700, oject_name: monDataCache
Oject_id: 1632005814, oject_name: monProcedureCache
Oject_id: 1664005928, oject_name: monprocedurecachemememoryusage
Oject_id: 1696006042, oject_name: monProcedureCacheModuleUsage
Oject_id: 1728006156, oject_name: monOpenDatabases
Oject_id: 1760006270, oject_name: monSysWorkerThread
Oject_id: 1792006384, oject_name: monNetworkIO
Oject_id: 1824006498, oject_name: monErrorLog
Oject_id: 1856006612, oject_name: monLocks
Oject_id: 1888006726, oject_name: monDeadLock
Oject_id: 1920006840, oject_name: monWaitClassInfo
Oject_id: 1952006954, oject_name: monWaitEventInfo
Oject_id: 1984007068, oject_name: monCachedObject
Oject_id: 2016007182, oject_name: monCachePool
Oject_id: 2048007296, oject_name: monOpenObjectActivity
Oject_id: 2080007410, oject_name: monIOQueue
Oject_id: 2112007524, oject_name: monDeviceIO
Oject_id: 2144007638, oject_name: monSysWaits
Oject_id: 28524104, oject_name: monProcess
Oject_id: 60524218, oject_name: monProcessLookup
Oject_id: 92524332, oject_name: monProcessActivity
Oject_id: 124524446, oject_name: monProcessWorkerThread
Oject_id: 156524560, oject_name: monProcessNetIO
Oject_id: 188524674, oject_name: monProcessObject
Oject_id: 220524788, oject_name: monProcessWaits
Oject_id: 252524902, oject_name: monProcessStatement
Oject_id: 284525016, oject_name: monSysStatement
Oject_id: 316525130, oject_name: monProcessSQLText
Oject_id: 348525244, oject_name: monSysSQLText
Oject_id: 380525358, oject_name: monCachedProcedures
Oject_id: 412525472, oject_name: monProcessProcedures
Oject_id: 444525586, oject_name: monSysPlanText
Oject_id: 476525700, oject_name: monOpenPartitionActivity
Oject_id: 508525814, oject_name: monLicense
Oject_id: 540525928, oject_name: monStatementCache
Oject_id: 572526042, oject_name: monCachedStatement
Oject_id: 604526156, oject_name: monPCIBridge
Oject_id: 636526270, oject_name: monPCISlots
Oject_id: 668526384, oject_name: monPCIEngine
Oject_id: 700526498, oject_name: monSQLRepActivity
Oject_id: 732526612, oject_name: monSQLRepMisses
Oject_id: 764526726, oject_name: spt_jdbc_table_types
Oject_id: 796526840, oject_name: spt_mda
Oject_id: 844527011, oject_name: spt_jtext
Oject_id: 908527239, oject_name: spt_jdbc_conversion
Oject_id: 940527353, oject_name: jdbc_function_escapes
Oject_id: 1100527923, oject_name: pbcattbl
Oject_id: 1148528094, oject_name: pbcatcol
Oject_id: 1196528265, oject_name: pbcatfmt
Oject_id: 1244528436, oject_name: pbcatvld
Oject_id: 1276528550, oject_name: pbcatedt
Oject_id: 1468529234, oject_name: test

The same is true for replacing jconn2.jar with jconn3.jar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.