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 使用 jt400.jar的更多相关文章
- python脚本简化jar操作命令
本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的.最近在回顾和构思shell脚本工具,后面一些文章应该会分享shell内容,希望大家继续关注. 获取磁 ...
- 在python中引用jar包
在本文中,首先创建一个maven项目,然后打包成jar包,在python中调用jar包,执行java类方法. 1.在eclipse中创建maven项目 我这里创建一个普通java项目,择选择 “mav ...
- Python一键转Jar包,Java调用Python新姿势!
粉丝朋友们,不知道大家看故事看腻了没(要是没腻可一定留言告诉我^_^),今天这篇文章换换口味,正经的来写写技术文.言归正传,咱们开始吧! 本文结构: 需求背景 进击的Python Java和Pytho ...
- python call java jar
python脚本如何调用Java的jar文件呢? HelloWorld.java public class HelloWorld { public static void main(String[] ...
- ojdbc14.jar 放在kettle那个目录下面
\libext\edtftpj-1.4.5.jar \libext\firebirdsql-full.jar \libext\firebirdsql.jar \libext\gis-shape.jar ...
- robotframework笔记24
侦听器接口 机器人框架有一个侦听器接口,可以用于接收 对测试执行通知. 示例用法包括 外部测试监控,测试失败时发送邮件消息, 与其他系统进行通信. 侦听器API版本3也使得 它可以修改测试在测试执行和 ...
- DB2 Magazine 中文版: 访问 iSeries 数据
当您第一次开始学习 DB2 for iSeries 时,一下子要弄清楚如何访问所有数据可能有些令人生畏.我将介绍访问 DB2 for iSeries 数据的一些常见的方法,并展示如何开始开发访问存储在 ...
- logstash jdbc 各种数据库配置
MySQL数据库 Driver ="path/to/jdbc-drivers/mysql-connector-java-5.1.35-bin.jar" //驱动程序Class ...
- Kettle简介
ETL和Kettle简介 ETL即数据抽取(Extract).转换(Transform).装载(Load)的过程.它是构建数据仓库的重要环节.数据仓库是面向主题的.集成的.稳定的且随时间不断变 ...
随机推荐
- XGBoost对波士顿房价进行预测
import numpy as np import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["SimHe ...
- leetcode 罗马数字和数字的互相转换
不知哪个大佬说过: 关于字符串的题都可以用指针或哈希解决. 罗马数字转数字: 思想: 我们能观察到规律: 一般情况下,表示大的字母在前,小字母在后; 特殊情况下,小字母会在大字母之前,但是相应的,得到 ...
- 作为一个纯粹数据结构的 Redis Streams
来源:antirez 翻译:Kevin (公众号:中间件小哥) Redis 5 中引入了一个名为 Streams 的新的 Redis 数据结构,吸引了社区极大的兴趣.接下来,我会在社区里进行调查,同用 ...
- appium 环境准备
一.环境准备 1.相关依赖环境 当前的环境是Windows 10版本 64位系统(32位的自己想办法吧) 1.jdk1.6.0 (64位) --最好1.6以上版本 2 ...
- CF778D Parquet Re-laying 构造
传送门 如果\(2 \not\mid M\),就把两个图折一下,把\(N\ M\)互换,这样就可以保证\(2 \mid M\). 因为操作可逆,所以我们可以选择一个中间状态,把起始和终点状态都变成这个 ...
- 文件属性的生成操作build action
文件属性的生成操作build action Action 说明 None 资源既不会被集成到程序集内,也不会打包到xap包中.不过我们可以通过设置CopyToOutputDirectory选项让其自动 ...
- LVS简单理解
LVS LVS(Linux Virtual Server)即Linux虚拟服务器 目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案 终端用户 ...
- CSS关于选择器和继承的一些小问题:
选择器的分组 对选择器进行分组,这样在同一组的选择器就可以分享相同的声明.使用逗号将需要分组的选择器分开 h1,h2,h3,h4,h5{ color:green; font-family:" ...
- 浮动和渐变色,定位position,元素的层叠顺序
浮动: float 是我们网页布局的一种 浮动 可以有 left 左浮动 right 右浮动 两种 浮动的特点: 脱离正常的文档流,原本的空间不占据,浮动的标签都具有块级标签的一些特点,可以手动设置宽 ...
- org.springframework.dao.DuplicateKeyException: 问题
转自:https://blog.51cto.com/chengxuyuan/1786938 org.springframework.dao.DuplicateKeyException: a diffe ...