和网上的相比 ,就是 linux使用
http://www.jython.org/Project/download.html
下载 jython 包,(安装!不,就直接解压jar 就可以用了,我 linux 没有窗口!可怜 )
jar -xvf jython_install.jar
使用 root
vim /etc/profile #添加
export JAVA_HOME=/usr/local/jdk1.5.0_17
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:/home/xj_liukaiyi/src/java/lib/ojdbc14-1.0.jar:/home/xj_liukaiyi/src/jython/evn_lib/jython.jar
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
alias jython='java org.python.util.jython '
测试 oracle
#!jython
from oracle.jdbc.driver import OracleDriver
from java.sql import DriverManager
def connect(un='dbuser', pw='pass123', sid='ora10g', host='192.168.102.223', port='1688'):
driver = OracleDriver()
DriverManager.registerDriver(driver)
connection = "jdbc:oracle:thin:@%s:%s:%s" % (host, port, sid)
return DriverManager.getConnection(connection, un, pw)
def doSelect(strSql,func=None,doConn=None):
is_now_conn=None
if not doConn :
is_now_conn=True
doConn=connect()
stmt = doConn.createStatement()
datas=[]
rows=0
try:
try:
rset = stmt.executeQuery(strSql)
ccount=rset.getMetaData().getColumnCount()
data=[]
while (rset.next()):
rows=rows+1
#print rset.getObject(1)
for ii in range(ccount):
data.append(rset.getObject(ii+1))
if func :
func(data,rows)
datas.append(data)
except:
pass
finally:
try:
stmt.close()
finally:
if is_now_conn :
doConn.close()
print "-conn close-"
return datas
def runRow(data,rows):
print "runRow row=%s data=%s " % (rows,data)
if __name__ == "__main__":
#模式1 定义一次 conn close
#可插拔式 定义方法使用 runRow (罗嗦下java 要达到只能写接口 !)
doSelect('select count(*) from MN05_TBL_BIZ ',runRow)
#模式2 外部定义 conn 并,可多次使用 后 conn close
conn=connect()
print doSelect('select count(*) from MN05_TBL_COPYRIGHT ',doConn=conn)
print doSelect('select count(*) from MN05_TBL_BIZ ',doConn=conn)
from java.lang import *
print tts[2]
#梁咏琪 #有换行 这 使用 java.lang.String.trim
#
sql="""
select tb.NAME
from MN05_TBL_BIZ tb , MN05_TBL_COPYRIGHT tc
where
tb.copyright_id=tc.COPYRIGHT_ID and
tb.actor = '%s'
""" %( String(tts[2]).trim() )
#中文匹配问题这 使用 decode('utf8') 变成unicod 就可以了
arr=db.doSelect(sql.decode('utf8'),doConn=conn)
for t in arr[0]:
print t.encode('utf-8')
conn.close()
jython 结果
-bash-3.00$ jython db.py
runRow row=1 data=[3487033]
-conn close-
[[1563479]]
[[3487033]]
整理 www.blogjava.net/Good-Game