Python安装jpype调用java,安装jaydebeapi通过jdbc连接数据库

pip install JPype1或下载JPype1-0.7.0.tar.gz包

经常出现需要安装VC++服务等

 1 import jpype,os,time,timer
 2 from jpype import java
 3 from jpype import javax
 4 
 5 HOST='192.168.48.103'
 6 PORT=9999
 7 USER=''
 8 PASS=''
 9 
10 URL = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi" % (HOST, PORT)
11 print('1',URL)
12 jvmPath = jpype.getDefaultJVMPath()
13 print(jvmPath)
14 #this it the path of your libjvm /usr/lib/jvm/sun-jdk-1.6/jre/lib/amd64/server/libjvm.so on linux
15 # jpype.startJVM("C:\Program Files\Java\jre1.8.0_191\bin\server\jvm.dll")
16 if not jpype.isJVMStarted():
17     jpype.startJVM(jvmPath)
18 jpype.java.lang.System.out.println('Hello world!')
19 # print(jpype.startJVM("C:\Program Files\Java\jre1.8.0_191\bin\server\jvm.dll"))
20 jhash  = java.util.HashMap()
21 jarray = jpype.JArray(java.lang.String)([USER,PASS])
22 jhash.put(javax.management.remote.JMXConnector.CREDENTIALS, jarray);
23 jmxurl = javax.management.remote.JMXServiceURL(URL)
24 print('2',jmxurl)
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('3',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('4',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('5',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('6',attribute,attr)
50 
51     object="java.lang:type=OperatingSystem"
52     attribute="Version"
53     attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
54     print('7',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('8',attribute,attr)

安装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()

猜你喜欢

转载自www.cnblogs.com/fameg/p/11204501.html