想将一个python项目部署到生产服务器上运行,但是服务器上没有装oracle客户端,无法使用cx_Oracle包,所以想要使用jdbc来连接数据库,遂有了一下测试。
python版本:
C:\Users\Administrator>python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import jaydebeapi
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 'jaydebeapi'python使用jdbc连接数据库,想要用到JayDeBeApi 包,
下载地址:https://pypi.python.org/pypi/JayDeBeApi3
目前的版本是1.3.2,python专用,
如果是python2可以下载JayDeBeApi1,地址:https://pypi.python.org/pypi/JayDeBeApi
从JayDeBeApi1网站上,我们可以大致了解下JayDeBeApi
The JayDeBeApi module allows you to connect from Python code to databases using Java JDBC. It provides a Python DB-API v2.0 to that database.
It works on ordinary Python (cPython) using the JPype Java integration or on Jython to make use of the Java JDBC driver.
In contrast to zxJDBC from the Jython project JayDeBeApi let’s you access a database with Jython AND Python with only minor code modifications. JayDeBeApi’s future goal is to provide a unique and fast interface to different types of JDBC-Drivers through a flexible plug-in mechanism.
JayDeBeApi可以在cpython(也就是通常我们说的python)和Jython中使用
安装:
因为我用的是python3所以下载JayDeBeApi3安装,这里推荐使用源码安装,如果下载whl安装,可能会报错误:
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
下载源码解压,使用命令行进入解压后的目录:
D:\MY_TOOLS\DEV_TOOL\python\JayDeBeApi3-1.3.2>python setup.py install安装成功后,在python控制台中测试import jaydebeapi,不报错即可
使用:
该包的核心就 是connect函数,注意jaydebeapi1和jaydebeapi3的connect,参数形式是不一样的,jaydebeapi1的话,直接在官网上看示例即可。但是jaydebeapi3的网页上没有任何说明和示例,所以需要自行查看帮助信息
在控制台中查看其帮助信息:
>>> help(jaydebeapi.connect)
Help on function connect in module jaydebeapi:
connect(jclassname, driver_args, jars=None, libs=None)
Open a connection to a database using a JDBC driver and return
a Connection instance.
jclassname: Full qualified Java class name of the JDBC driver.
driver_args: Argument or sequence of arguments to be passed to the
Java DriverManager.getConnection method. Usually the
database URL. See
http://docs.oracle.com/javase/6/docs/api/java/sql/DriverManager.html
for more details
jars: Jar filename or sequence of filenames for the JDBC driver
libs: Dll/so filenames or sequence of dlls/sos used as shared
library by the JDBC driver
>>>可以看到
第一个参数:是class name,这里连接oracle的话,就是oracle.jdbc.driver.OracleDriver
第二个参数:是应该列表,Java DriverManager.getConnection方法使用到它,帮助信息里面给出了一个网址提供参考,我们打开这个网址:http://docs.oracle.com/javase/6/docs/api/java/sql/DriverManager.html 找到getConnection方法,可以看到,这个方法有3个重载方法,期中第二个最好理解,对应数据库的URL地址,用户名,密码
getConnection
public static Connection getConnection(String url,
String user,
String password)
throws SQLException
Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.
Parameters:
url - a database url of the form jdbc:subprotocol:subname
user - the database user on whose behalf the connection is being made
password - the user's password
Returns:
a connection to the URL
Throws:
SQLException - if a database access error occurs
第三个参数:是指定JDBC驱动所在的jar包,我想如果驱动包在path_classe目录中,应该可以忽略这个参数
第四个参数:没看懂,不过可以忽略,不传这个参数
所以最后建立jdbc连接的写法为:
conn=jaydebeapi.connect('oracle.jdbc.driver.OracleDriver',['jdbc:oracle:thin:@127.0.0.1/orcl','scott','tiger'],'D:\\MY_TOOLS\\ojdbc6.jar')
以下为完整的测试代码:
import jaydebeapi
url='jdbc:oracle:thin:@127.0.0.1/orcl'
user='scott'
password='tiger'
dirver='oracle.jdbc.driver.OracleDriver'
jarFile='D:\\MY_TOOLS\\ojdbc6.jar'
sqlStr='select * from dual'
#conn=jaydebeapi.connect('oracle.jdbc.driver.OracleDriver',['jdbc:oracle:thin:@127.0.0.1/orcl','scott','tiger'],'D:\\MY_TOOLS\\ojdbc6.jar')
conn=jaydebeapi.connect(dirver,[url,user,password],jarFile)
curs = conn.cursor()
curs.execute(sqlStr)
result=curs.fetchall()
print(result)
curs.close()
conn.close()fetchall返回的是一个列表,如果使用pandas的read_sql_query方法,会得到更加美观的结果集,使用方法为:
pd.read_sql_query(sqlStr,conn)
最后还有一个小问题没解决,就是在ide中运行代码是,会弹出一个java.exe的黑窗口,不知道是什么原因