| 導購 | 订阅 | 在线投稿
分享
 
 
 

講解jython訪問Oracle數據庫的具體步驟

來源:互聯網  2008-06-01 02:20:55  評論

需要的准備工作:

下載

http://jaist.dl.sourceforge.net/sourceforge/zxjdbc/zxJDBC-2.1.tar.gz

http://jaist.dl.sourceforge.net/sourceforge/jython/jython_installer-2.2.1.jar

根據自己的jdk版本下載對應的ojdbc版本

http://download.oracle.com/otn/utilities_drivers/jdbc/10203/ojdbc14.jar

http://download.oracle.com/otn/utilities_drivers/jdbc/111060/lib/ojdbc5.jar

http://download.oracle.com/otn/utilities_drivers/jdbc/111060/lib/ojdbc6.jar

安裝(需要已經安裝了jdk/jre?)

java -jar jython_installer-2.2.1.jar 安裝到C:\jython2.2.1

將zxJDBC.jar解壓縮到c:\tmp

將mm.mysql-2.0.4-bin.jar複制到c:\tmp

C:\jython2.2.1>set CLASSPATH=c:\tmp\ojdbc5.jar;c:\tmp\zxJDBC.jar;%CLASSPATH%

啓動Oracle服務器:

進入jython環境訪問Oracle

編輯文件c:\tmp\ora.py

from oracle.jdbc.driver import OracleDriver

from java.sql import DriverManager

def connect(un, pw, sid, host, port):

driver = OracleDriver()

DriverManager.registerDriver(driver)

connection = "jdbc:oracle:thin:@%s:%s:%s" % (host, port, sid)

conn = DriverManager.getConnection(connection, un, pw)

return conn

def doStuff(conn):

stmt = conn.createStatement()

rset = stmt.executeQuery("SELECT banner FROM sys.v_$version")

while (rset.next()):

print rset.getString(1)

stmt.close()

if __name__ == "__main__":

un = 'ebc20'

pw = 'abc'

sid = 'abc'

host = '10.4.231.16'

port = '1521'

conn = connect(un, pw, sid, host, port)

doStuff(conn)

C:\jython2.2.1>jython \tmp\ora.py

Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi

PL/SQL Release 10.2.0.3.0 - Production

CORE 10.2.0.3.0 Production

TNS for HPUX: Version 10.2.0.3.0 - Production

NLSRTL Version 10.2.0.3.0 - Production

然後用jython自己封裝的dbexts庫。

最後編輯一個名爲dbexts.ini的文件保存到c:\jython2.2.1\lib。

[default]

name=mysqltest

[jdbc]

name=ora

url=jdbc:oracle:thin:@10.4.231.16:1521:abc

user=ebc20

pwd=abc

driver=oracle.jdbc.driver.OracleDriver

datahandler=com.ziclix.python.sql.handler.OracleDataHandler

進入jython

>>> from dbexts import dbexts

>>> oracon = dbexts("ora", "c:\jython2.2.1\lib\dbexts.ini")

>>> oracon.isql("SELECT * FROM v$version")

BANNER

----------------------------------------------------------------

Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi

PL/SQL Release 10.2.0.3.0 - Production

CORE 10.2.0.3.0 Production

TNS for HPUX: Version 10.2.0.3.0 - Production

NLSRTL Version 10.2.0.3.0 - Production

5 rows affected

需要的准備工作: 下載 http://jaist.dl.sourceforge.net/sourceforge/zxjdbc/zxJDBC-2.1.tar.gz http://jaist.dl.sourceforge.net/sourceforge/jython/jython_installer-2.2.1.jar 根據自己的jdk版本下載對應的ojdbc版本 http://download.oracle.com/otn/utilities_drivers/jdbc/10203/ojdbc14.jar http://download.oracle.com/otn/utilities_drivers/jdbc/111060/lib/ojdbc5.jar http://download.oracle.com/otn/utilities_drivers/jdbc/111060/lib/ojdbc6.jar 安裝(需要已經安裝了jdk/jre?) java -jar jython_installer-2.2.1.jar 安裝到C:\jython2.2.1 將zxJDBC.jar解壓縮到c:\tmp 將mm.mysql-2.0.4-bin.jar複制到c:\tmp C:\jython2.2.1>set CLASSPATH=c:\tmp\ojdbc5.jar;c:\tmp\zxJDBC.jar;%CLASSPATH% 啓動Oracle服務器: 進入jython環境訪問Oracle 編輯文件c:\tmp\ora.py from oracle.jdbc.driver import OracleDriver from java.sql import DriverManager def connect(un, pw, sid, host, port): driver = OracleDriver() DriverManager.registerDriver(driver) connection = "jdbc:oracle:thin:@%s:%s:%s" % (host, port, sid) conn = DriverManager.getConnection(connection, un, pw) return conn def doStuff(conn): stmt = conn.createStatement() rset = stmt.executeQuery("SELECT banner FROM sys.v_$version") while (rset.next()): print rset.getString(1) stmt.close() if __name__ == "__main__": un = 'ebc20' pw = 'abc' sid = 'abc' host = '10.4.231.16' port = '1521' conn = connect(un, pw, sid, host, port) doStuff(conn) C:\jython2.2.1>jython \tmp\ora.py Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi PL/SQL Release 10.2.0.3.0 - Production CORE 10.2.0.3.0 Production TNS for HPUX: Version 10.2.0.3.0 - Production NLSRTL Version 10.2.0.3.0 - Production 然後用jython自己封裝的dbexts庫。 最後編輯一個名爲dbexts.ini的文件保存到c:\jython2.2.1\lib。 [default] name=mysqltest [jdbc] name=ora url=jdbc:oracle:thin:@10.4.231.16:1521:abc user=ebc20 pwd=abc driver=oracle.jdbc.driver.OracleDriver datahandler=com.ziclix.python.sql.handler.OracleDataHandler 進入jython >>> from dbexts import dbexts >>> oracon = dbexts("ora", "c:\jython2.2.1\lib\dbexts.ini") >>> oracon.isql("SELECT * FROM v$version") BANNER ---------------------------------------------------------------- Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi PL/SQL Release 10.2.0.3.0 - Production CORE 10.2.0.3.0 Production TNS for HPUX: Version 10.2.0.3.0 - Production NLSRTL Version 10.2.0.3.0 - Production 5 rows affected
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有