samedi 14 décembre 2019

How to test jdbc connection in python?

I tried using the py4j referred Connecting and testing a JDBC driver from Python

from py4j.JavaGateway import java_gateway

# Open JVM interface with the JDBC Jar
jdbc_jar_path = 'C:\Program Files\CData\CData JDBC Driver for MongoDB 2019\lib\cdata.jdbc.mongodb.jar'
gateway = java_gateway(classpath=jdbc_jar_path) 

# Load the JDBC Jar
jdbc_class = "cdata.jdbc.mongodb.MongoDBDriver"
gateway.jvm.class.forName(jdbc_class)

# Initiate connection
jdbc_uri = "jdbc:mongodb:Server=127.0.0.1;Port=27017;Database=EmployeeDB;"
con =  gateway.jvm.DriverManager.getConnection(jdbc_uri)

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

Getting error as

File "assignment.py", line 9 gateway.jvm.class.forName(jdbc_class) ^ SyntaxError: invalid syntax

Aucun commentaire:

Enregistrer un commentaire