KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > remoteIterator > RITestDatabaseManager


1 /* ******************************************************************************** *
2  * Copyright (c) 2002 - 2004 Bright Side Factory. All rights reserved. *
3  * *
4  * Redistribution and use in source and binary forms, with or without modification, *
5  * are permitted provided that the following conditions are met: *
6  * *
7  * 1. Redistributions of source code must retain the above copyright notice, this *
8  * list of conditions and the following disclaimer. *
9  * *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, *
11  * this list of conditions and the following disclaimer in the documentation and/or *
12  * other materials provided with the distribution. *
13  * *
14  * 3. The end-user documentation included with the redistribution, if any, must *
15  * include the following acknowledgment: "This product includes software developed *
16  * by the Bright Side Factory (http://www.bs-factory.org/)." Alternately, this *
17  * acknowledgment may appear in the software itself, if and wherever such *
18  * third-party acknowledgments normally appear. *
19  * *
20  * 4. The names "Bright Side", "BS Framework" and "Bright Side Factory" must not be *
21  * used to endorse or promote products derived from this software without prior *
22  * written permission. For written permission, please contact info@bs-factory.org. *
23  * *
24  * 5. Products derived from this software may not be called "Bright Side", nor may *
25  * "Bright Side" appear in their name, without prior written permission of the *
26  * Bright Side Factory. *
27  * *
28  * THIS SOFTWARE IS PROVIDED ''AS IS'' BY THE COPYRIGHT OWNER AND ANY EXPRESSED OR *
29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT *
31  * SHALL THE COPYRIGHT OWNER OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
32  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
35  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE *
36  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED *
37  * OF THE POSSIBILITY OF SUCH DAMAGE. *
38  * *
39  * ================================================================================ *
40  * *
41  * This software consists of voluntary contributions made by many individuals on *
42  * behalf of the Bright Side Factory. For more information on the Bright Side *
43  * Factory, please see http://www.bs-factory.org. *
44  * *
45  * ******************************************************************************** */

46 package org.bsf.remoteIterator;
47
48 import java.sql.*;
49
50 /**
51  * Enables the creation of a table in the configured DB to test the RI. The
52  * TestRemoteIterator is using the TableName configured in this Helping class.
53  */

54 public class RITestDatabaseManager {
55     private Connection _connection = null;
56     private Statement _sqlStatement = null;
57     private ResultSet _resultSet = null;
58
59     // DB CNX PROPERTIES
60
private String JavaDoc DRIVER = "org.gjt.mm.mysql.Driver";
61     private String JavaDoc URL = "jdbc:mysql://localhost:3306/bookstore";
62     private String JavaDoc USER = "bookstore";
63     private String JavaDoc PASSWORD = "bookstore";
64
65     // DB VALUES
66
public static int NB_RECORDS = 10000;
67     public static String JavaDoc TABLE_NAME = "test_ri_table";
68
69     private String JavaDoc REMOVE_STATEMENT
70             = "DROP TABLE " + TABLE_NAME;
71
72     private String JavaDoc CREATE_STATEMENT
73             = "CREATE TABLE " + TABLE_NAME + " (oid INT PRIMARY KEY NOT NULL, label VARCHAR(20) NOT NULL)";
74
75     private String JavaDoc INSERT_STATEMENT
76             = "INSERT INTO " + TABLE_NAME + " VALUES (%OID%, %LABEL%)";
77
78     public RITestDatabaseManager() {
79         super();
80
81         try {
82             System.setProperty( "jdbc.drivers", DRIVER );
83             _connection = DriverManager.getConnection( URL, USER, PASSWORD );
84         } catch( SQLException sqlException ) {
85             System.out.println( "Unable to reach the DB with the parameters: " );
86             System.out.println( "Driver => " + DRIVER );
87             System.out.println( "URL => " + URL );
88             System.out.println( "USER => " + USER );
89             System.out.println( "PASSWORD => " + PASSWORD + "\n" );
90
91             sqlException.printStackTrace();
92
93             System.exit( 0 );
94         }
95     }
96
97     // *************************************************************************
98
// ************************** Actions methods ******************************
99
// *************************************************************************
100

101     public void removeTable() {
102         System.out.print( "Removing the old " + TABLE_NAME + " table if needed... " );
103
104         try {
105             _sqlStatement = _connection.createStatement();
106             _sqlStatement.execute( REMOVE_STATEMENT );
107         } catch( SQLException sqlException ) {
108             // Nothing to do (probably no such table...
109
} finally {
110             showDone();
111         }
112
113         // We close the statement here cause we might want to go on (with creation, population...)
114
try {
115             _sqlStatement.close();
116             _sqlStatement = null;
117         } catch( SQLException e ) {
118             // Who cares...
119
}
120     }
121
122     public void createTable() throws SQLException {
123         System.out.print( "Creating the table " + TABLE_NAME + " " );
124
125         _sqlStatement = _connection.createStatement();
126
127         _sqlStatement.execute( CREATE_STATEMENT );
128
129         showDone();
130     }
131
132     public void populateTable() throws SQLException {
133         System.out.print( "Populating the " + TABLE_NAME + " table with " + NB_RECORDS + " rows... " );
134
135         _sqlStatement = _connection.createStatement();
136
137         for ( int index = 0 ; index < NB_RECORDS ; index++ ) {
138             String JavaDoc sql = INSERT_STATEMENT.replaceAll( "%OID%", String.valueOf( index ) );
139             sql = sql.replaceAll( "%LABEL%", "'Label " + index + "'" );
140
141             _sqlStatement.execute( sql );
142         }
143
144         showDone();
145     }
146
147     public void releaseRessources() {
148         try {
149             if ( _resultSet != null ) {
150                 System.out.print( "Closing the ResultSet " );
151                 _resultSet.close();
152                 showDone();
153             }
154         } catch( SQLException e ) {
155             showFaillure();
156         }
157
158         try {
159             if ( _sqlStatement != null ) {
160                 System.out.print( "Closing the Statement " );
161                 _sqlStatement.close();
162                 showDone();
163             }
164         } catch( SQLException e ) {
165             showFaillure();
166         }
167
168         try {
169             if ( _connection != null ) {
170                 System.out.print( "Closing the Connection " );
171                 _connection.close();
172                 showDone();
173             }
174         } catch( SQLException e ) {
175             showFaillure();
176         }
177     }
178
179     // *************************************************************************
180
// ************************** Utility methods ******************************
181
// *************************************************************************
182

183     private void showDone() {
184         System.out.println( "[DONE]" );
185     }
186
187     private void showFaillure() {
188         System.out.println( "[FAILLURE]" );
189     }
190
191     // *************************************************************************
192
// ***************************** Main method *******************************
193
// *************************************************************************
194

195     public static void main( String JavaDoc[] args ) {
196         if ( args.length == 0 || args.length > 1 ) {
197             System.out.println( "RITestDatabaseManager usage:" );
198             System.out.println( " => (...) RITestDatabaseManager [OPTION]" );
199             System.out.println( "\nPossible values for OPTION are:" );
200             System.out.println( " -removeTestTable => Removes the table for the test" );
201             System.out.println( " -createTestTable => Creates and populates the table for the test" );
202
203             // We quit, no valid parameters...
204
System.exit( 0 );
205         }
206
207         RITestDatabaseManager dbManager = new RITestDatabaseManager();
208
209         if ( "-removeTestTable".equalsIgnoreCase( args[ 0 ] ) ) {
210             dbManager.removeTable();
211         } else if ( "-createTestTable".equalsIgnoreCase( args[ 0 ] ) ) {
212             dbManager.removeTable();
213
214             try {
215                 dbManager.createTable();
216                 dbManager.populateTable();
217             } catch( SQLException sqlException ) {
218                 dbManager.showFaillure();
219
220                 // And display the stacktrace
221
System.out.println( "\n" );
222                 sqlException.printStackTrace();
223             }
224         }
225
226         // We release the ressources
227
dbManager.releaseRessources();
228     }
229 }
230
Popular Tags