]
>
sample Jhyton program
<.. jython-sql.py ..> #!/usr/bin/env jython
"""
Sample jython program to test jdbc api
Class: CSE 670
Author: Rick Holbert
Date: 12 Nov 2004
"""
from java.lang import *
from java.sql import *
from sys import *
def get_connection(user, passwd):
server = ’database’
port = ’4101’
database = ’c670ab’
url = ’jdbc:sybase:Tds:’ + server + ’:’ + port + ’/’ + database
Class.forName(’com.sybase.jdbc2.jdbc.SybDriver’).newInstance()
try:
conn = DriverManager.getConnection(url, user, passwd)
except:
print("connection error")
exit(1)
return conn
# main part of program starts here
if len(argv) == 3:
username = argv[1]
password = argv[2]
conn = get_connection(username, password)
else:
print "Usage %s username password\n" % argv[0]
exit(0)
stmt = conn.createStatement()
# stmt.executeUpdate( "use c670ab" )
stmt.executeUpdate( "CREATE TABLE DBupdate (Name CHAR(10))" )
stmt.executeUpdate( "INSERT INTO DBupdate VALUES ( ’my name’)" )
stmt.executeUpdate( "INSERT INTO DBupdate VALUES ( ’your name’)" )
result = stmt.executeQuery( "SELECT * FROM DBupdate" )
while result.next():
print ’Name:’, result.getString(1)
stmt.executeUpdate( "DROP TABLE DBupdate" )
result.close()
stmt.close()
conn.close()
-_-_-
To test the Jython program you’ll need the following.
Subscribe to JDK and Jython: type ‘subscribe’ from your Unix command prompt, select the appropriate numbers,
then q.
Download the program and make it executable by typing ‘chmod 755 jython-sql.py’ from the command
prompt.
Run the program by typing
setenv CLASSPATH /usr/local/jConnect-5_2/classes/jconn2.jar:/usr/local/jConnect-5_2/classes:. ; jython-sql.py username password
where username is your username, and password is your password.
Notes:
Jython programs are automatically compiled as needed, so you don’t have to tell java to compile it for
you.
On cshrc shells, you can add to ‘.cshrc ’ the record ’setenv CLASSPATH
/usr/local/jConnect-5_2/classes/jconn2.jar:/usr/local/jConnect-5_2/classes:. ’
On bash shells, you can add to ’.bash_profile ’ the record ’export CLASSPATH
/usr/local/jConnect-5_2/classes/jconn2.jar:/usr/local/jConnect-5_2/classes:.