python如何使用jt400.jar包代码实例


Posted in Python onDecember 20, 2019

这篇文章主要介绍了python如何使用jt400.jar包代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

jt400helper.py

#coding=utf-8
import jpype
import os



class JT400Helper(object):
  def __init__(self, server,username,pwd):
    jvmpath=r"C:\Program Files\Java\jre1.8.0_60\bin\server\jvm.dll"
    jarpath=r'd:\temp\jt400.jar'
    jvmArg = "-Djava.class.path=%s" % jarpath
    jpype.startJVM(jvmpath,jvmArg)
    
    #jt400
    AS400=jpype.JClass('com.ibm.as400.access.AS400')
    CommandCall=jpype.JClass('com.ibm.as400.access.CommandCall')
    ProgramCall=jpype.JClass('com.ibm.as400.access.ProgramCall')
    SpooledFileList=jpype.JClass('com.ibm.as400.access.SpooledFileList')
    AS400Text=jpype.JClass('com.ibm.as400.access.AS400Text')
    AS400JDBCDriver=jpype.JClass('com.ibm.as400.access.AS400JDBCDriver')
    
    
    
    #java
    Connection=jpype.JClass('java.sql.Connection')
    DatabaseMetaData=jpype.JClass('java.sql.DatabaseMetaData')
    Statement=jpype.JClass('java.sql.Statement')
    self.ResultSet=jpype.JClass('java.sql.ResultSet')
    
    self.server,self.username,self.pwd = server,username,pwd
    self.RJobLog=jpype.JClass('com.ibm.as400.resource.RJobLog')
    self.AS400FTP=jpype.JClass('com.ibm.as400.access.AS400FTP')
    self.IFSFile=jpype.JClass('com.ibm.as400.access.IFSFile')
    self.ObjectDescription=jpype.JClass('com.ibm.as400.access.ObjectDescription')
    
    self.ProgramParameter=jpype.JClass('com.ibm.as400.access.ProgramParameter')
    self.DriverManager=jpype.JClass('java.sql.DriverManager')
    self.DriverManager.registerDriver(AS400JDBCDriver())
    
    
    self.system=AS400(server,username,pwd)
    self.cc=CommandCall(self.system)
    self.outqueue = SpooledFileList(self.system)
    self.pc = ProgramCall(self.system)
  
  def ShowIfs(self,ifspath):
    ifs=self.IFSFile(self.system,ifspath)
    print 'exists : ',ifs.exists()
    print 'isDirectory : ',ifs.isDirectory()
    print 'isFile : ',ifs.isFile()
    fs=ifs.listFiles()
    if len(fs)>0:
      for f in fs:
        print f.name
  
  def ShowObj2(self,lib,obj):
    od = self.ObjectDescription(system,lib,obj,"FILE")
    print od.CHANGE_DATE_AND_TIME
  
  def ShowObj(self,lib,obj,tmplib):
    cmdstr = "DSPOBJD OBJ(%s/%s) OBJTYPE(*PGM) OUTPUT(*OUTFILE) OUTFILE(%s/OBJD)" % (lib,obj,tmplib)
    msg = self.GetCmdMsg(cmdstr)
    if msg == "":
      cmdstr = "SELECT ODLBNM, ODOBNM, ODOBTP, ODOBAT,ODOBSZ , ODOBTX, ODLDAT, ODLTIM, ODOBSY, ODCRTU, ODJRST, ODJRNM, ODJRLB, ODJRIM, ODJREN,ODJRCN, ODJRDT, ODJRTI FROM %s.objd" % tmplib
      self.GetSQLResult(cmdstr)    
  
  def SaveFieldDef(self,flib,file,tmplib,ofile):
    cmdstr = "DSPFFD FILE(%s/%s) OUTPUT(*OUTFILE) OUTFILE(%s/FIELDDEF)" % (flib,file,tmplib)
    msg = self.GetCmdMsg(cmdstr)
    if msg == "":
      cmdstr = "SELECT WHFLDI, WHFTXT, WHFLDT, WHFLDD, WHFLDP,WHFLDB, WHFOBO FROM %s.FIELDDEF WHERE WHFLDI <> ' ' ORDER BY WHFOBO" % tmplib
      self.GetSQLResult(cmdstr,ofile)
  
  def SaveJobLog(self,jobstr,ofile):
    msg = self.GetJobLog(jobstr)
    with open(ofile,'w') as f:
      f.write(msg)
  
  def GetJobLog(self,jobstr):
    jobarr = jobstr.split('/')
    jobnum,jobuser,jobname=jobarr[0],jobarr[1],jobarr[2]
    return self.GetJobLogByNum(jobnum,jobuser,jobname)
    
  def GetJobLogByNum(self,jobnum,jobuser,jobname):
    joblog=self.RJobLog(self.system,jobname,jobuser,jobnum)
    joblog.open()
    joblog.waitForComplete()
    num=joblog.getListLength()
    msgs = []
    for i in range(0,num):
      qmsg=joblog.resourceAt(i)
      msgs.append(qmsg.getAttributeValue("MESSAGE_TEXT"))
    msg = '\n'.join(msgs)
    return msg
  
  
  def GetSQLResult(self,cmdstr,ofile=""):
    connection = self.DriverManager.getConnection("jdbc:as400://" + self.server, self.username, self.pwd)
    dmd = connection.getMetaData()
    select = connection.createStatement(self.ResultSet.TYPE_SCROLL_SENSITIVE,self.ResultSet.CONCUR_UPDATABLE)
    print cmdstr
    rs = select.executeQuery(cmdstr)
    strs = ""
    cols = rs.getMetaData().getColumnCount()
    while (rs.next()):
      rowstrs = []
      for i in range(1,cols+1):
        value = rs.getString(i)
        if ' ' in value:
          value = '"%s"' % value
        rowstrs.append(rs.getString(i))
      strs+=','.join(rowstrs)+'\n'
    connection.close();
    if ofile <> "":
      with open(ofile,'w') as f:
        f.write(strs)
    else:
      print strs
    
   
  def CallProgram(self,plib,pgm,paras):
    #WRKACTJOB SBS(QUSRWRK) JOB(QZRCSRVS) - dump not work
    #paras=["Y","12345","   "]  
    pgmparas=[]
    for para in paras:
      pgmparas.append(ProgramParameter(para))
      print str(pgmparas.getInputData())
    self.pc.setProgram("/QSYS.LIB/%s.LIB/%s.PGM" % (plib,pgm),pgmparas)
    successfully = self.pc.run()
    msg = ''
    job = pc.getJob()
    print pc.toString()
    print job.toString()
    if successfully <> True:
      ml=self.pc.getMessageList()
      for m in ml:
        msg += m.getText() +'\n'
      print msg
      self.system.disconnectAllServices()
    return msg
    
  def GetCmdMsg(self,cmdstr):
    msg = ''
    print cmdstr
    successfully = self.cc.run(cmdstr)
    if successfully <> True:
      ml=self.cc.getMessageList()
      for m in ml:
        msg += m.getText() +'\n'
      print msg
    return msg
  
  def CheckObjExists(self,lib,file,type="*FILE"):
    str = "CHKOBJ OBJ(%s/%s) OBJTYPE(%s)" % (lib,file,type)
    return self.GetCmdMsg(str)

  def DeleteObj(self,lib,file):  
    str = "DLTF FILE(%s/%s)" % (lib,file)
    return self.GetCmdMsg(str)
    
    
  def FileToIfs(self,lib,file,mem,ifspath):
    # don't use RMVBLANK(*TRAILING) ,will cause numeric field contains blank characters error when put ifs to file
    str = "CPYTOIMPF FROMFILE(%s/%s %s) TOSTMF('%s') MBROPT(*REPLACE) STMFCCSID(*STMF) RCDDLM(*CRLF) DTAFMT(*DLM) STRDLM(*NONE)" % (lib,file,mem,ifspath)
    print lib,file,mem,"->",ifspath
    return self.GetCmdMsg(str)
    
  def IfsToFile(self,ifspath,lib,file,mem):
    str = "CPYFRMIMPF FROMSTMF('%s') TOFILE(%s/%s %s) MBROPT(*REPLACE) RCDDLM(*CRLF) STRDLM(*NONE) FLDDLM(',') ERRRCDOPT(*REPLACE) RPLNULLVAL(*FLDDFT)" % (ifspath,lib,file,mem)
    print ifspath,"->",lib,file,mem
    return self.GetCmdMsg(str)
    
  def FileToPc(self,lib,file,mem,ofile):
    tmpifs = "QDLS/TEMP/%s.CSV" % mem
    self.FileToIfs(lib,file,mem,tmpifs)
    self.FtpGetIfsFile(tmpifs,ofile)
  
  def PcToFile(self,ifile,lib,file,mem):
    tmpifs = "QDLS/TEMP/%s.CSV" % mem
    self.FtpPutIfsFile(ifile,tmpifs)
    self.IfsToFile(tmpifs,lib,file,mem)
    

  def FtpGetIfsFile(self,ifspath,ofile):
    ftp=self.AS400FTP(self.system)
    successfully = ftp.get(ifspath,ofile)
    print ifspath,"->",ofile
    return successfully
    
  def FtpGetText(self,lib,srcf,mem,dest=r"d:\temp"):
    ftp=self.AS400FTP(self.system)
    target="/QSYS.LIB/%s.LIB/%s.FILE/%s.MBR" % (lib,srcf,mem)
    if os.path.isdir(dest):
      dest=os.path.join(dest,mem+".txt")
    print target,"->",dest
    successfully = ftp.get(target,dest)
    return successfully
    
  def FtpGetSavf(self,lib,savf,dest=r"d:\temp"):
    ftp=self.AS400FTP(self.system)
    target="/QSYS.LIB/%s.LIB/%s.SAVF" % (lib,savf)
    # QUOTE SITE NAMEFMT 0 QGPL/QCLSRC.TEST
    # QUOTE SITE NAMEFMT 1 /QSYS.lib/Libname.lib/Fname.file/Mname.mbr
    ftp.issueCommand("quote site namefmt 1")
    if os.path.isdir(dest):
      dest=os.path.join(dest,savf+".SAVF")
    # can not get it if not set data transfer type
    ftp.setDataTransferType(1)
    successfully = ftp.get(target,dest)
    return successfully
   
  
  def FtpPutIfsFile(self,ifile,ifspath):
    ftp=self.AS400FTP(self.system)
    successfully = ftp.put(ifile,ifspath)
    print ifile,"->",ifspath
    return successfully
       
  def FtpPutText(self,ifile,lib,srcf,mem):
    ftp=self.AS400FTP(self.system)
    dest="/QSYS.LIB/%s.LIB/%s.FILE/%s.MBR" % (lib,srcf,mem)
    successfully = ftp.put(ifile,dest)
    print ifile,"->",lib,srcf,mem
    return successfully
  
  def GetOutQList(self,outqlib,outq):
    print "OutQueue : %s/%s" % (outqlib,outq)
    self.outqueue.setQueueFilter("/QSYS.LIB/%s.LIB/%s.OUTQ" % (outqlib,outq))
    self.outqueue.setUserFilter("*ALL")
    self.outqueue.openSynchronously()
    enums=self.outqueue.getObjects()
    i = 1
    info = ""
    while (enums.hasMoreElements()):
      splf=enums.nextElement()
      if(splf<>None):
        strs=[]
        strs.append("System :%s , File : %s , File Number : %s , Progarm :%s , Date : %s" % (splf.getStringAttribute(271),splf.getStringAttribute(104),splf.getIntegerAttribute(105).toString(),splf.getStringAttribute(272),splf.getStringAttribute(34)))
        strs.append("Number/User/Job : %s/%s/%s" % (splf.getStringAttribute(60),splf.getStringAttribute(62),splf.getStringAttribute(59)))
        strs.append("File Pages: %s , Print quality : %s , Printer device type : %s , Page size length : %d " % (splf.getIntegerAttribute(111).toString(),splf.getStringAttribute(48),splf.getStringAttribute(90),splf.getFloatAttribute(78).intValue()))
        info += '\n'.join(strs)+'\n'
        i+=1
    self.system.disconnectAllServices()
    print "total spool files : ",i
    print info
  
  def GetSpoolFile(self,sflib,sffile,sfname,sfjobnum,fnum,dest):
    if self.CheckObjExists(sflib,sffile)<>"":
      # IGCDTA parameter is for DBCS file
      self.GetCmdMsg("CRTPF FILE(%s/%s) RCDLEN(160) IGCDTA(*YES)" % (sflib,sffile))
    str = "CPYSPLF FILE(%s) TOFILE(%s/%s) JOB(%s) SPLNBR(%d)" % (sfname,sflib,sffile,sfjobnum,fnum)
    msg = self.GetCmdMsg(str)
    if msg == "":
      self.FtpGetText(sflib,sffile,sffile,dest)
      print "download successfully!"
    def PutMsg(self,qlib,qname,msg):    qstr = "/QSYS.LIB/%s.LIB/%s.DTAQ" % (qlib,qname)    dataqueue=self.DataQueue(self.system,qstr)    dataqueue.write(msg)      def GetMsg(self,qlib,qname):    qstr = "/QSYS.LIB/%s.LIB/%s.DTAQ" % (qlib,qname)    dataqueue=self.DataQueue(self.system,qstr)    dqdata = dataqueue.read()    dqdatastr = 'No data'    if dqdata <> None:      dqdatastr = dqdata.getString()    print dqdatastr    return dqdatastr
    
if __name__ == '__main__':
  pass

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现全局变量的两个解决方法
Jul 03 Python
python elasticsearch环境搭建详解
Sep 02 Python
python mqtt 客户端的实现代码实例
Sep 25 Python
Python实现图像的垂直投影示例
Jan 17 Python
flask 框架操作MySQL数据库简单示例
Feb 02 Python
深入浅析python变量加逗号,的含义
Feb 22 Python
解决Jupyter无法导入已安装的 module问题
Apr 17 Python
Python matplotlib实时画图案例
Apr 23 Python
tensorflow之读取jpg图像长和宽实例
Jun 18 Python
Python如何读取、写入JSON数据
Jul 28 Python
python实现图像高斯金字塔的示例代码
Dec 11 Python
matplotlib grid()设置网格线外观的实现
Feb 22 Python
基于python使用tibco ems代码实例
Dec 20 #Python
使用python实现数组、链表、队列、栈的方法
Dec 20 #Python
python隐藏类中属性的3种实现方法
Dec 19 #Python
Python合并2个字典成1个新字典的方法(9种)
Dec 19 #Python
关于Python中定制类的比较运算实例
Dec 19 #Python
在python中利用try..except来代替if..else的用法
Dec 19 #Python
Python字符串的修改方法实例
Dec 19 #Python
You might like
php高级编程-函数-郑阿奇
2011/07/04 PHP
PHP守护进程化在C和PHP环境下的实现
2017/11/21 PHP
Mootools 1.2教程 定时器和哈希简介
2009/09/15 Javascript
JavaScript中使用构造函数实现继承的代码
2010/08/12 Javascript
js对象转json数组的简单实现案例
2014/02/28 Javascript
从JQuery源码分析JavaScript函数的apply方法与call方法
2014/09/25 Javascript
JavaScript 闭包机制详解及实例代码
2016/10/10 Javascript
微信小程序左滑删除效果的实现代码
2017/02/20 Javascript
Vue 2.0的数据依赖实现原理代码简析
2017/07/10 Javascript
JavaScript代码判断输入的字符串是否含有特殊字符和表情代码实例
2017/08/17 Javascript
JavaScript实现快速排序的方法分析
2018/01/10 Javascript
从vue源码看props的用法
2019/01/09 Javascript
详解如何在Vue项目中导出Excel
2019/04/19 Javascript
vue-cli脚手架打包静态资源请求出错的原因与解决
2019/06/06 Javascript
[00:43]FTP典藏礼包 DOTA2三大英雄霸气新套装
2014/03/21 DOTA
[54:45]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 Optic vs OG
2018/04/02 DOTA
[01:29:31]VP VS VG Supermajor小组赛胜者组第二轮 BO3第一场 6.2
2018/06/03 DOTA
Python中如何获取类属性的列表
2016/12/26 Python
windows下安装Python和pip终极图文教程
2017/03/05 Python
pandas对指定列进行填充的方法
2018/04/11 Python
Python求两个圆的交点坐标或三个圆的交点坐标方法
2018/11/07 Python
Python使用folium excel绘制point
2019/01/03 Python
Python中的十大图像处理工具(小结)
2019/06/10 Python
对Python中class和instance以及self的用法详解
2019/06/26 Python
使用python进行广告点击率的预测的实现
2019/07/04 Python
python使用tomorrow实现多线程的例子
2019/07/20 Python
python并发编程多进程 模拟抢票实现过程
2019/08/20 Python
Python爬取知乎图片代码实现解析
2019/09/17 Python
tensorflow安装成功import tensorflow 出现问题
2020/04/16 Python
如何利用python生成MD5并去重
2020/12/07 Python
Perfume’s Club澳大利亚官网:西班牙领先的在线美容店
2021/02/01 全球购物
Tomcat中怎么使用log4j输出所有的log
2016/07/07 面试题
请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值
2014/09/15 面试题
旷课检讨书1000字
2014/02/14 职场文书
企业财务管理制度范本
2015/08/04 职场文书
深入详解JS函数的柯里化
2021/06/09 Javascript