从Python连接和测试JDBC驱动程序 [英] Connecting and testing a JDBC driver from Python

查看:982
本文介绍了从Python连接和测试JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python在我们的JDBC驱动程序上进行一些测试.

I'm trying to do some testing on our JDBC driver using Python.

最初弄清楚JPype,我最终设法连接了驱动程序并执行了这样的选择查询(再现了通用代码段):

Initially figuring out JPype, I eventually managed to connect the driver and execute select queries like so (reproducing a generalized snippet):

from __future__ import print_function
from jpype import *

#Start JVM, attach the driver jar
jvmpath = 'path/to/libjvm.so'
classpath = 'path/to/JDBC_Driver.jar'
startJVM(jvmpath, '-ea', '-Djava.class.path=' + classpath)

# Magic line 1
driver = JPackage('sql').Our_Driver

# Initiating a connection via DriverManager()
jdbc_uri = 'jdbc:our_database://localhost:port/database','user', 'passwd')  
conn = java.sql.DriverManager.getConnection(jdbc_uri)

# Executing a statement
stmt = conn.createStatement()
rs = stmt.executeQuery ('select top 10 * from some_table')

# Extracting results
while rs.next():
    ''' Magic #2 - rs.getStuff() only works inside a while loop '''
    print (rs.getString('col_name'))

但是,我未能批处理插入件,而这正是我要测试的.即使executeBatch()返回的jpype int []应该表明插入成功,该表也不会被更新.

However, I've failed to to batch inserts, which is what I wanted to test. Even when executeBatch() returned a jpype int[], which should indicate a successful insert, the table was not updated.

然后我决定尝试py4j.

I then decided to try out py4j.

我的困境-我很难弄清楚如何做与上述相同的事情.据说py4j不会自行启动JVM,并且Java代码需要与GatewayServer()进行预先安排,因此我不确定它是否可行.

My plight - I'm having a hard time figuring out how to do the same thing as above. It is said py4j does not start a JVM on its own, and that the Java code needs to be prearranged with a GatewayServer(), so I'm not sure it's even feasible.

另一方面,有一个名为 py4jdbc 的库就可以做到这一点.

On the other hand, there's a library named py4jdbc that does just that.

我修改了dbapi.py代码,但对流程却不太了解,并且非常困惑.

I tinkered through the dbapi.py code but didn't quite understand the flow, and am pretty much jammed.

如果任何人都知道如何使用py4j从.jar文件中加载JDBC驱动程序,并且可以指出正确的方向,我将不胜感激.

If anyone understands how to load a JDBC driver from a .jar file with py4j and can point me in the right direction, I'd be much grateful.

推荐答案

在py4j中,使用您各自的JDBC uri:

In py4j, with your respective JDBC uri:

from py4j.JavaGateway import java_gateway

# Open JVM interface with the JDBC Jar
jdbc_jar_path = '/path/to/jdbc_driver.jar'
gateway = java_gateway(classpath=jdbc_jar_path) 

# Load the JDBC Jar
jdbc_class = "com.vendor.VendorJDBC"
gateway.jvm.class.forName(jdbc_class)

# Initiate connection
jdbc_uri = "jdbc://vendor:192.168.x.y:zzzz;..."
con =  gateway.jvm.DriverManager.getConnection(jdbc_uri)

# Run a query
sql = "select this from that"
stmt = con.createStatement(sql)
rs = stmt.executeQuery()
while rs.next():
    rs.getInt(1)
    rs.getFloat(2)
    .
    .
rs.close()
stmt.close()

这篇关于从Python连接和测试JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆