Source Code Cross Referenced for PostgreSqlTestDatabase.java in  » J2EE » openejb3 » org » apache » openejb » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.openejb.test;
017:
018:        import org.apache.openejb.test.beans.Database;
019:        import org.apache.openejb.test.beans.DatabaseHome;
020:
021:        import javax.naming.InitialContext;
022:        import java.rmi.RemoteException;
023:        import java.sql.Connection;
024:        import java.sql.DriverManager;
025:        import java.sql.PreparedStatement;
026:        import java.sql.ResultSet;
027:        import java.sql.SQLException;
028:        import java.sql.Statement;
029:        import java.util.Properties;
030:
031:        /**
032:         * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
033:         */
034:        public class PostgreSqlTestDatabase implements  TestDatabase {
035:
036:            protected Database database;
037:            protected InitialContext initialContext;
038:
039:            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\"))";
040:            private static String _dropAccount = "DROP TABLE account";
041:            //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\") )";
042:            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\") )";
043:            private static String _dropEntity = "DROP TABLE entity";
044:
045:            public void createEntityTable() throws java.sql.SQLException {
046:                try {
047:                    database.execute("DROP SEQUENCE entity_id_seq");
048:                } catch (Exception e) {
049:                    // not concerned
050:                }
051:                try {
052:                    database.execute(_dropEntity);
053:                } catch (Exception e) {
054:                    // not concerned
055:                }
056:                try {
057:                    database.execute("CREATE SEQUENCE entity_id_seq");
058:                } catch (Exception e) {
059:                    // not concerned
060:                }
061:                try {
062:                    database.execute(_createEntity);
063:                } catch (RemoteException re) {
064:                    if (re.detail != null
065:                            && re.detail instanceof  java.sql.SQLException) {
066:                        throw (java.sql.SQLException) re.detail;
067:                    } else {
068:                        throw new java.sql.SQLException(
069:                                "Cannot create entity table: "
070:                                        + re.getMessage(), _createEntity);
071:                    }
072:                }
073:            }
074:
075:            public void dropEntityTable() throws java.sql.SQLException {
076:                try {
077:                    database.execute("DROP SEQUENCE entity_id_seq");
078:                } catch (Exception e) {
079:                    // not concerned
080:                }
081:                try {
082:                    database.execute(_dropEntity);
083:                } catch (RemoteException re) {
084:                    if (re.detail != null
085:                            && re.detail instanceof  java.sql.SQLException) {
086:                        throw (java.sql.SQLException) re.detail;
087:                    } else {
088:                        throw new java.sql.SQLException(
089:                                "Unable to drop entity table: "
090:                                        + re.getMessage(), _dropEntity);
091:                    }
092:                }
093:            }
094:
095:            public void createAccountTable() throws java.sql.SQLException {
096:                try {
097:                    database.execute("DROP SEQUENCE account_id_seq");
098:                } catch (Exception e) {
099:                    // not concerned
100:                }
101:                try {
102:                    database.execute("DROP TABLE account");
103:                } catch (Exception e) {
104:                    // not concerned
105:                }
106:                try {
107:                    database.execute("CREATE SEQUENCE account_id_seq");
108:                } catch (Exception e) {
109:                    // not concerned
110:                }
111:                try {
112:                    database.execute(_createAccount);
113:                } catch (RemoteException re) {
114:                    if (re.detail != null
115:                            && re.detail instanceof  java.sql.SQLException) {
116:                        throw (java.sql.SQLException) re.detail;
117:                    } else {
118:                        throw new java.sql.SQLException(
119:                                "Cannot create account table: "
120:                                        + re.getMessage(), _createAccount);
121:                    }
122:                }
123:            }
124:
125:            public void dropAccountTable() throws java.sql.SQLException {
126:                try {
127:                    try {
128:                        database.execute("DROP SEQUENCE account_id_seq");
129:                    } catch (Exception e) {
130:                        // not concerned
131:                    }
132:                    database.execute(_dropAccount);
133:                } catch (RemoteException re) {
134:                    if (re.detail != null
135:                            && re.detail instanceof  java.sql.SQLException) {
136:                        throw (java.sql.SQLException) re.detail;
137:                    } else {
138:                        throw new java.sql.SQLException(
139:                                "Cannot drop account table: " + re.getMessage(),
140:                                _dropAccount);
141:                    }
142:                }
143:            }
144:
145:            public void start() throws IllegalStateException {
146:                try {
147:                    Properties properties = TestManager.getServer()
148:                            .getContextEnvironment();
149:                    initialContext = new InitialContext(properties);
150:
151:                    /* Create database */
152:                    Object obj = initialContext
153:                            .lookup("client/tools/DatabaseHome");
154:                    DatabaseHome databaseHome = (DatabaseHome) javax.rmi.PortableRemoteObject
155:                            .narrow(obj, DatabaseHome.class);
156:                    database = databaseHome.create();
157:                } catch (Exception e) {
158:                    throw new IllegalStateException("Cannot start database: "
159:                            + e.getClass().getName() + " " + e.getMessage());
160:                }
161:            }
162:
163:            public void stop() throws IllegalStateException {
164:            }
165:
166:            public void init(Properties props) throws IllegalStateException {
167:            }
168:
169:            public static void main(String[] args) {
170:                System.out
171:                        .println("Checking if driver is registered with DriverManager.");
172:                try {
173:                    ClassLoader cl = (ClassLoader) java.security.AccessController
174:                            .doPrivileged(new java.security.PrivilegedAction() {
175:                                public Object run() {
176:                                    return Thread.currentThread()
177:                                            .getContextClassLoader();
178:                                }
179:                            });
180:                    Class.forName("org.postgresql.Driver", true, cl);
181:                } catch (ClassNotFoundException e) {
182:                    System.out.println("Couldn't find the driver!");
183:                    e.printStackTrace();
184:                    System.exit(1);
185:                }
186:
187:                System.out
188:                        .println("Registered the driver, so let's make a connection.");
189:
190:                Connection conn = null;
191:
192:                try {
193:                    conn = DriverManager.getConnection(
194:                            "jdbc:postgresql://localhost/openejbtest",
195:                            "openejbuser", "javaone");
196:                } catch (SQLException e) {
197:                    System.out.println("Couldn't connect.");
198:                    e.printStackTrace();
199:                    System.exit(1);
200:                }
201:
202:                if (conn == null) {
203:                    System.out.println("No connection!");
204:                }
205:
206:                Statement stmt = null;
207:
208:                try {
209:                    stmt = conn.createStatement();
210:                } catch (SQLException e) {
211:                    System.out.println("Couldn't create a statement.");
212:                    e.printStackTrace();
213:                    System.exit(1);
214:                }
215:
216:                ResultSet rs = null;
217:
218:                try {
219:                    stmt.execute("DROP TABLE entity");
220:                } catch (SQLException e) {
221:                }
222:
223:                System.out.println("Creating entity table.");
224:                try {
225:                    stmt.execute(_createEntity);
226:                } catch (SQLException e) {
227:                    System.out.println("Couldn't create the entity table");
228:                    e.printStackTrace();
229:                    System.exit(1);
230:                }
231:
232:                System.out.println("Inserting record.");
233:                try {
234:                    PreparedStatement pstmt = conn
235:                            .prepareStatement("insert into entity (id, first_name, last_name) values (?,?,?)");
236:                    pstmt.setInt(1, 101);
237:                    pstmt.setString(2, "Bunson");
238:                    pstmt.setString(3, "Honeydew");
239:                    pstmt.executeUpdate();
240:                } catch (SQLException e) {
241:                    System.out.println("Couldn't create the entity table");
242:                    e.printStackTrace();
243:                    System.exit(1);
244:                }
245:
246:                System.out.println("Selecting the record.");
247:                try {
248:                    PreparedStatement pstmt = conn
249:                            .prepareStatement("select id from entity where first_name = ? AND last_name = ?");
250:                    pstmt.setString(1, "Bunson");
251:                    pstmt.setString(2, "Honeydew");
252:                    ResultSet set = pstmt.executeQuery();
253:                } catch (SQLException e) {
254:                    System.out.println("Couldn't select the entry");
255:                    e.printStackTrace();
256:                    System.exit(1);
257:                }
258:
259:                System.out.println("Dropping the entity table.");
260:                try {
261:                    stmt.execute(_dropEntity);
262:                } catch (SQLException e) {
263:                    System.out.println("Couldn't drop the entity table");
264:                    e.printStackTrace();
265:                    System.exit(1);
266:                }
267:
268:                try {
269:                    conn.close();
270:                } catch (SQLException e) {
271:                    System.out.println("Couldn't create the sequense");
272:                    e.printStackTrace();
273:                    System.exit(1);
274:                }
275:
276:            }
277:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.