1 46 package org.bsf.remoteIterator; 47 48 import java.sql.*; 49 50 54 public class RITestDatabaseManager { 55 private Connection _connection = null; 56 private Statement _sqlStatement = null; 57 private ResultSet _resultSet = null; 58 59 private String DRIVER = "org.gjt.mm.mysql.Driver"; 61 private String URL = "jdbc:mysql://localhost:3306/bookstore"; 62 private String USER = "bookstore"; 63 private String PASSWORD = "bookstore"; 64 65 public static int NB_RECORDS = 10000; 67 public static String TABLE_NAME = "test_ri_table"; 68 69 private String REMOVE_STATEMENT 70 = "DROP TABLE " + TABLE_NAME; 71 72 private String CREATE_STATEMENT 73 = "CREATE TABLE " + TABLE_NAME + " (oid INT PRIMARY KEY NOT NULL, label VARCHAR(20) NOT NULL)"; 74 75 private String 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 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 } finally { 110 showDone(); 111 } 112 113 try { 115 _sqlStatement.close(); 116 _sqlStatement = null; 117 } catch( SQLException e ) { 118 } 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 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 183 private void showDone() { 184 System.out.println( "[DONE]" ); 185 } 186 187 private void showFaillure() { 188 System.out.println( "[FAILLURE]" ); 189 } 190 191 195 public static void main( String [] 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 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 System.out.println( "\n" ); 222 sqlException.printStackTrace(); 223 } 224 } 225 226 dbManager.releaseRessources(); 228 } 229 } 230 | Popular Tags |