Python安装jpype调用java,安装jaydebeapi通过jdbc连接数据库
pip install JPype1或下载JPype1-0.7.0.tar.gz包
经常出现需要安装VC++服务等
测试代码如下:
1 # Author: zfh 2 import jpype,os,time,timer 3 from jpype import java 4 from jpype import javax 5 6 HOST='192.168.48.103' 7 PORT=9999 8 USER='' 9 PASS='' 10 11 URL = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi" % (HOST, PORT) 12 print('URL-->',URL) 13 jvmPath = jpype.getDefaultJVMPath() 14 print('jvmPath-->',jvmPath) 15 #this it the path of your libjvm /usr/lib/jvm/sun-jdk-1.6/jre/lib/amd64/server/libjvm.so on linux 16 # jpype.startJVM("C:\Program Files\Java\jre1.8.0_191\bin\server\jvm.dll") 17 if not jpype.isJVMStarted(): 18 jpype.startJVM(jvmPath) 19 jpype.java.lang.System.out.println('Hello world!') 20 # print(jpype.startJVM("C:\Program Files\Java\jre1.8.0_191\bin\server\jvm.dll")) 21 jhash = java.util.HashMap() 22 jarray = jpype.JArray(java.lang.String)([USER,PASS]) 23 jhash.put(javax.management.remote.JMXConnector.CREDENTIALS, jarray); 24 jmxurl = javax.management.remote.JMXServiceURL(URL) 25 jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl,jhash) 26 try: 27 connection = jmxsoc.getMBeanServerConnection(); 28 except Exception as e: 29 print(e) 30 print('connection-->',connection) 31 # 32 # 33 while True: 34 time.sleep(60) 35 object="java.lang:type=Threading" 36 attribute="ThreadCount" 37 attr=connection.getAttribute(javax.management.ObjectName(object),attribute) 38 print('ThreadCount-->',attribute, attr) 39 # 40 #Memory is a special case the answer is a Treemap in a CompositeDataSupport 41 object="java.lang:type=Memory" 42 attribute="HeapMemoryUsage" 43 attr=connection.getAttribute(javax.management.ObjectName(object),attribute) 44 print('HeapMemoryUsage-->',attr.contents.get("used")) 45 #ceshi 46 object="kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec" 47 attribute="Count" 48 attr=connection.getAttribute(javax.management.ObjectName(object),attribute) 49 print('Count-->',attribute,attr) 50 51 object="java.lang:type=OperatingSystem" 52 attribute="Version" 53 attr=connection.getAttribute(javax.management.ObjectName(object),attribute) 54 print('Version-->',attribute,attr) 55 56 object="kafka.server:type=ReplicaManager,name=PartitionCount" 57 attribute="Value" 58 attr=connection.getAttribute(javax.management.ObjectName(object),attribute) 59 print('Value-->',attribute,attr)
运行结果如下:(记录一下jvmPath)
1 URL--> service:jmx:rmi:///jndi/rmi://192.168.48.103:9999/jmxrmi 2 jvmPath--> C:\Program Files\Java\jre1.8.0_211\bin\server\jvm.dll 3 Hello world! 4 connection--> javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@531d72ca 5 ThreadCount--> ThreadCount 60 6 HeapMemoryUsage--> 156563848 7 Count--> Count 0 8 Version--> Version 2.6.32-431.el6.x86_64 9 Value--> Value 24
安装pip install JayDeBeApi 或下载JayDeBeApi-1.1.1.tar.gz(安装JayDeBeApi依赖JPype1)
# Author: zfh import jaydebeapi url = '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()