Source Code Cross Referenced for TableManager.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » tests » common » db » 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 » ow2 easybeans » org.ow2.easybeans.tests.common.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: TableManager.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.tests.common.db;
025:
026:        import java.sql.Connection;
027:        import java.sql.PreparedStatement;
028:        import java.sql.ResultSet;
029:        import java.sql.SQLException;
030:        import java.sql.Statement;
031:
032:        import javax.naming.NamingException;
033:        import javax.sql.DataSource;
034:
035:        import org.ow2.easybeans.server.Embedded;
036:        import org.ow2.easybeans.tests.common.helper.DBHelper;
037:        import org.ow2.util.log.Log;
038:        import org.ow2.util.log.LogFactory;
039:
040:        /**
041:         * Manages a table in the database.
042:         * @author Gisele Pinheiro Souza
043:         * @author Eduardo Studzinski Estima de Castro
044:         */
045:        public class TableManager {
046:
047:            /**
048:             * The primary key inserted in the table.
049:             */
050:            protected static final int PRIMARY_KEY = 1;
051:
052:            /**
053:             * Logger.
054:             */
055:            private static Log logger = LogFactory.getLog(Embedded.class);
056:
057:            /**
058:             * Link to a datasource.
059:             */
060:            private DataSource ds = null;
061:
062:            /**
063:             * Creates a new instance of TableManager.
064:             * @param dbName the database name in the registry.
065:             * @throws NamingException if a lookup error occurs.
066:             */
067:            public TableManager(final String dbName) throws NamingException {
068:                this (DBHelper.getDataSource(dbName));
069:            }
070:
071:            /**
072:             * Creates a new instance of TableManager.
073:             * @param ds the datasource.
074:             */
075:            public TableManager(final DataSource ds) {
076:                this .ds = ds;
077:                if (ds == null) {
078:                    throw new IllegalArgumentException("DataSource is null");
079:                }
080:            }
081:
082:            /**
083:             * Creates the table in the database done without make the commit after the
084:             * insertion.
085:             * @param tableName the table name.
086:             * @throws SQLException if a database error occurs.
087:             */
088:            public void insertTable(final String tableName) throws SQLException {
089:                Connection connection = null;
090:                try {
091:                    logger.debug("Before insert table.");
092:                    connection = ds.getConnection();
093:                    logger.debug("Connection opened.");
094:
095:                    PreparedStatement stmUpdate = null;
096:                    try {
097:                        stmUpdate = connection.prepareStatement("CREATE TABLE "
098:                                + tableName
099:                                + " (CodeTest integer, NameTest varchar(30), "
100:                                + "PRIMARY KEY (CodeTest))");
101:                        stmUpdate.executeUpdate();
102:                    } finally {
103:                        if (stmUpdate != null) {
104:                            stmUpdate.close();
105:                        }
106:                    }
107:                    logger.debug("Table created.");
108:
109:                    //creates a line in the table
110:                    PreparedStatement stmUpdateField = null;
111:                    try {
112:                        stmUpdateField = connection
113:                                .prepareStatement("INSERT INTO " + tableName
114:                                        + " (CodeTest) VALUES  (" + PRIMARY_KEY
115:                                        + ")");
116:                        stmUpdateField.executeUpdate();
117:                    } finally {
118:                        if (stmUpdateField != null) {
119:                            stmUpdateField.close();
120:                        }
121:                    }
122:                } finally {
123:                    if (connection != null) {
124:                        connection.close();
125:                        logger.debug("Connection closed.");
126:                    }
127:                }
128:            }
129:
130:            /**
131:             * Deletes the table test. This methods does not make the commit after
132:             * delete.
133:             * @param tableName the table name.
134:             * @throws SQLException if a database error occurs.
135:             */
136:            public void deleteTable(final String tableName) throws SQLException {
137:                Connection connection = null;
138:                try {
139:                    connection = ds.getConnection();
140:                    // deletes the table
141:                    PreparedStatement stmUpdate = null;
142:                    try {
143:                        stmUpdate = connection.prepareStatement("DROP TABLE "
144:                                + tableName + " CASCADE");
145:                        stmUpdate.executeUpdate();
146:                    } finally {
147:                        if (stmUpdate != null) {
148:                            stmUpdate.close();
149:                        }
150:                    }
151:                } finally {
152:                    if (connection != null) {
153:                        connection.close();
154:                    }
155:                }
156:            }
157:
158:            /**
159:             * Verifies if the table was created.
160:             * @param tableName the table name.
161:             * @throws SQLException if a database error occurs or the table not exists.
162:             */
163:            public void verifyTable(final String tableName) throws SQLException {
164:                Connection connection = null;
165:                try {
166:                    connection = ds.getConnection();
167:                    Statement stmt = null;
168:                    try {
169:                        stmt = connection.createStatement();
170:                        ResultSet rs = stmt.executeQuery("SELECT * FROM "
171:                                + tableName + " WHERE CodeTest = "
172:                                + PRIMARY_KEY);
173:                        if (!rs.next()) {
174:                            throw new SQLException(
175:                                    "There are not values in the table");
176:                        }
177:                    } finally {
178:                        if (stmt != null) {
179:                            stmt.close();
180:                        }
181:                    }
182:                } finally {
183:                    if (connection != null) {
184:                        connection.close();
185:                    }
186:                }
187:            }
188:
189:            /**
190:             * Creates and deletes a table.
191:             * @param tableName the table name.
192:             * @throws SQLException if a database error occurs or the table not exists.
193:             */
194:            public void test(final String tableName) throws SQLException {
195:                insertTable(tableName);
196:                deleteTable(tableName);
197:            }
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.