学习如何用 Jython 编写 DB2 JDBC 工具
2009-12-08 00:00:00 来源:WEB开发网从命令控制台“jython select1.jy”运行 select1.jy。
图 1. “jython select1.jy”的输出
以下是 Jython 代码。
清单 1. select1.jyfrom java.lang import *
from java.sql import *
# load DB2 JDBC type 2 driver (app driver)
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance()
con = DriverManager.getConnection( 'jdbc:db2:sample','vyang','jythonrocks')
stmt = con.createStatement()
sql='select firstname,lastname,salary from employee where salary > 20000 order by salary'
rs = stmt.executeQuery(sql)
employeeList=[]
while (rs.next()):
row={}
row['firstname']=rs.getString(1)
row['lastname']=rs.getString(2)
row['salary']=rs.getDouble(3)
employeeList.append(row)
rs.close()
stmt.close()
con.close()
print 'employee salary over $20,000'
print '============================'
print 'firstname lastname salary'
print '============================'
# print the result
for e in employeeList:
print e['firstname'],e['lastname'],'$'+ str(e['salary'])
更多精彩
赞助商链接