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 11 of 11

Thread: java code for Next button

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java code for Next button

    hay
    i am new in java world, please guide me , i have made a phone directory and i have also maintained a database for this. i want on clicking the next button each row from database table display one by one on the GUI into their relevant text Fields. i have written this code , this code show the last row of the table on single click on next button. i want one by one row on each next button click. please if u provide me little bit solution

    private void next_contactActionPerformed(java.awt.event.ActionE vent evt) {

    try{
    String Url = "Jdbc:OdbcirDSN";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection(Url,"sa","mysql");

    String sql = "SELECT * FROM PersonInformation";
    PreparedStatement pstmt = con.prepareStatement(sql,
    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR _UPDATABLE);
    ResultSet rs = pstmt.executeQuery();

    while(rs.next())

    {

    String name = rs.getString("First_Name");
    String lName = rs.getString("Last_Name");
    String add = rs.getString ("Address");
    String em = rs.getString("Email_Id");
    String cell = rs.getString("cell#");
    jTextField2.setText(name);
    jTextField3.setText(lName);
    jTextField4.setText(add);
    jTextField5.setText(em);
    jTextField10.setText(cell);


    }
    con.close();
    }

    catch(Exception sqlEx)
    {

    System.out.println(sqlEx);


    }
    finally{

    }


    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: java code for Next button

    Which part of this is giving you trouble? What have you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java code for Next button

    i facing this trouble , when i click the next button , i want each time new record from the phone directory and display into text fields ,but now i am getting only the last record, and can click the next button only one time

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: java code for Next button

    Well, you're looping through your results (every row) and setting the text of your components to the values in those rows. When the loop completes, your components will be showing the values of the last row.

    Instead, you either need to save your results to an ArrayList, or only go to the next result when the user clicks a button.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java code for Next button

    please tell me how i save the state of next button. so that when i click it 2nd time or so on, it behave according to that and give me row 2nd , 3rd and so on

    --- Update ---

    i have also tried rs.next() ,it gives the first record of phone directory on single next click. but when i click the next button , it does not work for 2nd or third row on each click

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: java code for Next button

    What exactly do you mean when you say it doesn't work?

    Please post an MCVE that shows what you've tried.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java code for Next button

    ScreenShot006.jpg
    this is GUi, here is next button , that when i click , it fetches the row from table,and display on text fields. so problem is only with next button. i want each time on single click on next fetches the row from table and display on text fields , so for this i have written the above mention code.
    so now when i click the next button i get the only last row from the table into text fields and when i use rs.next() instead of while(rs.next())i get only first row.

  8. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java code for Next button

    kevinworkman you have said"you either need to save your results to an ArrayList" how i maintain this ArrayList for this, and how it will work?

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: java code for Next button

    An MCVE is a small code example, not a screenshot of a gui.

    I'm not sure what you're asking. You would create an Object that contains the data for a particular row, then create an instance of that Object for each row, then add each instance to an ArrayList. This will only work if your entire result set will fit in memory though.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,285
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: java code for Next button

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: java code for Next button

    Some pseudo code:
    if first time {
       fill ArrayList with records from DB
       set nextRecPtr to 0
    }
    if nextRecPtr past end -> return null to show no more records
    get record at nextRecPtr from ArrayList
    incr nextRecPtr 
    return record
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java code for handle 'next' button
    By ars42025 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 23rd, 2013, 12:25 PM
  2. Code help with reset button error
    By Razorfc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2012, 02:20 PM
  3. Replies: 2
    Last Post: August 12th, 2011, 06:59 PM
  4. Lock up code to the click of a button!!!
    By Allen Walker in forum Java Theory & Questions
    Replies: 2
    Last Post: July 1st, 2011, 10:12 AM
  5. need to add a Double Button to the code
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 5th, 2010, 12:19 PM

Tags for this Thread