A maior rede de estudos do Brasil

Grátis
142 pág.
Apostila Formação Programador Java

Pré-visualização | Página 24 de 25

+ member.getFirstName() + "','" + 
 member.getMiddleName() + "','" + 
 member.getLastName() + "','" + 
 member.getPhoneNumber() + "','" + 
 member.getAbility() + "')" ; 
 System.out.println( addrecord ); 
 rowInserted = insert.executeUpdate(addrecord); 
 if ( rowInserted != 1 ) { 
 throw new DBopException( 
 "Unable to add new member to database" ); 
 } 
 return; 
 } 
 
 synchronized static public void updateMember( 
 ClubMember member ) 
 throws SQLException, DBopException 
 { 
 Statement update = getConnection().createStatement(); 
 String changeRecord = "UPDATE " + 
Programação Aplicada - Java 135 
 
SOCIESC – Sociedade Educacional de Santa Catarina 
 
 ( dbOwner.length( ) > 0 ? dbOwner + "." : "" ) + 
 memberTable + " SET " + 
 "FNAME = '" + member.getFirstName() + "'," + 
 "MNAME = '" + member.getMiddleName() + "'," + 
 "LNAME = '" + member.getLastName() + "'," + 
 "PHONENUM = '" + member.getPhoneNumber() + "'," + 
 "ABILITY = '" + member.getAbility() + "'" + 
 "WHERE ID = " + member.getId(); 
 System.out.println( changeRecord ); 
 int changed = update.executeUpdate( changeRecord ); 
 if ( changed < 1 ) { 
 throw new DBopException( 
 "No changes made to databare" ); 
 } else if ( changed > 1 ) { 
 throw new DBopException( 
 "More than one member's data was affected: " 
 + changed + "records changed." ); 
 } 
 return; 
 } 
 
 synchronized static public ClubMember 
 getMemberByID ( int id ) 
 throws SQLException, DBopException, 
 BadDataException 
 { 
 String[] tables = { memberTable }; 
 ResultSet result = getAllColumns( tables, 
 "WHERE ID = " + id ); 
 if ( ! result.next() ) { 
 return null; 
 } 
 ClubMember member = new ClubMember(); 
 member.setId( result.getInt( "ID" ) ); 
 member.setFirstName( 
 result.getString( "FNAME" ).trim() ); 
 member.setMiddleName( 
 result.getString( "MNAME" ).trim() ); 
 member.setLastName( 
 result.getString( "LNAME" ).trim() ); 
 member.setPhoneNumber( 
Programação Aplicada - Java 136 
 
SOCIESC – Sociedade Educacional de Santa Catarina 
 
 result.getString( "PHONENUM" ).trim() ); 
 member.setAbility( 
 result.getString( "ABILITY" ).trim() ); 
 if ( result.next( ) ) { 
 throw new DBopException ( 
 "Duplicate Member ID: " + id ); 
 } 
 return member; 
 } 
 
 synchronized static public ClubMember getMemberByName ( 
 String firstName, String lastName ) 
 throws SQLException, DBopException, BadDataException 
 { 
 String[] tables = { memberTable }; 
 ResultSet result = getAllColumns( tables, 
 " WHERE FNAME = '" + firstName + 
 "' AND LNAME = '" + lastName + "'" ); 
 if ( ! result.next() ) { 
 return null; 
 } 
 ClubMember member = new ClubMember(); 
 member.setId( result.getInt( "ID" ) ); 
 member.setFirstName( 
 result.getString( "FNAME" ).trim() ); 
 member.setMiddleName( 
 result.getString( "MNAME" ).trim() ); 
 member.setLastName( 
 result.getString( "LNAME" ).trim() ); 
 member.setPhoneNumber( 
 result.getString( "PHONENUM" ).trim() ); 
 member.setAbility( 
 result.getString( "ABILITY" ).trim() ); 
 if ( result.next( ) ) { 
 throw new DBopException ( 
 "Duplicate member name: " 
 + firstName + " " + lastName ); 
 } 
 return member; 
 } 
 
Programação Aplicada - Java 137 
 
SOCIESC – Sociedade Educacional de Santa Catarina 
 
 
 synchronized static public Vector retrieveTrips( ) 
 throws DBopException 
 { 
 Vector trips = new Vector(); 
 try { 
 String[] tables = { tripTable }; 
 ResultSet result = getAllColumns( tables, 
 "ORDER BY ID" ); 
 while( result.next() ) { 
 SkiTrip trip = new SkiTrip(); 
 trip.setId( result.getInt( "ID" ) ); 
 trip.setWeek( result.getInt( "WEEK" ) ); 
 trip.setDay( result.getString( "DAY" ).trim() ); 
 trip.setCapacity( result.getInt( "CAPACITY" ) ); 
 trip.setBooked( result.getInt( "BOOKED" ) ); 
 trips.addElement(trip); 
 } 
 } catch ( SQLException e ) { 
 throw new DBopException ( 
 "Error reading Table: " + tripTable); 
 } 
 return trips; 
 } 
 
 
 synchronized static public Vector getSkierTrips( 
 int skierID ) 
 throws DBopException 
 { 
 String[] tables = { tripTable, bookTable }; 
 String condition = "WHERE " + 
 ( dbOwner.length( ) > 0 ? dbOwner + "." : "" ) + 
 tripTable + "." + "ID" + "=" + 
 ( dbOwner.length( ) > 0 ? dbOwner + "." : "" ) + 
 bookTable + "." + "TRIPID" + 
 " AND " + 
 ( dbOwner.length( ) > 0 ? dbOwner + "." : "" ) + 
 bookTable + "." + "MEMBERID=" + skierID; 
 Vector trips = new Vector(); 
 try { 
Programação Aplicada - Java 138 
 
SOCIESC – Sociedade Educacional de Santa Catarina 
 
 ResultSet result = getAllColumns( 
 tables, condition ); 
 while( result.next() ) { 
 SkiTrip trip = new SkiTrip(); 
 trip.setId( result.getInt( "ID" ) ); 
 trip.setWeek( result.getInt( "WEEK" ) ); 
 trip.setDay( result.getString( "DAY" ).trim() ); 
 trip.setCapacity( result.getInt( "CAPACITY" ) ); 
 trip.setBooked( result.getInt( "BOOKED" ) ); 
 trips.addElement(trip); 
 } 
 } catch ( SQLException e ) { 
 throw new DBopException ( 
 "Error reading Table: " + tripTable); 
 } 
 return trips; 
 } 
 
 
 synchronized static public Vector retrieveBookings( 
 int skierID ) 
 throws DBopException 
 { 
 Vector trips = new Vector(); 
 try { 
 String[] tables = { bookTable }; 
 String[] columns = { "TRIPID" }; 
 ResultSet result = getSelectedColumns( columns, 
 tables, " WHERE MEMBERID = " + skierID + 
 " ORDER BY TRIPID" ); 
 while( result.next() ) { 
 trips.addElement( new Integer 
 ( result.getInt( "TRIPID" ) ) ); 
 } 
 } catch ( SQLException e ) { 
 throw new DBopException ( 
 "Error reading Table: " + bookTable); 
 } 
 return trips; 
 } 
 
Programação Aplicada - Java 139 
 
SOCIESC – Sociedade Educacional de Santa Catarina 
 
 
 synchronized public static void updateTrip( 
 SkiTrip trip ) 
 throws DBopException, SQLException 
 { 
 Statement update = 
 getConnection().createStatement(); 
 String doChange = "UPDATE " + 
 ( dbOwner.length( ) > 0 ? dbOwner + "." : "" ) + 
 tripTable + " SET " + 
 "BOOKED = " + trip.getBooked() + 
 " WHERE ID = " + trip.getId(); 
 System.out.println( doChange ); 
 int changed = update.executeUpdate( doChange ); 
 if ( changed != 1 ) { 
 throw new DBopException( 
 "Error updating booked count for trip " 
 + trip.getId() ); 
 } 
 return; 
 } 
 
 
 synchronized public static void addBooking( 
 Booking booking) 
 throws