实际操作环境为: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数据库