Connecting and testing a JDBC driver from Python


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

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/'
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
    ''' Magic #2 - rs.getStuff() only works inside a while loop '''
    print (rs.getString('col_name'))

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.

I then decided to try out py4j.

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.

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

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

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.

Asked By: Jay



add a commit after adding the records and before retrieving.

Answered By: brddawg

In py4j, with your respective JDBC uri:

from py4j.java_gateway import JavaGateway

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

# Load the JDBC Jar
jdbc_class = "com.vendor.VendorJDBC"

# 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()
Answered By: Jay

I have met a similar problem in airflow, I used teradata jdbc jars and jaydebeapi to connect teradata database and execute sql:

[root@myhost transfer]# cat 
import jaydebeapi
from contextlib import closing

jdbc_driver_loc = '/opt/spark-2.3.1/jars/terajdbc4-,/opt/spark-2.3.1/jars/tdgssconfig-'
jdbc_driver_name = 'com.teradata.jdbc.TeraDriver'

url='jdbc:teradata://' + host + '/TMODE=TERA'

sql = "SELECT COUNT(*) FROM  A_TERADATA_TABLE_NAME where month_key='202009'"

conn = jaydebeapi.connect(jclassname=jdbc_driver_name,
                                  driver_args=[login, psw],

with closing(conn) as conn:
    with closing(conn.cursor()) as cur:
[root@myhost transfer]# python
[root@myhost transfer]# 
Answered By: kain
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.