Java串連Sybase ASE資料庫的一個簡單例子

來源:互聯網
上載者:User

示範在用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)); // 取得第二列的值
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
 

將上面這段代碼儲存到:d:\sybase\shared\demo\Conn2ASE.java

下載:Conn2ASE.java

第四步:編譯java源檔案為class檔案

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 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也是一樣的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.