5793
Comment:
|
5754
killed semicolons
|
Deletions are marked like this. | Additions are marked like this. |
Line 12: | Line 12: |
Posted on the Jython-users mailing list by Alfonso Reyes, September 2007 | |
Line 16: | Line 16: |
["RSMD.py"] a database utility I found in IBM. [[BR]] | ["RSMD.py"] is a database utility I found in IBM. [[BR]] |
Line 21: | Line 21: |
{{{ | {{{#!python |
Line 28: | Line 28: |
from java.sql import Connection; from java.sql import DriverManager; from java.sql import ResultSet; from java.sql import SQLException; from java.sql import Statement; |
from java.sql import Connection, DriverManager, ResultSet, SQLException, Statement |
Line 34: | Line 30: |
from java.util import Properties; | from java.util import Properties |
Line 40: | Line 36: |
self.framework = "embedded"; self.driver = "org.apache.derby.jdbc.EmbeddedDriver"; self.protocol = "jdbc:derby:"; self.username = "user1"; self.password = "user1"; |
self.framework = "embedded" self.driver = "org.apache.derby.jdbc.EmbeddedDriver" self.protocol = "jdbc:derby:" self.username = "user1" self.password = "user1" |
Line 48: | Line 44: |
#parseArguments(args); | #parseArguments(args) |
Line 53: | Line 49: |
ds = None; conn = None; props = Properties(); props.put("user", self.username); props.put("password", self.password); |
ds = None conn = None props = Properties() props.put("user", self.username) props.put("password", self.password) |
Line 61: | Line 57: |
javaspec = props.getProperty( "java.specification.name" ); | javaspec = props.getProperty( "java.specification.name" ) |
Line 73: | Line 69: |
Class.forName(self.driver).newInstance(); | Class.forName(self.driver).newInstance() |
Line 79: | Line 75: |
conn = DriverManager.getConnection(dbStr, props); | conn = DriverManager.getConnection(dbStr, props) |
Line 82: | Line 78: |
conn.setAutoCommit(False); | conn.setAutoCommit(False) |
Line 88: | Line 84: |
s = conn.createStatement(); | s = conn.createStatement() |
Line 91: | Line 87: |
s.execute("create table derbyDB(num int, addr varchar(40))"); | s.execute("create table derbyDB(num int, addr varchar(40))") |
Line 93: | Line 89: |
s.execute("insert into derbyDB values (1956,'Webster St.')"); | s.execute("insert into derbyDB values (1956,'Webster St.')") |
Line 95: | Line 91: |
s.execute("insert into derbyDB values (1910,'Union St.')"); | s.execute("insert into derbyDB values (1910,'Union St.')") |
Line 97: | Line 93: |
s.execute("insert into derbyDB values (1,'Wandering Oak')"); | s.execute("insert into derbyDB values (1,'Wandering Oak')") |
Line 101: | Line 97: |
"update derbyDB set num=180, addr='Grand Ave.' where num=1956"); | "update derbyDB set num=180, addr='Grand Ave.' where num=1956") |
Line 105: | Line 101: |
"update derbyDB set num=300, addr='Lakeshore Ave.' where num=180"); | "update derbyDB set num=300, addr='Lakeshore Ave.' where num=180") |
Line 109: | Line 105: |
rs = s.executeQuery("SELECT num, addr FROM derbyDB ORDER BY num"); | rs = s.executeQuery("SELECT num, addr FROM derbyDB ORDER BY num") |
Line 125: | Line 121: |
s.execute("drop table derbyDB"); | s.execute("drop table derbyDB") |
Line 129: | Line 125: |
rs.close(); s.close(); |
rs.close() s.close() |
Line 134: | Line 130: |
conn.commit(); conn.close(); |
conn.commit() conn.close() |
Line 146: | Line 142: |
gotSQLExc = False; | gotSQLExc = False |
Line 151: | Line 147: |
gotSQLExc = True; | gotSQLExc = True |
Line 157: | Line 153: |
print("SimpleApp finished"); | print("SimpleApp finished") |
Line 160: | Line 156: |
SimpleApp().go(); | SimpleApp().go() |
Apache Derby Example(s)
back to OtherExamples
-- Posted on the Jython-users mailing list by Alfonso Reyes, September 2007
This Derby example doesn't need the use of zxJDBC. I was looking to complete the exercises in the book "Python programming with Java libraries" but couldn't find anywhere a site to download InstantDB. Trying Apache Derby was quite a pleasant experience. I am attaching files here.
["RSMD.py"] is a database utility I found in IBM. BR SimpleApp.java the original Java demo
If you follow the instructions in this tutorial everything will go smooth. [http://db.apache.org/derby/papers/DerbyTut/install_software.html Derby Tutorial]. (note: there is also a lot of info on their wiki)
1 """ Derby Class SimpleApp.py
2 Add derby.jar to your CLASSPATH
3 Ported by: Alfonso Reyes, September 2007
4
5 """
6 from RSMD import printRSMD, RSMD # nice methods I downloaded from IBM site
7 from java.sql import Connection, DriverManager, ResultSet, SQLException, Statement
8
9 from java.util import Properties
10 from java.lang import Class
11
12 class SimpleApp:
13 def __init__(self):
14 # the default framework is embedded
15 self.framework = "embedded"
16 self.driver = "org.apache.derby.jdbc.EmbeddedDriver"
17 self.protocol = "jdbc:derby:"
18 self.username = "user1"
19 self.password = "user1"
20
21 def go(self):
22 # parse the arguments to determine which framework is desired
23 #parseArguments(args)
24 """ The driver is installed by loading its class.
25 In an embedded environment, this will start up Derby, since it is not already running.
26
27 """
28 ds = None
29 conn = None
30 props = Properties()
31 props.put("user", self.username)
32 props.put("password", self.password)
33 print props
34
35 # check for J2ME specification - J2ME must use a DataSource further on */
36 javaspec = props.getProperty( "java.specification.name" )
37
38 """
39 The connection specifies create=true in the url to cause
40 the database to be created. To remove the database,
41 remove the directory derbyDB and its contents.
42 The directory derbyDB will be created under
43 the directory that the system property
44 derby.system.home points to, or the current
45 directory if derby.system.home is not set.
46
47 """
48 Class.forName(self.driver).newInstance()
49 print "Loaded the appropriate driver."
50
51 database = "derbyDB5" # put here the name for your database
52 dbStr = self.protocol + database + ";create=true"
53 print dbStr
54 conn = DriverManager.getConnection(dbStr, props)
55 print "Connected to and created database derbyDB"
56
57 conn.setAutoCommit(False)
58
59 """ Creating a statement lets us issue commands against
60 the connection.
61
62 """
63 s = conn.createStatement()
64
65 # We create a table, add a few rows, and update one.
66 s.execute("create table derbyDB(num int, addr varchar(40))")
67 print "Created table derbyDB"
68 s.execute("insert into derbyDB values (1956,'Webster St.')")
69 print "Inserted 1956 Webster"
70 s.execute("insert into derbyDB values (1910,'Union St.')")
71 print "Inserted 1910 Union"
72 s.execute("insert into derbyDB values (1,'Wandering Oak')")
73 print "Inserted 1 Wandering Oak"
74
75 s.execute(
76 "update derbyDB set num=180, addr='Grand Ave.' where num=1956")
77 print "Updated 1956 Webster to 180 Grand"
78
79 s.execute(
80 "update derbyDB set num=300, addr='Lakeshore Ave.' where num=180")
81 print "Updated 180 Grand to 300 Lakeshore"
82
83 # We select the rows and verify the results.
84 rs = s.executeQuery("SELECT num, addr FROM derbyDB ORDER BY num")
85
86 print "Verified the rows"
87 stmt = conn.createStatement()
88 Query = 'SELECT * FROM derbyDB'
89 rs = stmt.executeQuery( Query )
90 rsmd = RSMD( rs )
91 printRSMD( rsmd, Query )
92
93 rowCount = 0
94 while ( rs.next() ) :
95 rowCount += 1
96 row = ( rs.getInt( 1 ), rs.getString( 2 ) )
97 print row
98
99 stmt.close() # close stmt connection
100 s.execute("drop table derbyDB")
101 print"Dropped table derbyDB"
102
103 # We release the result and statement resources.
104 rs.close()
105 s.close()
106 print "Closed result set and statements"
107
108 # We end the transaction and the connection.
109 conn.commit()
110 conn.close()
111 print "Committed transaction and closed connection"
112
113 """ In embedded mode, an application should shut down Derby.
114 If the application fails to shut down Derby explicitly,
115 the Derby does not perform a checkpoint when the JVM shuts down, which means
116 that the next connection will be slower.
117 Explicitly shutting down Derby with the URL is preferred.
118 This style of shutdown will always throw an "exception".
119
120 """
121 gotSQLExc = False
122 try:
123 DriverManager.getConnection("jdbc:derby:;shutdown=true")
124 except SQLException:
125 print "Catching exceptions"
126 gotSQLExc = True
127
128 if (not gotSQLExc):
129 print "Database did not shut down normally"
130 else:
131 print "Database shut down normally"
132 print("SimpleApp finished")
133
134 if __name__ == '__main__':
135 SimpleApp().go()