Jython中链接Oracle数据库

时间:2014-05-09 00:20:23   收藏:0   阅读:348

实际操作环境为:Eclipse+Pydev中使用Jython链接Oracle数据库。方法有二,如下所示。(注意:将要使用的.jar文件路径加入系统变量classpath中)

一:

Note:使用ojdbc.jar库

from oracle.jdbc.driver import OracleDriver

from java.sql import DriverManager

def connect(host , port , sid , user , password):

  driver = OracleDriver()

  DriverManager.registerDriver(driver)

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

  conn = DriverManager.getConnect(connection , user , password)

  stmt = conn.createStatement()

  rset = stmt.executeQuery("select * from table1")

  while(rset.next()):

    print rset.getString(1)

  stmt.close()

 

二:

Note:使用zxJDBC.jar,符合Python DB API 规范。

from com.ziclix.python.sql import zxJDBC

def connect():

  driver = "oracle.jdbc.driver.OracleDriver"

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

  conn = zxJDBC.connect(url , user , password , driver)

  cur = conn.cursor()

  cur.execute("select * from table2")

  for data in cur.fetchall():

    print data

  cur.close()

  conn.commit()

  conn.close()

 

 

Jython中链接Oracle数据库,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!