I'm trying to connect to a DB2 instance running in RDS using Python. I rely on jaydebeapi for that and rely on the doc.
Despite several tries, I still facing the issue in title. My code is below. Any idea what is missing?
import jaydebeapi
# variable set hidden
def connectStr (dbName,userName,userPassword,dbHostName,dbPort):
"""" Create the DB connection string """
connStr = []
credentials = []
url = 'jdbc:db2://%s:%s/%s:securityMechanism=9;encryptionAlgorithm=2;' % (dbHostName,dbPort,dbName)
credentials.append (userName)
credentials.append (userPassword)
connStr.append ('com.ibm.db2.jcc.DB2Driver')
connStr.append (url)
connStr.append (credentials)
return connStr
def connectDB (config):
conn = jaydebeapi.connect(config[0],config[1],config[2])
return conn
connStr = connectStr (dbName,userName,userPassword,dbHostName,dbPort)
conn = connectDB (connStr)
cursor = conn.cursor()
query = """ select count(*) from {} """.format(tableName)
cursor.execute(query)
cursor.close()
I expect the connection to happen and run the query. I checked the security limitation, and confirm I can manage access from a local DBeaver.
It looks like you are missing the jvm in your code. If no CLASSPATH is set, you may want to point to the db2jcc4.jar location. Give a try to the example below:
def connectDB (config):
jar = './Db2JdbcDriver/db2jcc4.jar' # location of the jdbc driver jar
args='-Djava.class.path=%s' % jar
jvm = jpype.getDefaultJVMPath()
jpype.startJVM(jvm, args)
conn = jaydebeapi.connect(config[0],config[1],config[2])
return conn