Java连接Sybase ASE数据库的一个简单例子

演示在用java连接Sybase ASE数据库。用了一个简单的java程序作试验。

第一步:获得jdbc for sybase驱动程序
下载JDBC驱动:http://www.sybase.com/detail?id=1009726

也可到CSDN下载:http://download.csdn.net/source/2697077
有jConnect6.05和jConnect5.5两种。jConnect6.05中包含jconn3.jar包,jConnect5.5中包含jconn2.jar包。建议最新的Sybase驱动jconn3.jar。不过我的这个例子很简单,两者都可以用。

下载:jconn3.jar ,下载:jconn2.jar

第二步:设置环境变量CLASSPATH

将下载下来的jconn2.jar或jconn3.jar放到目录D:\sybase\Shared\lib下。将环境变量CLASSPATH设置为:D:\sybase\Shared\lib\jconn2.jar

set CLASSPATH=.;d:\sybase\shared\lib\jconn2.jar;   (注意:第一个“.”,这个代表当前目录。)

第三步:编写一段简单的java程序。

下面的这段程序用jconn2.jar连接Sybase ASE15.0.3数据库并提取master数据库中的用户表的ID和name两列信息。

Sybase ASE数据库的ip地址为:192.168.0.168,端口:5000。登录sa的密码为:sybase

执行:select id,name,crdate from dbo.sysobjects where type='U'   这条语句返回当前数据库(这在url中指定!)中的用户表的id和name列。

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";// 数据库名
            Properties sysProps = System.getProperties();
            sysProps.put("user", "sa"); // 设置数据库访问用户名
            sysProps.put("password", "sybase"); // 密码
            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'"; // 表
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                System.out.println("oject_id:"+rs.getString(1)+",oject_name:"+rs.getString(2)); // 取得第二列的值
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

将上面这段代码保存到:d:\sybase\shared\demo\Conn2ASE.java

下载:Conn2ASE.java

第四步:编译java源文件为class文件

Java编译器javac.exe的版本为:1.5.0_15

javac.exe Conn2ASE.java

C:\Documents and Settings\Administrator>cd /d 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
 驱动器 D 中的卷没有标签。
 卷的序列号是 24C3-2A76

 D:\sybase\Shared\demo 的目录

2010-09-15  17:08    <DIR>          .
2010-09-15  17:08    <DIR>          ..
2010-09-15  17:08             1,683 Conn2ASE.class
2010-09-15  17:02             1,107 Conn2ASE.java
               2 个文件          2,790 字节
               2 个目录 18,468,270,080 可用字节

第五步:执行java程序连接Sybase ASE数据库,并显示查询结果

java程序的版本也是:1.5.0_15

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:monProcedureCacheMemoryUsage
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

上面演示时用到的jconn2.jar换成jconn3.jar也是一样的。

  • 本文链接地址:http://www.dbainfo.net/java-connect-to-sybase-ase-demo.htm
  • 本文为dbainfo个人原创,请在尊重作者劳动成果的前提下进行转载;
  • 转载务必注明原始出处 : Sybase数据库技术,数据库恢复专家
  • 对《Java连接Sybase ASE数据库的一个简单例子》有何疑问或见解,请在本文下方发表;
  • 对网站还有其他问题或建议,请提交在留言板,谢谢!
  • 目前还没有任何评论.
    :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)