1 19 20 package org.apache.avalon.examples.jdbcdatasource; 21 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Timestamp ; 27 28 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 29 import org.apache.avalon.framework.activity.Disposable; 30 import org.apache.avalon.framework.activity.Initializable; 31 import org.apache.avalon.framework.configuration.Configurable; 32 import org.apache.avalon.framework.configuration.Configuration; 33 import org.apache.avalon.framework.configuration.ConfigurationException; 34 import org.apache.avalon.framework.logger.AbstractLogEnabled; 35 import org.apache.avalon.framework.service.ServiceException; 36 import org.apache.avalon.framework.service.ServiceManager; 37 import org.apache.avalon.framework.service.ServiceSelector; 38 import org.apache.avalon.framework.service.Serviceable; 39 40 51 public class DefaultHelloDBService 52 extends AbstractLogEnabled 53 implements HelloDBService, Serviceable, Configurable, Initializable, Disposable 54 { 55 56 private String m_dataSourceName; 57 private ServiceSelector m_dbSelector; 58 private DataSourceComponent m_dataSource; 59 60 63 64 public DefaultHelloDBService() 65 { 66 } 67 68 71 79 private Connection getConnection() 80 throws SQLException 81 { 82 return m_dataSource.getConnection(); 83 } 84 85 92 private void initializeDatabase() 93 throws SQLException 94 { 95 try 96 { 97 Connection conn = getConnection(); 98 try 99 { 100 PreparedStatement stmt = conn.prepareStatement( 101 "CREATE CACHED TABLE titles ( " + 102 " title VARCHAR NOT NULL, " + 103 " time TIMESTAMP NOT NULL " + 104 ")" ); 105 stmt.executeUpdate(); 106 } 107 finally 108 { 109 conn.close(); 111 } 112 } 113 catch( SQLException e ) 114 { 115 if( e.getMessage().startsWith( "Table already exists" ) ) 116 { 117 } 119 else 120 { 121 throw e; 122 } 123 } 124 } 125 126 129 134 public void addRow( String title ) 135 { 136 getLogger().debug( "DefaultHelloDBService.addRow(" + title + ")" ); 137 138 try 139 { 140 Connection conn = getConnection(); 141 try 142 { 143 PreparedStatement stmt = conn.prepareStatement( 144 "INSERT INTO titles (title, time) VALUES (?, now())" ); 145 stmt.setString( 1, title ); 146 int result = stmt.executeUpdate(); 147 if( result == 1 ) 148 { 149 System.out.println( "Added '" + title + "' to the database." ); 150 } 151 else 152 { 153 getLogger().error( "Unable to add title to the database. database returned " + 154 result + " inserted." ); 155 } 156 } 157 finally 158 { 159 conn.close(); 161 } 162 } 163 catch( SQLException e ) 164 { 165 getLogger().error( "Unable to add title to the database.", e ); 166 } 167 } 168 169 172 public void deleteRows() 173 { 174 getLogger().debug( "DefaultHelloDBService.deleteRows()" ); 175 176 try 177 { 178 Connection conn = getConnection(); 179 try 180 { 181 PreparedStatement stmt = conn.prepareStatement( 182 "DELETE FROM titles" ); 183 int result = stmt.executeUpdate(); 184 System.out.println( "Deleted " + result + " titles from the database." ); 185 } 186 finally 187 { 188 conn.close(); 190 } 191 } 192 catch( SQLException e ) 193 { 194 getLogger().error( "Unable to delete old titles from the database.", e ); 195 } 196 } 197 198 202 public void logRows() 203 { 204 getLogger().debug( "DefaultHelloDBService.logRows()" ); 205 206 try 207 { 208 Connection conn = getConnection(); 209 try 210 { 211 PreparedStatement stmt = conn.prepareStatement( 212 "SELECT title, time FROM titles" ); 213 ResultSet rs = stmt.executeQuery(); 214 int count = 0; 215 while( rs.next() ) 216 { 217 String title = rs.getString( 1 ); 218 Timestamp time = rs.getTimestamp( 2 ); 219 220 System.out.println( " '" + title + "' saved at " + time ); 221 count++; 222 } 223 224 if( count == 0 ) 225 { 226 System.out.println( "The database does not contain any saved titles." ); 227 } 228 else 229 { 230 System.out.println( "The database contains " + count + " titles." ); 231 } 232 } 233 finally 234 { 235 conn.close(); 237 } 238 } 239 catch( SQLException e ) 240 { 241 getLogger().error( "Unable to delete old titles from the database.", e ); 242 } 243 } 244 245 248 256 public void service( final ServiceManager manager ) 257 throws ServiceException 258 { 259 getLogger().debug( "DefaultHelloDBService.compose()" ); 260 m_dbSelector = (ServiceSelector)manager.lookup( DataSourceComponent.ROLE + "Selector" ); 261 } 262 263 266 273 public void configure( Configuration configuration ) 274 throws ConfigurationException 275 { 276 getLogger().debug( "DefaultHelloDBService.configure()" ); 277 278 m_dataSourceName = configuration.getChild( "dbpool" ).getValue(); 280 } 281 282 285 290 public void initialize() 291 throws Exception 292 { 293 getLogger().debug( "DefaultHelloDBService.initialize()" ); 294 295 m_dataSource = (DataSourceComponent)m_dbSelector.select( m_dataSourceName ); 297 298 initializeDatabase(); 300 } 301 302 305 308 public void dispose() 309 { 310 getLogger().debug( "DefaultHelloDBService.dispose()" ); 311 312 if( m_dbSelector != null ) 314 { 315 if( m_dataSource != null ) 316 { 317 m_dbSelector.release( m_dataSource ); 318 m_dataSource = null; 319 } 320 m_dbSelector = null; 321 } 322 } 323 } 324 325 | Popular Tags |