jaydebeapi 使用Python连接JDBC操作数据库
Mysql
import jaydebeapi
conn=jaydebeapi.connect('com.mysql.jdbc.Driver',['jdbc:mysql://localhost:3306/test','hive','hive'],'/data/users/huser/lan/codes/useJPype/mysql-connector-java-5.1.36/mysql-connector-java-5.1.36-bin.jar')
# 其中mysql的用户名和密码都是hive,最后一个参数是驱动的jar包
curs=conn.cursor()
curs.execute('create table CUSTOMER("ID" INTEGER not null primary key,"NAME" varchar not null)')
curs.execute("insert into CUSTOMER values(1,'John')")
curs.execute("select * from CUSTOMER")
curs.fetchall()
Oracleimport jaydebeapurl = 'jdbc:oracle:thin:@192.168.48.102:1521/orcl'
user = 'scott' password = 'scott' dirver = 'oracle.jdbc.driver.OracleDriver' jarFile = 'D:\Program Files (x86)\Python\ojdbc6.jar' sqlStr = 'select * from emp' # conn=jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@127.0.0.1:1521/orcl',['hwf_model','hwf_model'],'E:/pycharm/lib/ojdbc14.jar') conn = jaydebeapi.connect(dirver, url, [user, password], jarFile) curs = conn.cursor() curs.execute(sqlStr) result = curs.fetchall() for row in result: print(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7]) curs.close() conn.close()
Phoenix
import jaydebeapi
conn = jaydebeapi.connect('org.apache.phoenix.jdbc.PhoenixDriver', \
'jdbc:phoenix:my_zk_server:2181:test', ['', ''], \
'/workspace/service/phoenix-client/phoenix-client.jar')
curs = conn.cursor()
curs.execute('select * from your_table limit 1')
curs.fetchall()