python如何使用jt400.jar包代码实例
作者:白马酒凉 发布时间:2021-05-07 03:50:46
标签:python,jt400,jar,包
这篇文章主要介绍了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
来源:https://www.cnblogs.com/sui84/p/8448462.html
0
投稿
猜你喜欢
- python的版本经过了python2.x和python3.x等版本,无论哪种版本,关于python爬虫相关的知识是融会贯通的,脚本之家关于
- 目录准备数据集导入所需的软件包将数据从文件加载到Python变量拆分数据进行训练和测试标记化并准备词汇预处理输出标签/类建立Keras模型并
- 详解Python中的三元运算什么是三元运算符在大部分编程语言中都有三目运算,也称三元运算,Python语言从Python2.5版本开始也引入
- 为什么训练误差比测试误差高很多?一个Keras的模型有两个模式:训练模式和测试模式。一些正则机制,如Dropout,L1/L2正则项在测试模
- 晚上帮同学用Python脚本绘图,大概需求是读取一个txt文件的两列分别作为x和y的值,绘图即可,代码如下:#coding:utf-8imp
- 简单类型内置到 Python 编程语言中的简单数据类型包括: bool
- 官方文档中关于super的定义说的不是很多,大致意思是返回一个代理对象让你能够调用一些继承过来的方法,查找的机制遵循mro规则,最常用的情况
- 引言今天在使用Pytorch导入此前保存的模型进行测试,在过程中发现输出的结果与验证结果差距甚大,经过排查后发现是forward与eval(
- 你可以输入'脚本'查查看 查找页面中的字符 <script language="JavaScript&quo
- 前言我们上一篇博客,给大家展现了一个动态的爱心。今天,我们给大家画一个圣诞树,我们一起来看看效果吧。效果展示我们先来看看最终的效果看看我们画
- 1.cookie的作用cookie 是指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据,就像有些网站上的一些数
- 概览最近开始在学习mysql相关知识,自己根据学到的知识点,根据自己的理解整理分享出来,本篇文章会分析下一个sql语句在mysql中的执行流
- 数据修改主要以增删改差为主,这里比较几种写法在数据处理时间上的巨大差别。数据量大概是500万行级别的数据,文件大小为100M。1.iloci
- 前言SQLSERVER 2005中不知因何去掉了很重要的DEBUGGER功能,要调试,必须要安装VS2005专业版或者更高版本。非常不方便。
- 这是针对oracle编写的,可以用在不同的电脑上,因为它会自动读取当前电脑的环境变量,从而取得oracle的安装路径,唯一有一点不足的地方是
- 以https://books.toscrape.com/网站为例:打开网页先把网页打开,然后右键检查,找到网络一栏,这个时候发现下面是空白,
- 建议用pycharm,使用比较方便,并且可以直接编辑html文件import timelocatime = time.strftime(&q
- Pycharm中SQL语句提示SQL Dialect is Not Configured使用Pycharm进行数据库查询时,SQL语句经常提
- 这里我们在SQL Server 2005\SQL Server 2008版本上通过举例子,说明临时表和表变量两者的一些特征,让我们对临时表和
- 最终运行效果:OCR(optical character recognition)文字识别是指电子设备(例如扫描仪或数码相机)检查纸上打印的