问题
This sample code is used to connect in Python to Oracle SID.
import jpype
import jaydebeapi
jHome = jpype.getDefaultJVMPath()
jpype.startJVM(jHome, '-Djava.class.path=/path/to/ojdbc6.jar')
conn = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:user/password@DB_HOST_IP:1521:DB_NAME')
How can we connect to Oracle Service Name?
回答1:
Regarding your connection string, you could use TNS
syntax (read on, here),
as opposed to host:port:sid
syntax you're using now.
In that case you would describe SERVICE_NAME
inside CONNECT_DATA
, as follows:
jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','[MYUSER]/[MYPASS]@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=[MYHOST])(PORT=1521))(CONNECT_DATA=(SERVER=dedicated) (SERVICE_NAME=[MYSERVICENAME])))')
By the way - you could also use cx_Oracle to connect to oracle - no java
hassle. (just a suggestion)
回答2:
This way should work
conn = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:user/password@//DB_HOST_IP:1521/DB_NAME')
来源:https://stackoverflow.com/questions/39568378/python-connection-with-jdbc-to-oracle-service-name-jaydebeapi