python通过jdbc连接数据库
python要访问sybase或oracle数据库,尝试通过odbc以及pyodbc等方式去访问数据库,一直没有成功,通过JayDeBeApi模块调用 java jdbc 成功访问数据库。
1、安装 jaydebeapi
pip install JayDeBeApi
2、连接Sybase数据库
import jaydebeapi import pandas as pd url='jdbc:jtds:sybase://127.0.0.1:5000/db;loginTimeout=1;socketTimeout=15000;charset=GB2312' user='sa' password='password' dirver='net.sourceforge.jtds.jdbc.Driver' jarFile='jtds-1.2.5.jar' sqlStr='select * from v_bill' conn=jaydebeapi.connect(dirver,url,[user,password],jarFile) curs = conn.cursor() data=pd.read_sql_query(sqlStr,conn) print(data) curs.close() conn.close()
调用存储过程的方式:
import jaydebeapi import pandas as pd url='jdbc:jtds:sybase://127.0.0.1:5000/db;loginTimeout=1;socketTimeout=15000;charset=GB2312' user='sa' password='password' dirver='net.sourceforge.jtds.jdbc.Driver' jarFile='jtds-1.2.5.jar' conn=jaydebeapi.connect(dirver,url,[user,password],jarFile) curs = conn.cursor() curs.execute('db..proc_Test') result=curs.fetchall() print(result) curs.close() conn.close()
3、连接Oracle数据库
import jaydebeapi url='jdbc:oracle:thin:@192.168.1.63:1521:orcl' user='dbuser' password='password' dirver='oracle.jdbc.OracleDriver' jarFile='ojdbc6.jar' sqlStr='select * from dual' conn=jaydebeapi.connect(dirver,url,[user,password],jarFile) curs = conn.cursor() curs.execute(sqlStr) result=curs.fetchall() print(result) curs.close() conn.close()