?
Solved

JButton modification

Posted on 2011-10-22
10
Medium Priority
?
363 Views
Last Modified: 2012-05-12
Can someone help me with the JButton?  When I run the program the JButton is not showing up.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

public class PersonQueries
{
   private static final String URL = "jdbc:derby:AddressBook";
   private static final String USERNAME = "deitel";
   private static final String PASSWORD = "deitel";

   private Connection connection = null; // manages connection
   private PreparedStatement selectAllPeople = null;
   private PreparedStatement selectPeopleByLastName = null;
   private PreparedStatement insertNewPerson = null;
   
   // constructor
   public PersonQueries()throws Exception
   {
      try
      {
         Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            
                  connection =
            DriverManager.getConnection( URL, USERNAME, PASSWORD );

         // create query that selects all entries in the AddressBook
         selectAllPeople =
            connection.prepareStatement( "SELECT * FROM Addresses" );
         
         // create query that selects entries with a specific last name
         selectPeopleByLastName = connection.prepareStatement(
            "SELECT * FROM Addresses WHERE LastName = ?" );
         
         // create insert that adds a new entry into the database
         insertNewPerson = connection.prepareStatement(
            "INSERT INTO Addresses " +
            "( FirstName, LastName, Email, PhoneNumber ) " +
            "VALUES ( ?, ?, ?, ? )" );
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
         System.exit( 1 );
      } // end catch
            catch ( Exception exc )
            {
                  exc.printStackTrace();
            } // end catch
   } // end PersonQueries constructor
   
   // select all of the addresses in the database
   public List< Person > getAllPeople()
   {
      List< Person > results = null;
      ResultSet resultSet = null;
     
      try
      {
         // executeQuery returns ResultSet containing matching entries
         resultSet = selectAllPeople.executeQuery();
         results = new ArrayList< Person >();
         
         while ( resultSet.next() )
         {
            results.add( new Person(
               resultSet.getInt( "addressID" ),
               resultSet.getString( "firstName" ),
               resultSet.getString( "lastName" ),
               resultSet.getString( "email" ),
               resultSet.getString( "phoneNumber" ) ) );
         } // end while
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();        
      } // end catch
      finally
      {
         try
         {
            resultSet.close();
         } // end try
         catch ( SQLException sqlException )
         {
            sqlException.printStackTrace();        
            close();
         } // end catch
      } // end finally
     
      return results;
   } // end method getAllPeople
   
   // select person by last name  
   public List< Person > getPeopleByLastName( String name )
   {
      List< Person > results = null;
      ResultSet resultSet = null;

      try
      {
         selectPeopleByLastName.setString( 1, name ); // specify last name

         // executeQuery returns ResultSet containing matching entries
         resultSet = selectPeopleByLastName.executeQuery();

         results = new ArrayList< Person >();

         while ( resultSet.next() )
         {
            results.add( new Person( resultSet.getInt( "addressID" ),
               resultSet.getString( "firstName" ),
               resultSet.getString( "lastName" ),
               resultSet.getString( "email" ),
               resultSet.getString( "phoneNumber" ) ) );
         } // end while
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
      } // end catch
      finally
      {
         try
         {
            resultSet.close();
         } // end try
         catch ( SQLException sqlException )
         {
            sqlException.printStackTrace();        
            close();
         } // end catch
      } // end finally
     
      return results;
   } // end method getPeopleByName
   
   // add an entry
   public int addPerson(
      String fname, String lname, String email, String num )
   {
      int result = 0;
     
      // set parameters, then execute insertNewPerson
      try
      {
         insertNewPerson.setString( 1, fname );
         insertNewPerson.setString( 2, lname );
         insertNewPerson.setString( 3, email );
         insertNewPerson.setString( 4, num );

         // insert the new entry; returns # of rows updated
         result = insertNewPerson.executeUpdate();
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
         close();
      } // end catch
     
      return result;
   } // end method addPerson
     
      public void updatePerson( Person person )
                  throws Exception
      {
      String sql = "UPDATE persons SET firstName = ?, lastName = ?,email = ?, phoneNumber = ?, email = ? WHERE id = ?";
      Object[] parameters = new Object[] {
                  person.getAddressID(), person.getFirstName(),
                  person.getLastName(), person.getEmail(),
                  person.getPhoneNumber()
      };
     
   }  
   // close the database connection
   public void close()
   {
      try
      {
         connection.close();
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
      } // end catch
   } // end method close
} // end class PersonQueries


public class Person
{
   private int addressID;
   private String firstName;
   private String lastName;
   private String email;
   private String phoneNumber;

   // no-argument constructor
   public Person()
   {
   } // end no-argument Person constructor

   // constructor
   public Person( int id, String first, String last,
      String emailAddress, String phone )
   {
      setAddressID( id );
      setFirstName( first );
      setLastName( last );
      setEmail( emailAddress );
      setPhoneNumber( phone );
   } // end five-argument Person constructor

   // sets the addressID
   public void setAddressID( int id )
   {
      addressID = id;
   } // end method setAddressID

   // returns the addressID
   public int getAddressID()
   {
      return addressID;
   } // end method getAddressID
   
   // sets the firstName
   public void setFirstName( String first )
   {
      firstName = first;
   } // end method setFirstName

   // returns the first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName
   
   // sets the lastName
   public void setLastName( String last )
   {
      lastName = last;
   } // end method setLastName

   // returns the last name
   public String getLastName()
   {
      return lastName;
   } // end method getLastName
   
   // sets the email address
   public void setEmail( String emailAddress )
   {
      email = emailAddress;
   } // end method setEmail

   // returns the email address
   public String getEmail()
   {
      return email;
   } // end method getEmail
   
   // sets the phone number
   public void setPhoneNumber( String phone )
   {
      phoneNumber = phone;
   } // end method setPhoneNumber

   // returns the phone number
   public String getPhoneNumber()
   {
      return phoneNumber;
   } // end method getPhoneNumber
} // end class Person


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

public class AddressBookDisplay extends JFrame
{
   private Person currentEntry;
   private PersonQueries personQueries;
   private List< Person > results;  
   private int numberOfEntries = 0;
   private int currentEntryIndex;

   private JButton browseButton;
   private JLabel emailLabel;
   private JTextField emailTextField;
   private JLabel firstNameLabel;
   private JTextField firstNameTextField;
   private JLabel idLabel;
   private JTextField idTextField;
   private JTextField indexTextField;
   private JLabel lastNameLabel;
   private JTextField lastNameTextField;
   private JTextField maxTextField;
   private JButton nextButton;
   private JLabel ofLabel;
   private JLabel phoneLabel;
   private JTextField phoneTextField;
   private JButton previousButton;
   private JButton queryButton;
   private JLabel queryLabel;
   private JPanel queryPanel;
   private JPanel navigatePanel;
   private JPanel displayPanel;
   private JTextField queryTextField;
   private JButton insertButton;
      private JButton updateButton;
   
   // no-argument constructor
   public AddressBookDisplay()
   {
      super( "Address Book" );
     
      // establish database connection and set up PreparedStatements
      personQueries = new PersonQueries();
     
      // create GUI
      navigatePanel = new JPanel();
      previousButton = new JButton();
      indexTextField = new JTextField( 2 );
      ofLabel = new JLabel();
      maxTextField = new JTextField( 2 );
      nextButton = new JButton();
      displayPanel = new JPanel();
      idLabel = new JLabel();
      idTextField = new JTextField( 10 );
      firstNameLabel = new JLabel();
      firstNameTextField = new JTextField( 10 );
      lastNameLabel = new JLabel();
      lastNameTextField = new JTextField( 10 );
      emailLabel = new JLabel();
      emailTextField = new JTextField( 10 );
      phoneLabel = new JLabel();
      phoneTextField = new JTextField( 10 );
      queryPanel = new JPanel();
      queryLabel = new JLabel();
      queryTextField = new JTextField( 10 );
      queryButton = new JButton();
      browseButton = new JButton();
      insertButton = new JButton();
            updateButton = new JButton();

      setLayout( new FlowLayout( FlowLayout.CENTER, 10, 10 ) );
      setSize( 400, 300 );
      setResizable( false );

      navigatePanel.setLayout(
         new BoxLayout( navigatePanel, BoxLayout.X_AXIS ) );

      previousButton.setText( "Previous" );
      previousButton.setEnabled( false );
      previousButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               previousButtonActionPerformed( evt );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener

      navigatePanel.add( previousButton );
      navigatePanel.add( Box.createHorizontalStrut( 10 ) );

      indexTextField.setHorizontalAlignment(
         JTextField.CENTER );
      indexTextField.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               indexTextFieldActionPerformed( evt );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener

      navigatePanel.add( indexTextField );
      navigatePanel.add( Box.createHorizontalStrut( 10 ) );

      ofLabel.setText( "of" );
      navigatePanel.add( ofLabel );
      navigatePanel.add( Box.createHorizontalStrut( 10 ) );

      maxTextField.setHorizontalAlignment(
         JTextField.CENTER );
      maxTextField.setEditable( false );
      navigatePanel.add( maxTextField );
      navigatePanel.add( Box.createHorizontalStrut( 10 ) );

      nextButton.setText( "Next" );
      nextButton.setEnabled( false );
      nextButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               nextButtonActionPerformed( evt );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener

      navigatePanel.add( nextButton );
      add( navigatePanel );

      displayPanel.setLayout( new GridLayout( 5, 2, 4, 4 ) );

      idLabel.setText( "Address ID:" );
      displayPanel.add( idLabel );

      idTextField.setEditable( false );
      displayPanel.add( idTextField );

      firstNameLabel.setText( "First Name:" );
      displayPanel.add( firstNameLabel );
      displayPanel.add( firstNameTextField );

      lastNameLabel.setText( "Last Name:" );
      displayPanel.add( lastNameLabel );
      displayPanel.add( lastNameTextField );

      emailLabel.setText( "Email:" );
      displayPanel.add( emailLabel );
      displayPanel.add( emailTextField );

      phoneLabel.setText( "Phone Number:" );
      displayPanel.add( phoneLabel );
      displayPanel.add( phoneTextField );
      add( displayPanel );

      queryPanel.setLayout(
         new BoxLayout( queryPanel, BoxLayout.X_AXIS) );

      queryPanel.setBorder( BorderFactory.createTitledBorder(
         "Find an entry by last name" ) );
      queryLabel.setText( "Last Name:" );
      queryPanel.add( Box.createHorizontalStrut( 5 ) );
      queryPanel.add( queryLabel );
      queryPanel.add( Box.createHorizontalStrut( 10 ) );
      queryPanel.add( queryTextField );
      queryPanel.add( Box.createHorizontalStrut( 10 ) );

      queryButton.setText( "Find" );
      queryButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               queryButtonActionPerformed( evt );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener

      queryPanel.add( queryButton );
      queryPanel.add( Box.createHorizontalStrut( 5 ) );
      add( queryPanel );

      browseButton.setText( "Browse All Entries" );
      browseButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               browseButtonActionPerformed( evt );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener

      add( browseButton );

      insertButton.setText( "Insert New Entry" );
      insertButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               insertButtonActionPerformed( evt );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener

         add( insertButton );      
           
            updateButton.setText( "Update Current Entry" );
            updateButton.addActionListener(
                  new ActionListener()
                  {
                        public void actionPerformed( ActionEvent evt )
                        {
                              updateButtonActionPerformed( evt );
                        } // end method actionPerformed
                  } // end anonymous inner class
                  );// end call to ActionListener
           
            add( updateButton );
      addWindowListener(
         new WindowAdapter()
         {  
            public void windowClosing( WindowEvent evt )
            {
               personQueries.close(); // close database connection
               System.exit( 0 );
            } // end method windowClosing
         } // end anonymous inner class
      ); // end call to addWindowListener
     
      setVisible( true );
   } // end no-argument constructor

   // handles call when previousButton is clicked
   private void previousButtonActionPerformed( ActionEvent evt )
   {
      currentEntryIndex--;
     
      if ( currentEntryIndex < 0 )
         currentEntryIndex = numberOfEntries - 1;
     
      indexTextField.setText( "" + ( currentEntryIndex + 1 ) );
      indexTextFieldActionPerformed( evt );  
   } // end method previousButtonActionPerformed

   // handles call when nextButton is clicked
   private void nextButtonActionPerformed( ActionEvent evt )
   {
      currentEntryIndex++;
     
      if ( currentEntryIndex >= numberOfEntries )
         currentEntryIndex = 0;
     
      indexTextField.setText( "" + ( currentEntryIndex + 1 ) );
      indexTextFieldActionPerformed( evt );
   } // end method nextButtonActionPerformed

   // handles call when queryButton is clicked
   private void queryButtonActionPerformed( ActionEvent evt )
   {
      results =
         personQueries.getPeopleByLastName( queryTextField.getText() );
      numberOfEntries = results.size();
     
      if ( numberOfEntries != 0 )
      {
         currentEntryIndex = 0;
         currentEntry = results.get( currentEntryIndex );
         idTextField.setText("" + currentEntry.getAddressID() );
         firstNameTextField.setText( currentEntry.getFirstName() );
         lastNameTextField.setText( currentEntry.getLastName() );
         emailTextField.setText( currentEntry.getEmail() );
         phoneTextField.setText( currentEntry.getPhoneNumber() );
         maxTextField.setText( "" + numberOfEntries );
         indexTextField.setText( "" + ( currentEntryIndex + 1 ) );
         nextButton.setEnabled( true );
         previousButton.setEnabled( true );
      } // end if
      else
         browseButtonActionPerformed( evt );
   } // end method queryButtonActionPerformed

   // handles call when a new value is entered in indexTextField
   private void indexTextFieldActionPerformed( ActionEvent evt )
   {
      currentEntryIndex =
         ( Integer.parseInt( indexTextField.getText() ) - 1 );
     
      if ( numberOfEntries != 0 && currentEntryIndex < numberOfEntries )
      {
         currentEntry = results.get( currentEntryIndex );
         idTextField.setText("" + currentEntry.getAddressID() );
         firstNameTextField.setText( currentEntry.getFirstName() );
         lastNameTextField.setText( currentEntry.getLastName() );
         emailTextField.setText( currentEntry.getEmail() );
         phoneTextField.setText( currentEntry.getPhoneNumber() );
         maxTextField.setText( "" + numberOfEntries );
         indexTextField.setText( "" + ( currentEntryIndex + 1 ) );
      } // end if
    } // end method indexTextFieldActionPerformed

   // handles call when browseButton is clicked
   private void browseButtonActionPerformed( ActionEvent evt )
   {
      try
      {
         results = personQueries.getAllPeople();
         numberOfEntries = results.size();
     
         if ( numberOfEntries != 0 )
         {
            currentEntryIndex = 0;
            currentEntry = results.get( currentEntryIndex );
            idTextField.setText("" + currentEntry.getAddressID() );
            firstNameTextField.setText( currentEntry.getFirstName() );
            lastNameTextField.setText( currentEntry.getLastName() );
            emailTextField.setText( currentEntry.getEmail() );
            phoneTextField.setText( currentEntry.getPhoneNumber() );
            maxTextField.setText( "" + numberOfEntries );
            indexTextField.setText( "" + ( currentEntryIndex + 1 ) );
            nextButton.setEnabled( true );
            previousButton.setEnabled( true );
         } // end if
      } // end try
      catch ( Exception e )
      {
         e.printStackTrace();
      } // end catch
   } // end method browseButtonActionPerformed

   // handles call when insertButton is clicked
   private void insertButtonActionPerformed( ActionEvent evt )
   {
      int result = personQueries.addPerson( firstNameTextField.getText(),
         lastNameTextField.getText(), emailTextField.getText(),
         phoneTextField.getText() );
     
      if ( result == 1 )
         JOptionPane.showMessageDialog( this, "Person added!",
            "Person added", JOptionPane.PLAIN_MESSAGE );
      else
         JOptionPane.showMessageDialog( this, "Person not added!",
            "Error", JOptionPane.PLAIN_MESSAGE );
         
      browseButtonActionPerformed( evt );
   } // end method insertButtonActionPerformed
   
      private void updateButtonActionPerformed( ActionEvent evt )
      {
            int result = personQueries.addPerson( firstNameTextField.getText(),
         lastNameTextField.getText(), emailTextField.getText(),
         phoneTextField.getText() );
            if ( result == 1 )
                  JOptionPane.showMessageDialog( this, "Person updated!",
                        "Person updated", JOptionPane.PLAIN_MESSAGE );
            else
                  JOptionPane.showMessageDialog( this, "Person not updated!",
                        "Person not updated", JOptionPane.PLAIN_MESSAGE );
                       
            updateButtonActionPerformed( evt );
      } // end method updateButtonActionPerformed
   // main method
   public static void main( String args[] )
   {
      new AddressBookDisplay();
   } // end method main
} // end class AddressBookDisplay


address.sql
derby.log
Person.java
PersonQueries.java
0
Comment
Question by:assaultkitty
  • 6
  • 4
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37013026
Which button you want to see?
adrbook.PNG
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37013063
The above screen was generated using your code.
I oanly changed setSize arguments to (400,400) within
this fragment:

            updateButton = new JButton();

      setLayout( new FlowLayout( FlowLayout.CENTER, 10, 10 ) );
      setSize( 400, 400 );
      setResizable( false );

      navigatePanel.setLayout(
         new BoxLayout( navigatePanel, BoxLayout.X_AXIS ) );

Open in new window

0
 

Author Comment

by:assaultkitty
ID: 37013218
When I run it is running an exception.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37013224
Well the only execption I had initially was about lack of Derby driver;
to see the GUI I commented out this line

 //  personQueries = new PersonQueries();

I assume you do have derby driver.

Other than that I just corrected the size to see full picture

0
 

Author Comment

by:assaultkitty
ID: 37013247
I have the derby installed on my computer but I get that the driver cannot be found on the command prompt.  As you can see from, I should have it configured properly.  I went to the Apache derby site and downloaded it.  I was running yesterday. Can you help me get the command right in my code?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 2000 total points
ID: 37013302
Do you have deby jar file - I guess it should be named derbyclient.jar - look somewheer in
your project in Eclipse or maybe somewhere in plugin theer should be such file

maybe it is easier to download:
http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/derby/derbyclient/10.2.1.6/derbyclient-10.2.1.6.jar

and then put it in classpath in you command line

say something like that:

java -cp .\;.\derbyclient.jar  AddessBookQuery

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37013399

Great!
Thsi works for me
with jar file form here:

http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/derby/derby/10.5.3.0_1/derby-10.5.3.0_1.jar

It actually creates embedded  database and even inserts records there -
see my cahnges in PersonQueries - mind that I
added  create=trueand sql for   creatiing of the table there
and removed user password otherwise I neede to knwo how to create schema
and without user password it makes it in the defauilt schema I guess


In the main class I of couurse uncommented that line

  personQueries = new PersonQueries();

Other than that I didn't make any other changes  in main class


// Fig. 28.31: PersonQueries.java
// PreparedStatements used by the Address Book application

import java.sql.*;
import java.util.List;
import java.util.ArrayList;

public class PersonQueries
{
  // private static final String URL = "jdbc:derby:AddressBook";
 //private static String dbURL = "jdbc:derby://localhost:1527/AddressBook;create=true;user=me;password=mine";
    private static String URL = "jdbc:derby:AddressBook1;create=true";

   private static final String USERNAME = "deitel";
   private static final String PASSWORD = "deitel";

   private Connection connection = null; // manages connection
   private PreparedStatement selectAllPeople = null;
   private PreparedStatement selectPeopleByLastName = null;
   private PreparedStatement insertNewPerson = null;

   // constructor
   public PersonQueries()
   {
      try
      {
           Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
         connection =
            DriverManager.getConnection( URL);//, USERNAME, PASSWORD );

         // create query that selects all entries in the AddressBook

          Statement stmt = connection.createStatement();
          stmt.executeUpdate("create table addresses (AddressID int, FirstName varchar(25), LastName varchar(25), Email varchar(25), PhoneNumber varchar(25))");

         selectAllPeople =
            connection.prepareStatement( "SELECT * FROM Addresses" );

         // create query that selects entries with a specific last name
         selectPeopleByLastName = connection.prepareStatement(
            "SELECT * FROM Addresses WHERE LastName = ?" );

         // create insert that adds a new entry into the database
         insertNewPerson = connection.prepareStatement(
            "INSERT INTO Addresses " +
            "( FirstName, LastName, Email, PhoneNumber ) " +
            "VALUES ( ?, ?, ?, ? )" );
      } // end try
      catch ( Exception sqlException )
      {
         sqlException.printStackTrace();
         System.exit( 1 );
      } // end catch
   } // end PersonQueries constructor

   // select all of the addresses in the database
   public List< Person > getAllPeople()
   {
      List< Person > results = null;
      ResultSet resultSet = null;

      try
      {
         // executeQuery returns ResultSet containing matching entries
         resultSet = selectAllPeople.executeQuery();
         results = new ArrayList< Person >();

         while ( resultSet.next() )
         {
            results.add( new Person(
               resultSet.getInt( "addressID" ),
               resultSet.getString( "firstName" ),
               resultSet.getString( "lastName" ),
               resultSet.getString( "email" ),
               resultSet.getString( "phoneNumber" ) ) );
         } // end while
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
      } // end catch
      finally
      {
         try
         {
            resultSet.close();
         } // end try
         catch ( SQLException sqlException )
         {
            sqlException.printStackTrace();
            close();
         } // end catch
      } // end finally

      return results;
   } // end method getAllPeople

   // select person by last name
   public List< Person > getPeopleByLastName( String name )
   {
      List< Person > results = null;
      ResultSet resultSet = null;

      try
      {
         selectPeopleByLastName.setString( 1, name ); // specify last name

         // executeQuery returns ResultSet containing matching entries
         resultSet = selectPeopleByLastName.executeQuery();

         results = new ArrayList< Person >();

         while ( resultSet.next() )
         {
            results.add( new Person(
                 resultSet.getInt( "addressID" ),
               resultSet.getString( "firstName" ),
               resultSet.getString( "lastName" ),
               resultSet.getString( "email" ),
               resultSet.getString( "phoneNumber" ) ) );
         } // end while
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
      } // end catch
      finally
      {
         try
         {
            resultSet.close();
         } // end try
         catch ( SQLException sqlException )
         {
            sqlException.printStackTrace();
            close();
         } // end catch
      } // end finally

      return results;
   } // end method getPeopleByName

   // add an entry
   public int addPerson(
      String fname, String lname, String email, String num )
   {
      int result = 0;

      // set parameters, then execute insertNewPerson
      try
      {
         insertNewPerson.setString( 1, fname );
         insertNewPerson.setString( 2, lname );
         insertNewPerson.setString( 3, email );
         insertNewPerson.setString( 4, num );

         // insert the new entry; returns # of rows updated
         result = insertNewPerson.executeUpdate();
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
         close();
      } // end catch

      return result;
   } // end method addPerson

   // close the database connection
   public void close()
   {
      try
      {
         connection.close();
      } // end try
      catch ( SQLException sqlException )
      {
         sqlException.printStackTrace();
      } // end catch
   } // end method close
} // end class PersonQueries


/**************************************************************************
 * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

Open in new window

0
 

Author Comment

by:assaultkitty
ID: 37013438
One more thing I must set up in PersonQueries the option called deletePerson to delete current records and another JButton called Delete Entry in AddressBookDisplay.  
0
 

Author Comment

by:assaultkitty
ID: 37013659
I have a question I am using JGrasp.  Can I get it to run the derby.  It is not picking up the driver.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37013673


>One more thing I must set up in PersonQueries the option called deletePerson to delete current records and another JButton called Delete Entry in AddressBookDisplay.  

What is the problem with that? Add one more button and add sql command to delete the record - the same way as you added other buttions.

Don't know JGrasp, but there should be a way to add third party libraries


This is how to add library to JGrasp project:

http://www.jgrasp.org/jgrasp/howto1.htm

How To Change the Classpath for Java Programs

Go to Settings > PATH/CLASSPATH > Workspace from the control panel.

Select the CLASSPATH tab in the settings dialog if not already selected, then hit the New button.

In the New CLASSPATH / Doc Path dialog, click the Browse button for Path or JAR File and navigate to the JAR file or the folder containing the target Java package root, and click Choose.

(Optional) If you would like set the documentation path for the classes in the JAR or package folder, click the Browse button for Documentation Path, select the folder containing the associated javadoc documentation, and click Choose.

Finally, click OK on the New CLASSPATH / Doc Path dialog, and OK on the settings dialog.

 
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question