通过python连接到Oracle DB

时间:2019-12-16 12:54:18

标签: python oracle jdbc

我正在使用jaydebeapi连接到Oracle数据库。代码如下:

host = [address]
port = "1521"
sid = "ctginst1"
database = "oracle"
drivertype = "thin"
uid = [user]
pwd = [pass]

driver_class = "oracle.jdbc.OracleDriver"

driver_file = "ojdbc10.jar"

connection_string="jdbc:{}:{}@{}:{}:{}".format(database, drivertype, host, port, sid)

conn=jaydebeapi.connect(driver_class, connection_string, [uid, pwd], driver_file, )

但是这失败了,并给了我一个错误:

java.lang.RuntimeException: Class oracle.jdbc.OracleDriver not found

编辑: 通过在启动JVM时将CLASSPATH和.jar的位置传递给我,然后才尝试连接,我设法进一步进行了

import jpype
jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path=%s' % driver_file)

现在我遇到java.sql.SQLException: Invalid Oracle URL specified错误

1 个答案:

答案 0 :(得分:0)

好的,因此从那里开始,在“ @”之前显然缺少一个冒号。 完整的成功连接代码如下:

import jaydebeapi
import jpype

host = host
port = "1521"
sid = "ctginst1"
database = "oracle"
drivertype = "thin"
uid = user
pwd = password

driver_class = "oracle.jdbc.OracleDriver"

driver_file = "C:\ojdbc8.jar"

connection_string="jdbc:{}:{}:@{}:{}:{}".format(database, drivertype, host, port, sid)

jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path=%s' % driver_file)

conn=jaydebeapi.connect(driver_class, connection_string, [uid, pwd])