-1

How to Retrieve Records from MySQL database on click of two button?

  • If user clicks on NEXT button, it should display the row content of first/next record only on various JLabel.
  • If user clicks on PREVIOUS button , it should display the previous record only.
  • If user reaches the last record , NEXT Button should be disabled.
  • Similarly if user is at first record then PREVIOUS button should be disabled. It should only be enabled when user can move to previous record.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Lets Code
  • 723
  • 8
  • 15
  • i just want a logic how to move up and down dynamically in mysql database. Code is too long and its divided in modules. – Lets Code Jul 13 '13 at 21:19
  • Question is fine as well as , as soon as i founded the solution i have posted the relevant code too. Similar question were asked before too but there solution were too complex. – Lets Code Jul 15 '13 at 06:59

3 Answers3

0
  1. I think what you are trying to do is to implement paging. I'm not into swing, but if you search probably there are some already implemented controls in swing offering paging. Check this link .
  2. Second step is retrieving information from the database. I believe that you need to write a stored procedure -> this is something like a method with parameters on DB level that will return only the records needed to be shown on the current page. You should make the stored procedure to accept @pageSize and @pageIndex parametersYou can read about paging stored procedures here.

I'm have not done this in swing, I've done it with ASP.NET and SQL Management Studio that's why I'm posting just the concept with links. Hope this information is helpful.

Community
  • 1
  • 1
Anton Belev
  • 11,963
  • 22
  • 70
  • 111
  • Thank you its helpful but i have seen a code long back which i am unable to recall. Only by using ResultSet object, which we get after the mysql query is successfully executed , we can achieve this functionality but i am unable to get that. ResultSet object has various functions but i am unable to develop logic how to use them to achieve the required functionality. – Lets Code Jul 13 '13 at 21:27
0

you must implement the behaviour by yourself, by using ResultSet statement methods, in this case, next() and previous() methods

the methods retruns a boolean values that indicates whether there is a next or previous record, you can use it to enable and disable your buttons

look this link for how to use a JDBC statement

http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

assuming that nextBtn and PreviousBtn are your and your Bouttons and res your ResultSet, we need two variable (hasNext and hasPrevious) to save the state of our buttons, you may refer to the snippet below that you must adapt to your case, it will not work directly .

private boolean hasNext = false;
private boolean hasPrevious = false;

public void updateBtnState() {

     nextBtn.setEnabled(hasNext); 
     previousBtn.setEnabled(hasPrevious);

 }


previousBtn = new JButton(" Previous <<");
                previousBtn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                    hasPrevious = res.previous();
                                            if (hasPrevious ) hasNext = true;
                        // do something 
                        updateBtnState();

                    }
                });

nextBtn = new JButton(" Next >>");
nextBtn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        hasNext = res.next();
                                                    if (hasNext ) hasPrevious = true;
                        // do something 
                        updateBtnState();

                    }
                });
Ait Yahia Idir
  • 370
  • 4
  • 18
0

SOLUTION IS :-

public void actionPerformed(ActionEvent evt) {

   //HERE next and pre are Button name;
   //rs is REsultSet
  if (evt.getSource()==next) 
    {

        try {

            if (rs.next()) 
            {
                if(rs.isLast())
                { 
                    pre.setEnabled(true);
                    next.setEnabled(false);
                }
                //do action

            }





        } catch (SQLException ex) {
            Logger.getLogger(DeleteQ.class.getName()).log(Level.SEVERE, null, ex);
        }

    } 

    else if (evt.getSource() == pre)
    {

       try {


           if(rs.previous())
            {
                if(rs.isFirst())
                {
                    pre.setEnabled(false);
                    next.setEnabled(true);
                }
               //do action


            }




        } catch (SQLException ex) 
        {
            Logger.getLogger(DeleteQ.class.getName()).log(Level.SEVERE, null, ex);
        }

}
}
Lets Code
  • 723
  • 8
  • 15