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 使用 jt400.jar的更多相关文章

  1. python脚本简化jar操作命令

    本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的.最近在回顾和构思shell脚本工具,后面一些文章应该会分享shell内容,希望大家继续关注. 获取磁 ...

  2. 在python中引用jar包

    在本文中,首先创建一个maven项目,然后打包成jar包,在python中调用jar包,执行java类方法. 1.在eclipse中创建maven项目 我这里创建一个普通java项目,择选择 “mav ...

  3. Python一键转Jar包,Java调用Python新姿势!

    粉丝朋友们,不知道大家看故事看腻了没(要是没腻可一定留言告诉我^_^),今天这篇文章换换口味,正经的来写写技术文.言归正传,咱们开始吧! 本文结构: 需求背景 进击的Python Java和Pytho ...

  4. python call java jar

    python脚本如何调用Java的jar文件呢? HelloWorld.java public class HelloWorld { public static void main(String[] ...

  5. ojdbc14.jar 放在kettle那个目录下面

    \libext\edtftpj-1.4.5.jar \libext\firebirdsql-full.jar \libext\firebirdsql.jar \libext\gis-shape.jar ...

  6. robotframework笔记24

    侦听器接口 机器人框架有一个侦听器接口,可以用于接收 对测试执行通知. 示例用法包括 外部测试监控,测试失败时发送邮件消息, 与其他系统进行通信. 侦听器API版本3也使得 它可以修改测试在测试执行和 ...

  7. DB2 Magazine 中文版: 访问 iSeries 数据

    当您第一次开始学习 DB2 for iSeries 时,一下子要弄清楚如何访问所有数据可能有些令人生畏.我将介绍访问 DB2 for iSeries 数据的一些常见的方法,并展示如何开始开发访问存储在 ...

  8. logstash jdbc 各种数据库配置

    MySQL数据库 Driver ="path/to/jdbc-drivers/mysql-connector-java-5.1.35-bin.jar"   //驱动程序Class ...

  9. Kettle简介

    ETL和Kettle简介     ETL即数据抽取(Extract).转换(Transform).装载(Load)的过程.它是构建数据仓库的重要环节.数据仓库是面向主题的.集成的.稳定的且随时间不断变 ...

随机推荐

  1. 使用VMware安装Mac OS的综合整理教程(你要的这里都有……)

    最近打算使用vmware安装Mac系统,听说安装Mac系统是非常繁琐的,经过几番尝试确实如此,所以写下此教程来更完整地讲解能够成功安装mac的步骤,那么废话不多说. 本次教程整合于多个网上教程,但如果 ...

  2. MySQL Group Replication的安装部署

    一.简介 这次给大家介绍下MySQL官方最新版本5.7.17中GA的新功能 Group Replication . Group Replication是一种可用于实现容错系统的技术.复制组是一组通过消 ...

  3. sql 数据库实时增量更新

    ---一下sql可以添加到作业中每秒执行一次   数据过多会消耗性能 --数据表如下,其中字段pid mid time price_type是一个组合主键--pid mid time price pr ...

  4. NEST routing timeout scroll

    /// <summary> /// PUT /employee/employee/9e5e50da-7740-488e-bee2-b24951435691?routing=test_rou ...

  5. Linux中etc目录详解大全总汇详解

    /etc etc不是什么缩写,是and so on的意思 来源于 法语的 et cetera 翻译成中文就是 等等 的意思. 至于为什么在/etc下面存放配置文件, 按照原始的UNIX的说法(Linu ...

  6. 机器码-字节码-CLR-JIT-托管代码-非托管代码-unsafe-GC-fixed

    0. 机器码 直接由机器码对应平台的CPU执行的指令集, 因此无法在其他指令集的CPU上运行. 无法跨平台. 由本地代码编译得到. (托管代码通过JIT生成) 1. 字节码 即 bytecode 是一 ...

  7. 服务网关ZuulFilter过滤器--pre/post/error的用法(校验请求信息,获取路由后的请求/响应信息,处理服务网关异常)

    微服务中Zuul服务网关一共定义了四种类型的过滤器: pre:在请求被路由(转发)之前调用 route:在路由(请求)转发时被调用 error:服务网关发生异常时被调用 post:在路由(转发)请求后 ...

  8. Oracle 多表插入

    多表插入 作用:一条INSERT语句可以完成向多张表的插入任务(Multitable insert).有两种形式:insert all与insert first,准备测试环境:1.创建表T并初始化测试 ...

  9. 学习python的日常2

    ---恢复内容开始--- 数组list 添加用.append(i,'xxx')方法 删除用.pop(i)方法,其中i为索引位置 数组可以直接作为元素插入数组 元组tuple(初始化后不能改变,所谓不变 ...

  10. Nginx配置文件示例

    Nginx的配置文件示例:(仅供参考) 强烈建议先将默认的配置文件备份再进行操作! 请根据自己项目的实际路径来配置相关路径! uwsgi配置文件请参考:uwsgi配置文件示例 # For more i ...