1 package org.openejb.test; 2 3 import java.rmi.RemoteException ; 4 import java.sql.Connection ; 5 import java.sql.DriverManager ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.sql.Statement ; 10 import java.util.Properties ; 11 12 import javax.naming.InitialContext ; 13 14 import org.openejb.test.beans.Database; 15 import org.openejb.test.beans.DatabaseHome; 16 import org.openejb.OpenEJB; 17 21 public class PostgreSqlTestDatabase implements TestDatabase{ 22 23 protected Database database; 24 protected InitialContext initialContext; 25 26 27 private static String _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 _dropAccount = "DROP TABLE account"; 29 private static String _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 _dropEntity = "DROP TABLE entity"; 32 33 public void createEntityTable() throws java.sql.SQLException { 34 try{ 35 database.execute("DROP SEQUENCE entity_id_seq"); 36 } catch (Exception e){ 37 } 39 try{ 40 database.execute(_dropEntity); 41 } catch (Exception e){ 42 } 44 try{ 45 database.execute("CREATE SEQUENCE entity_id_seq"); 46 } catch (Exception e){ 47 } 49 try{ 50 database.execute(_createEntity); 51 } catch (RemoteException re){ 52 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 53 throw (java.sql.SQLException )re.detail; 54 } else { 55 throw new java.sql.SQLException ("Cannot create entity table: "+re.getMessage(), _createEntity); 56 } 57 } 58 } 59 public void dropEntityTable() throws java.sql.SQLException { 60 try{ 61 database.execute("DROP SEQUENCE entity_id_seq"); 62 } catch (Exception e){ 63 } 65 try { 66 database.execute(_dropEntity); 67 } catch (RemoteException re){ 68 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 69 throw (java.sql.SQLException )re.detail; 70 } else { 71 throw new java.sql.SQLException ("Unable to drop entity table: "+re.getMessage(), _dropEntity); 72 } 73 } 74 } 75 76 77 public void createAccountTable() throws java.sql.SQLException { 78 try{ 79 database.execute("DROP SEQUENCE account_id_seq"); 80 } catch (Exception e){ 81 } 83 try{ 84 database.execute("DROP TABLE account"); 85 } catch (Exception e){ 86 } 88 try{ 89 database.execute("CREATE SEQUENCE account_id_seq"); 90 } catch (Exception e){ 91 } 93 try{ 94 database.execute(_createAccount); 95 } catch (RemoteException re){ 96 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 97 throw (java.sql.SQLException )re.detail; 98 } else { 99 throw new java.sql.SQLException ("Cannot create account table: "+re.getMessage(), _createAccount); 100 } 101 } 102 } 103 104 public void dropAccountTable() throws java.sql.SQLException { 105 try { 106 try{ 107 database.execute("DROP SEQUENCE account_id_seq"); 108 } catch (Exception e){ 109 } 111 database.execute(_dropAccount); 112 } catch (RemoteException re){ 113 if (re.detail != null && re.detail instanceof java.sql.SQLException ) { 114 throw (java.sql.SQLException )re.detail; 115 } else { 116 throw new java.sql.SQLException ("Cannot drop account table: "+re.getMessage(), _dropAccount); 117 } 118 } 119 } 120 121 public void start() throws IllegalStateException { 122 try { 123 Properties properties = TestManager.getServer().getContextEnvironment(); 124 initialContext = new InitialContext (properties); 125 126 127 Object obj = initialContext.lookup("client/tools/DatabaseHome"); 128 DatabaseHome databaseHome = (DatabaseHome)javax.rmi.PortableRemoteObject.narrow( obj, DatabaseHome.class); 129 database = databaseHome.create(); 130 } catch (Exception e){ 131 throw new IllegalStateException ("Cannot start database: "+e.getClass().getName()+" "+e.getMessage()); 132 } 133 } 134 135 public void stop() throws IllegalStateException { 136 } 137 138 public void init(Properties props) throws IllegalStateException { 139 } 140 141 public static void main(String [] args){ 142 System.out.println("Checking if driver is registered with DriverManager."); 143 try{ 144 ClassLoader cl = OpenEJB.getContextClassLoader(); 145 Class.forName("org.postgresql.Driver", true, cl); 146 } catch (ClassNotFoundException 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 conn = null; 155 156 try{ 157 conn = DriverManager.getConnection("jdbc:postgresql://localhost/openejbtest", "openejbuser","javaone"); 158 } catch (SQLException 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 stmt = null; 169 170 try{ 171 stmt = conn.createStatement(); 172 } catch (SQLException e){ 173 System.out.println("Couldn't create a statement."); 174 e.printStackTrace(); 175 System.exit(1); 176 } 177 178 ResultSet rs = null; 179 180 try{ 181 stmt.execute("DROP TABLE entity"); 182 } catch (SQLException e){ 183 } 184 185 System.out.println("Creating entity table."); 186 try{ 187 stmt.execute(_createEntity); 188 } catch (SQLException 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 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 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 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 set = pstmt.executeQuery(); 213 } catch (SQLException 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 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 e){ 232 System.out.println("Couldn't create the sequense"); 233 e.printStackTrace(); 234 System.exit(1); 235 } 236 237 } 238 } 239 | Popular Tags |