python 用 jaydebeapi 调 JDBC 连接数据库

 

GitHub:https://github.com/baztian/jaydebeapi

 

1.安装:pip install JayDeBeApi

 

2.下载JDBC的jar包,事实上如果是用的DataGrip,可以直接拷出来使用

 

3.使用 jaydebeapi.connect 连接数据库

参数:

 

连接mysql

import jaydebeapi

driver = 'com.mysql.jdbc.Driver'
url = 'jdbc:mysql://*.*.*.*:3306/api_test_v3?characterEncoding=UTF8&serverTimezone=Asia/Shanghai'
user = 'root'
password = 'Admin123@qwe'
jarFile = r'mysql-connector-java-5.1.47.jar'
conn = jaydebeapi.connect(jclassname=driver, url=url, driver_args=[user, password], jars=jarFile)
curs = conn.cursor()
curs.execute('select * from users')
result = curs.fetchall()
print(result)
curs.close()

连接oracle

import jaydebeapi
url = 'jdbc:oracle:thin:@//*.*.*.*:1521/db1'
driver = 'oracle.jdbc.driver.OracleDriver'
user = '****'
password = '****'
jarFile = r'ojdbc8.jar'
conn = jaydebeapi.connect(jclassname=driver, url=url, driver_args=[user, password], jars=jarFile)
curs = conn.cursor()
curs.execute('select * from *** where ID = -1')
result = curs.fetchall()
print(result)
curs.close()

 

posted @ 2021-04-25 21:15  向前走。  阅读(4749)  评论(1编辑  收藏  举报