python使用jdbc连接oracle数据库

想将一个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 "<stdin>", line 1, in <module>
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

安装的过程中,可能会报缺少py4j包的错误:

error: Could not find suitable distribution for Requirement.parse('py4j')

这时候要先安装py4j,我上传了一个安装包,有需要可以直接下载:

https://download.csdn.net/download/cakecc2008/10612010

安装方法与JayDeBeApi一样,也是源码安装,安装好py4j后,再来安装JayDeBeApi就可以了。

安装成功后,在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的黑窗口,不知道是什么原因

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值