KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > db > TableManager


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: TableManager.java 541 2006-05-30 12:34:14Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.db;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.Statement JavaDoc;
32
33 import javax.naming.NamingException JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38 import org.objectweb.easybeans.server.Embedded;
39 import org.objectweb.easybeans.tests.common.helper.DBHelper;
40
41 /**
42  * Manages a table in the database.
43  * @author Gisele Pinheiro Souza
44  * @author Eduardo Studzinski Estima de Castro
45  */

46 public class TableManager {
47
48     /**
49      * The primary key inserted in the table.
50      */

51     protected static final int PRIMARY_KEY = 1;
52
53     /**
54      * Logger.
55      */

56     private static JLog logger = JLogFactory.getLog(Embedded.class);
57
58     /**
59      * Link to a datasource.
60      */

61     private DataSource JavaDoc ds = null;
62
63     /**
64      * Creates a new instance of TableManager.
65      * @param dbName the database name in the registry.
66      * @throws NamingException if a lookup error occurs.
67      */

68     public TableManager(final String JavaDoc dbName) throws NamingException JavaDoc {
69         this(DBHelper.getDataSource(dbName));
70     }
71
72     /**
73      * Creates a new instance of TableManager.
74      * @param ds the datasource.
75      */

76     public TableManager(final DataSource JavaDoc ds) {
77         this.ds = ds;
78         if (ds == null) {
79             throw new IllegalArgumentException JavaDoc("DataSource is null");
80         }
81     }
82
83     /**
84      * Creates the table in the database done without make the commit after the
85      * insertion.
86      * @param tableName the table name.
87      * @throws SQLException if a database error occurs.
88      */

89     public void insertTable(final String JavaDoc tableName) throws SQLException JavaDoc {
90         Connection JavaDoc connection = null;
91         try {
92             logger.debug("Before insert table.");
93             connection = ds.getConnection();
94             logger.debug("Connection opened.");
95
96             PreparedStatement JavaDoc stmUpdate = null;
97             try {
98                 stmUpdate = connection.prepareStatement("CREATE TABLE " + tableName + " (CodeTest integer, NameTest varchar(30), "
99                         + "PRIMARY KEY (CodeTest))");
100                 stmUpdate.executeUpdate();
101             } finally {
102                 if (stmUpdate != null) {
103                     stmUpdate.close();
104                 }
105             }
106             logger.debug("Table created.");
107
108             //creates a line in the table
109
PreparedStatement JavaDoc stmUpdateField = null;
110             try {
111                 stmUpdateField = connection.prepareStatement("INSERT INTO " + tableName + " (CodeTest) VALUES ("
112                         + PRIMARY_KEY + ")");
113                 stmUpdateField.executeUpdate();
114             } finally {
115                 if (stmUpdateField != null) {
116                     stmUpdateField .close();
117                 }
118             }
119         } finally {
120             if (connection != null) {
121                 connection.close();
122                 logger.debug("Connection closed.");
123             }
124         }
125     }
126
127     /**
128      * Deletes the table test. This methods does not make the commit after
129      * delete.
130      * @param tableName the table name.
131      * @throws SQLException if a database error occurs.
132      */

133     public void deleteTable(final String JavaDoc tableName) throws SQLException JavaDoc {
134         Connection JavaDoc connection = null;
135         try {
136             connection = ds.getConnection();
137             // deletes the table
138
PreparedStatement JavaDoc stmUpdate = null;
139             try {
140                 stmUpdate = connection.prepareStatement("DROP TABLE " + tableName + " CASCADE");
141                 stmUpdate.executeUpdate();
142             } finally {
143                 if (stmUpdate != null) {
144                     stmUpdate.close();
145                 }
146             }
147         } finally {
148             if (connection != null) {
149                 connection.close();
150             }
151         }
152     }
153
154     /**
155      * Verifies if the table was created.
156      * @param tableName the table name.
157      * @throws SQLException if a database error occurs or the table not exists.
158      */

159     public void verifyTable(final String JavaDoc tableName) throws SQLException JavaDoc {
160         Connection JavaDoc connection = null;
161         try {
162             connection = ds.getConnection();
163             Statement JavaDoc stmt = null;
164             try {
165                 stmt = connection.createStatement();
166                 ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM " + tableName + " WHERE CodeTest = " + PRIMARY_KEY);
167                 if(!rs.next()){
168                     throw new SQLException JavaDoc("There are not values in the table");
169                 }
170             } finally {
171                 if(stmt != null){
172                     stmt.close();
173                 }
174             }
175         } finally {
176             if (connection != null) {
177                 connection.close();
178             }
179         }
180     }
181
182     /**
183      * Creates and deletes a table.
184      * @param tableName the table name.
185      * @throws SQLException if a database error occurs or the table not exists.
186      */

187     public void test(final String JavaDoc tableName) throws SQLException JavaDoc {
188         insertTable(tableName);
189         deleteTable(tableName);
190     }
191 }
192
Popular Tags