KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > PostgreSqlTestDatabase


1 package org.openejb.test;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.DriverManager JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.Statement JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import javax.naming.InitialContext JavaDoc;
13
14 import org.openejb.test.beans.Database;
15 import org.openejb.test.beans.DatabaseHome;
16 import org.openejb.OpenEJB;
17 /**
18  *
19  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
20  */

21 public class PostgreSqlTestDatabase implements TestDatabase{
22     
23     protected Database database;
24     protected InitialContext JavaDoc initialContext;
25     
26
27     private static String JavaDoc _createAccount = "CREATE TABLE account ( ssn CHAR(11), first_name CHAR(20), last_name CHAR(20), balance INT, Constraint \"account_pkey\" Primary Key (\"ssn\"))";
28     private static String JavaDoc _dropAccount = "DROP TABLE account";
29     //private static String _createEntity = "CREATE TABLE entity ( id INT NOT NULL, first_name CHAR(20), last_name CHAR(20), Constraint \"entity_pkey\" Primary Key (\"id\") )";
30
private static String JavaDoc _createEntity = "CREATE TABLE entity ( id INT DEFAULT nextval('entity_id_seq') , first_name CHAR(20), last_name CHAR(20), Constraint \"entity_pkey\" Primary Key (\"id\") )";
31     private static String JavaDoc _dropEntity = "DROP TABLE entity";
32         
33     public void createEntityTable() throws java.sql.SQLException JavaDoc {
34         try{
35             database.execute("DROP SEQUENCE entity_id_seq");
36         } catch (Exception JavaDoc e){
37             // not concerned
38
}
39         try{
40             database.execute(_dropEntity);
41         } catch (Exception JavaDoc e){
42             // not concerned
43
}
44         try{
45             database.execute("CREATE SEQUENCE entity_id_seq");
46         } catch (Exception JavaDoc e){
47             // not concerned
48
}
49         try{
50             database.execute(_createEntity);
51         } catch (RemoteException JavaDoc re){
52             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
53                 throw (java.sql.SQLException JavaDoc)re.detail;
54             } else {
55                 throw new java.sql.SQLException JavaDoc("Cannot create entity table: "+re.getMessage(), _createEntity);
56             }
57         }
58     }
59     public void dropEntityTable() throws java.sql.SQLException JavaDoc {
60         try{
61             database.execute("DROP SEQUENCE entity_id_seq");
62         } catch (Exception JavaDoc e){
63             // not concerned
64
}
65         try {
66             database.execute(_dropEntity);
67         } catch (RemoteException JavaDoc re){
68             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
69                 throw (java.sql.SQLException JavaDoc)re.detail;
70             } else {
71                 throw new java.sql.SQLException JavaDoc("Unable to drop entity table: "+re.getMessage(), _dropEntity);
72             }
73         }
74     }
75     
76     
77     public void createAccountTable() throws java.sql.SQLException JavaDoc {
78         try{
79             database.execute("DROP SEQUENCE account_id_seq");
80         } catch (Exception JavaDoc e){
81             // not concerned
82
}
83         try{
84             database.execute("DROP TABLE account");
85         } catch (Exception JavaDoc e){
86             // not concerned
87
}
88         try{
89             database.execute("CREATE SEQUENCE account_id_seq");
90         } catch (Exception JavaDoc e){
91             // not concerned
92
}
93         try{
94             database.execute(_createAccount);
95         } catch (RemoteException JavaDoc re){
96             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
97                 throw (java.sql.SQLException JavaDoc)re.detail;
98             } else {
99                 throw new java.sql.SQLException JavaDoc("Cannot create account table: "+re.getMessage(), _createAccount);
100             }
101         }
102     }
103     
104     public void dropAccountTable() throws java.sql.SQLException JavaDoc {
105         try {
106             try{
107                 database.execute("DROP SEQUENCE account_id_seq");
108             } catch (Exception JavaDoc e){
109                 // not concerned
110
}
111             database.execute(_dropAccount);
112         } catch (RemoteException JavaDoc re){
113             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
114                 throw (java.sql.SQLException JavaDoc)re.detail;
115             } else {
116                 throw new java.sql.SQLException JavaDoc("Cannot drop account table: "+re.getMessage(), _dropAccount);
117             }
118         }
119     }
120     
121     public void start() throws IllegalStateException JavaDoc {
122         try {
123             Properties JavaDoc properties = TestManager.getServer().getContextEnvironment();
124             initialContext = new InitialContext JavaDoc(properties);
125     
126             /* Create database */
127             Object JavaDoc obj = initialContext.lookup("client/tools/DatabaseHome");
128             DatabaseHome databaseHome = (DatabaseHome)javax.rmi.PortableRemoteObject.narrow( obj, DatabaseHome.class);
129             database = databaseHome.create();
130         } catch (Exception JavaDoc e){
131             throw new IllegalStateException JavaDoc("Cannot start database: "+e.getClass().getName()+" "+e.getMessage());
132         }
133     }
134     
135     public void stop() throws IllegalStateException JavaDoc {
136     }
137     
138     public void init(Properties JavaDoc props) throws IllegalStateException JavaDoc {
139     }
140
141     public static void main(String JavaDoc[] args){
142         System.out.println("Checking if driver is registered with DriverManager.");
143         try{
144             ClassLoader JavaDoc cl = OpenEJB.getContextClassLoader();
145             Class.forName("org.postgresql.Driver", true, cl);
146         } catch (ClassNotFoundException JavaDoc e){
147             System.out.println("Couldn't find the driver!");
148             e.printStackTrace();
149             System.exit(1);
150         }
151
152         System.out.println("Registered the driver, so let's make a connection.");
153         
154         Connection JavaDoc conn = null;
155
156         try{
157             conn = DriverManager.getConnection("jdbc:postgresql://localhost/openejbtest", "openejbuser","javaone");
158         } catch (SQLException JavaDoc e){
159             System.out.println("Couldn't connect.");
160             e.printStackTrace();
161             System.exit(1);
162         }
163
164         if (conn == null) {
165             System.out.println("No connection!");
166         }
167
168         Statement JavaDoc stmt = null;
169
170         try{
171             stmt = conn.createStatement();
172         } catch (SQLException JavaDoc e){
173             System.out.println("Couldn't create a statement.");
174             e.printStackTrace();
175             System.exit(1);
176         }
177
178         ResultSet JavaDoc rs = null;
179
180         try{
181             stmt.execute("DROP TABLE entity");
182         } catch (SQLException JavaDoc e){
183         }
184
185         System.out.println("Creating entity table.");
186         try{
187             stmt.execute(_createEntity);
188         } catch (SQLException JavaDoc e){
189             System.out.println("Couldn't create the entity table");
190             e.printStackTrace();
191             System.exit(1);
192         }
193
194         System.out.println("Inserting record.");
195         try{
196             PreparedStatement JavaDoc pstmt = conn.prepareStatement("insert into entity (id, first_name, last_name) values (?,?,?)");
197             pstmt.setInt(1, 101);
198             pstmt.setString(2, "Bunson");
199             pstmt.setString(3, "Honeydew");
200             pstmt.executeUpdate();
201         } catch (SQLException JavaDoc e){
202             System.out.println("Couldn't create the entity table");
203             e.printStackTrace();
204             System.exit(1);
205         }
206
207         System.out.println("Selecting the record.");
208         try{
209             PreparedStatement JavaDoc pstmt = conn.prepareStatement("select id from entity where first_name = ? AND last_name = ?");
210             pstmt.setString(1, "Bunson");
211             pstmt.setString(2, "Honeydew");
212             ResultSet JavaDoc set = pstmt.executeQuery();
213         } catch (SQLException JavaDoc e){
214             System.out.println("Couldn't select the entry");
215             e.printStackTrace();
216             System.exit(1);
217         }
218
219     
220         System.out.println("Dropping the entity table.");
221         try{
222             stmt.execute(_dropEntity);
223         } catch (SQLException JavaDoc e){
224             System.out.println("Couldn't drop the entity table");
225             e.printStackTrace();
226             System.exit(1);
227         }
228
229         try{
230             conn.close();
231         } catch (SQLException JavaDoc e){
232             System.out.println("Couldn't create the sequense");
233             e.printStackTrace();
234             System.exit(1);
235         }
236
237     }
238 }
239
Popular Tags