通过python连接到Oracle DB

问题描述 投票:1回答:1

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

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错误

python oracle jdbc
1个回答
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])
© www.soinside.com 2019 - 2024. All rights reserved.