oracle python:如何使用jdbc连接到oracle数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28009366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python: how to connect to oracle database using jdbc
提问by nskalis
Could you please enlighten me on how to connect to an Oracle instance using Python/Jython ?
你能告诉我如何使用 Python/Jython 连接到 Oracle 实例吗?
After installing fully Jython, the Oracle website (http://www.oracle.com/technetwork/articles/dsl/mastering-oracle-python-providers-1395759.html) suggests to : All you need to provide is to make sure ojdbc6.jar is in the CLASSPATH or JYTHONPATH so that the connection driver could be resolved.
完全安装 Jython 后,Oracle 网站 ( http://www.oracle.com/technetwork/articles/dsl/mastering-oracle-python-providers-1395759.html) 建议: 您需要提供的只是确保 ojdbc6 .jar 位于 CLASSPATH 或 JYTHONPATH 中,以便可以解析连接驱动程序。
I read that when using the -jaroption ignores the CLASSPATH environment variable. So I did like :
我读到使用-jar选项时会忽略 CLASSPATH 环境变量。所以我确实喜欢:
java -classpath /usr/lib/oracle/12.1/client64/lib/ojdbc6.jar -jar jython.jar
from java.sql import DriverManager
db_connection = DriverManager.getConnection("jdbc:oracle:thin:@xxxxx:1521/P1FNTPE", "xxx", "xxx")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
java.sql.SQLException: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@xxxxx:1521/P1FNTPE
Could you please help/advise me on how to resolve this issue ?
你能帮我/建议我如何解决这个问题吗?
采纳答案by Gord Thompson
As mentioned in the question, the full path to the JAR file for the JDBC driver must be present in either the CLASSPATH or the JYTHONPATH environment variable so the Jython script can find it. These variables can be modified in several ways depending on the environment (shell) being used, as described in the Jata tutorial here:
如问题中所述,JDBC 驱动程序的 JAR 文件的完整路径必须存在于 CLASSPATH 或 JYTHONPATH 环境变量中,以便 Jython 脚本可以找到它。这些变量可以根据所使用的环境(shell)以多种方式进行修改,如此处的 Jata 教程中所述:
In this particular case, simply adding the line
在这种特殊情况下,只需添加行
export CLASSPATH=/usr/lib/oracle/12.1/client64/lib/ojdbc6.jar
to one of the startup files (e.g., ~/.bash_profile, ~/.profile, ~/.bashrc, ...) and then logging back in (or running source
on the file) was the solution.
到启动文件之一(例如,~/.bash_profile、~/.profile、~/.bashrc、...)然后重新登录(或source
在文件上运行)是解决方案。