Source of GuestDataBean.java


  1: // Fig. 27.21: GuestDataBean.java
  2: // Class GuestDataBean makes a database connection and supports 
  3: // inserting and retrieving data from the database.
  4: package com.deitel.jhtp6.jsp.beans;
  5: 
  6: import java.sql.SQLException;
  7: import javax.sql.rowset.CachedRowSet;
  8: import java.util.ArrayList;
  9: import com.sun.rowset.CachedRowSetImpl; // CachedRowSet implementation
 10: 
 11: public class GuestDataBean 
 12: {
 13:    private CachedRowSet rowSet;
 14: 
 15:    // construct TitlesBean object 
 16:    public GuestDataBean() throws Exception
 17:    {
 18:       // load the MySQL driver
 19:       Class.forName( "com.mysql.jdbc.Driver" );
 20:       
 21:       // specify properties of CachedRowSet
 22:       rowSet = new CachedRowSetImpl();  
 23:       rowSet.setUrl( "jdbc:mysql://localhost/guestbook" ); 
 24:       rowSet.setUsername( "jhtp6" );
 25:       rowSet.setPassword( "jhtp6" );
 26: 
 27:           // obtain list of titles
 28:       rowSet.setCommand( 
 29:          "SELECT firstName, lastName, email FROM guests" );
 30:       rowSet.execute();
 31:    } // end GuestDataBean constructor
 32: 
 33:    // return an ArrayList of GuestBeans
 34:    public ArrayList< GuestBean > getGuestList() throws SQLException
 35:    {
 36:       ArrayList< GuestBean > guestList = new ArrayList< GuestBean >();
 37: 
 38:       rowSet.beforeFirst(); // move cursor before the first row
 39: 
 40:       // get row data
 41:       while ( rowSet.next() ) 
 42:       {
 43:          GuestBean guest = new GuestBean();
 44: 
 45:          guest.setFirstName( rowSet.getString( 1 ) );
 46:          guest.setLastName( rowSet.getString( 2 ) );
 47:          guest.setEmail( rowSet.getString( 3 ) );
 48: 
 49:          guestList.add( guest ); 
 50:       } // end while
 51: 
 52:       return guestList;
 53:    } // end method getGuestList
 54:    
 55:    // insert a guest in guestbook database
 56:    public void addGuest( GuestBean guest ) throws SQLException
 57:    {
 58:       rowSet.moveToInsertRow(); // move cursor to the insert row
 59: 
 60:       // update the three columns of the insert row 
 61:       rowSet.updateString( 1, guest.getFirstName() ); 
 62:       rowSet.updateString( 2, guest.getLastName() ); 
 63:       rowSet.updateString( 3, guest.getEmail() ); 
 64:       rowSet.insertRow(); // insert row to rowSet
 65:       rowSet.moveToCurrentRow(); // move cursor to the current row
 66:       rowSet.acceptChanges(); // propagate changes to database
 67:    } // end method addGuest
 68: } // end class GuestDataBean
 69: 
 70: 
 71:  /**************************************************************************
 72:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 73:  * Pearson Education, Inc. All Rights Reserved.                           *
 74:  *                                                                        *
 75:  * DISCLAIMER: The authors and publisher of this book have used their     *
 76:  * best efforts in preparing the book. These efforts include the          *
 77:  * development, research, and testing of the theories and programs        *
 78:  * to determine their effectiveness. The authors and publisher make       *
 79:  * no warranty of any kind, expressed or implied, with regard to these    *
 80:  * programs or to the documentation contained in these books. The authors *
 81:  * and publisher shall not be liable in any event for incidental or       *
 82:  * consequential damages in connection with, or arising out of, the       *
 83:  * furnishing, performance, or use of these programs.                     *
 84:  *************************************************************************/