zoukankan      html  css  js  c++  java
  • 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 FilesJavajre1.8.0_191inserverjvm.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 FilesJavajre1.8.0_191inserverjvm.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 FilesJavajre1.8.0_211inserverjvm.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)Pythonojdbc6.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()
  • 相关阅读:
    【弹道】Cocos 跟踪导弹
    腾讯云短信测试 (云短信、短信验证码)
    【组件】战斗力滚动数字
    Navicat访问远程服务器数据库
    解决Python读取SQL Server中文乱码问题
    【前端】GIS及JS题目
    【Postgres】空间计算
    PYTHONPATH在vscode和pycahrm的区别?为什么有的代码在vscode导入报错,pycahrm正常?精通PYTHONPATH作用真的很重要
    python 快速万能同步转异步语法
    fastjson序列化显示出null值的键值对
  • 原文地址:https://www.cnblogs.com/fameg/p/11204501.html
Copyright © 2011-2022 走看看