Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: ResultSet and variable to be define.

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ResultSet and variable to be define.

    Hello everybody

    I have problem with my variables.
    The data's of my table enfts are :

    1234 Eric 1/1/2000
    1234 Patrick 23/4/2010

    When 1234 is selected then Eric and Patrick appair in my Combobox cboChildName.


    Eric appairs first end Patrick at the second place and that's OK.
    But the birthdate of Eric is 23/4/2010 instead of 1/1/2000. It's appairs in a texfield TxtChildBirth.

    id, firstname et naiss are correct

    What's the problem please.

    Habiler

        private void cboNatureActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
       if (cboNature.getSelectedItem().toString().equalsIgnoreCase("412")){
     
                   jPanel2.setVisible(true);
       conn = ConnectDecisions.ConnectDB();
       int MatrSelect = Integer.parseInt(txtMatricule.getText());
     
       try {
       PreparedStatement stmt = conn.prepareStatement("select EMPLOYEENBR,CHILDNAME,BIRTHDATE from Enfts where EMPLOYEENBR = '"+ MatrSelect+"'");
       ResultSet rs = stmt.executeQuery();
     
      System.out.println("SQL Statement:\n\t" + MatrSelect);
     
                         while(rs.next()) {
               			String id = rs.getString("EMPLOYEENBR");
    				String firstName = rs.getString("CHILDNAME");
    				String Naiss = rs.getString("BIRTHDATE");
    				System.out.println("ID: " + id + ", First Name: " + firstName + ",BIRTHDATE: " + Naiss);            
                         cboChildName.addItem(firstName);
                         txtChildBirth.setText(Naiss); 
                } 
                       } catch (SQLException ex) {
                           Logger.getLogger(AddData.class.getName()).log(Level.SEVERE, null, ex);
                       }
       }
    else {
      jPanel2.setVisible(false);
    }
     
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    What is printed by the println statement inside of the while loop?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.

    1234 Eric 1/1/2000
    1234 Patrick 23/4/2010

    That's OK.

    But cboChildName.addItem(firstName); Eric
    txtChildBirth.setText(Naiss); 24/4/2010

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    What is printed by this statement when it is executed:
    		System.out.println("ID: " + id + ", First Name: " + firstName + ",BIRTHDATE: " + Naiss);
    I expect to see each printed line start with the String "ID: "
    Copy what was printed and paste it here. Do not type from memory.



    But cboChildName.addItem(firstName); Eric
    txtChildBirth.setText(Naiss); 24/4/2010
    I am sorry, I have no idea what that means

    The setText method will only show what was sent on the last call because each call to the method replaces what was there before.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.

    How can i put the right birthdate in txtChildBirth

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    1) read the right date as a String. But how does the code find the right date? How many dates are read in the while loop?
    2) call the setText method with that date

    What does the println statement print? Please copy that and paste it here.
    Note: Only the last value passed to the setText method will be shown. Previous values are replaced.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.


  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    Please copy the text and paste it here. It is not possible to copy text from an image to include in a response.

    The image does not have the names: Eric or Patrick or the dates you have posted.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.

    Sorry Norm

    SQL Statement:
    3478
    ID: 3478.0, First Name: HABILS JULIEN,BIRTHDATE: 1993-05-24 00:00:00.000000
    ID: 3478.0, First Name: HABILS THIBAULT,BIRTHDATE: 1996-06-27 00:00:00.000000

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    Where are the names and dates shown in post#3?
    Why are the names in post#9 different from those in post#3?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.

    iN POST #3 THERE ARE fakes data.

    Hello everybody

    I have problem with my variables.
    The data's of my table enfts are :

    1234 Eric 1/1/2000
    1234 Patrick 23/4/2010
    In reality

    3478 HABILS JULIEN 1993-05-24
    3478 HABILS THIBAULT 1996-06-27

    But cboChildName.addItem(firstName); HABILS THIBAULT
    txtChildBirth.setText(Naiss); 1996-06-27

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    3478 HABILS THIBAULT 1996-06-27

    But cboChildName.addItem(firstName); HABILS THIBAULT
    txtChildBirth.setText(Naiss); 1996-06-27
    That looks like the name and the date are from the same line of the print out.
    What do you want to be different?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.

    Sorry

    3478 HABILS JULIEN 1993-05-24
    3478 HABILS THIBAULT 1996-06-27

    But cboChildName.addItem(firstName); HABILS JULIEN
    txtChildBirth.setText(Naiss); 1996-06-27

    --- Update ---

    Sorry

    3478 HABILS JULIEN 1993-05-24
    3478 HABILS THIBAULT 1996-06-27

    But cboChildName.addItem(firstName); HABILS JULIEN
    txtChildBirth.setText(Naiss); 1996-06-27

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    What is cboChildName? Is it a list? If it is, there should be two items (the two names) in the list after the while loop executes.
    What is txtChildBirth? Is it a text field? It can only hold one value. After the loop, it will have the last value passed to it in the loop. Preceding values will be replaced.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ResultSet and variable to be define.

    Quote Originally Posted by Norm View Post
    What is cboChildName? Is it a list? If it is, there should be two items (the two names) in the list after the while loop executes.
    What is txtChildBirth? Is it a text field? It can only hold one value. After the loop, it will have the last value passed to it in the loop. Preceding values will be replaced.
    How can i store the righy value in this case the birthdate of Julien (24/5/1993) in txtChildBirth.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,120
    Thanks
    52
    Thanked 2,369 Times in 2,323 Posts

    Default Re: ResultSet and variable to be define.

    store the righy value in this case the birthdate of Julien (24/5/1993) in txtChildBirth.
    Look at the value of the name. If it is Julien, store the associated date in txtChildBirth field. Do not store other values in the text field after the correct one has been stored there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. resultset
    By takamine in forum JDBC & Databases
    Replies: 3
    Last Post: February 1st, 2013, 02:27 PM
  2. Best Database with a scrollable resultSet
    By Jeff.Steinkamp in forum JDBC & Databases
    Replies: 2
    Last Post: January 21st, 2013, 05:56 AM
  3. Required help about the resultset.last()
    By derick_ho in forum JDBC & Databases
    Replies: 1
    Last Post: July 16th, 2012, 09:21 AM
  4. ResultSet issues
    By _lithium_ in forum JDBC & Databases
    Replies: 3
    Last Post: March 4th, 2011, 02:20 PM
  5. ResultSet is Closed
    By ramayya4u in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 24th, 2009, 03:54 AM