Raghvendra Rai
Raghvendra Rai

Reputation: 3

move rs.next() forward in jdbc application

i'm working on an online examinatiion software which connects to a Ms Sql server using a JDBC ODBC connection. I want to move the Result Set cursor to the next row and backwards. The connection works and the program can retrieve the fields from the database, so there's no issue with that.

The code that I have here is a on a button labeled "Submit". When you click this button it should move to the next row in the database and retrieve the data from that row. The data retrieved should be displayed in the "textarea". The problem is that when I click next it displays the last result?

class Abc extends JFrame implements ActionListener
{

    JLabel      l1, l2, l3, l4;
    JButton     b1, b2;
    JTextArea   j1, j2;
    JCheckBox   c1, c2, c3, c4;
    ButtonGroup group   = new ButtonGroup();

    JPanel      panel2;

    Abc ()
    {
        j1 = new JTextArea();
        j2 = new JTextArea();
        c1 = new JCheckBox();
        c2 = new JCheckBox();
        c3 = new JCheckBox();
        c4 = new JCheckBox();
        j1.setEditable(false);
        c1.setSelected(false);
        c2.setSelected(false);
        c3.setSelected(false);
        c4.setSelected(false);

        group.add(c1);
        group.add(c2);
        group.add(c3);
        group.add(c4);

        Connection conn = null;
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:xe", "system",
                    "java");
            System.out.println("Dc is obtained");

        }
        catch (Exception e2)
        {
            System.out.println("conne" + e2.getMessage());
        }
        try
        {
            String sq = "select * from questionans";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sq);
            System.out
                    .println("------------------------------------------");
            if (rs.next())
            {

                j1.setText(rs.getString(2));
                c1.setText(rs.getString(3));
                c2.setText(rs.getString(4));
                c3.setText(rs.getString(5));
                c4.setText(rs.getString(6));
            }
            System.out
                    .println("-------------------------------------------");
        }
        catch (Exception e1)
        {
            System.out.println("query" + e1.getMessage());
        }
        finally
        {
            try
            {
                conn.close();
            }
            catch (Exception e)
            {
                System.out.println("close conn" + e.getMessage());
            }
        }

        b1 = new JButton("REGISTER");
        b2 = new JButton("SUBMIT");
        panel2 = new JPanel(new GridLayout(4, 1));
        panel2.add(b1);
        panel2.add(j1);
        panel2.add(c1);
        panel2.add(c2);
        panel2.add(c3);
        panel2.add(c4);
        panel2.add(b2);

        add(panel2, BorderLayout.CENTER);
        b1.addActionListener(this);
        b2.addActionListener(this);
        setTitle("REGISTRATION");

    }

    public void actionPerformed (ActionEvent e)
    {
        Object source = e.getSource();
        if (source == b1)
        {
            System.out.println("you pressed button b1");
        }
        else if (source == b2)
        {

            Connection conn = null;

            try
            {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection(
                        "jdbc:oracle:thin:@localhost:1521:xe", "system",
                        "java");
                System.out.println("Dc is obtained");

            }
            catch (Exception e4)
            {
                System.out.println("conne" + e4.getMessage());
            }
            try
            {
                String sq = "select * from questionans";
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(sq);
                System.out
                        .println("------------------------------------------");

                while (rs.next())
                {

                    j1.setText(rs.getString(2));
                    c1.setText(rs.getString(3));
                    c2.setText(rs.getString(4));
                    c3.setText(rs.getString(5));
                    c4.setText(rs.getString(6));

                }

                System.out
                        .println("-------------------------------------------");
            }
            catch (Exception e3)
            {
                System.out.println("query" + e3.getMessage());
            }
            finally
            {
                try
                {
                    conn.close();
                }
                catch (Exception e6)
                {
                    System.out.println("close conn" + e6.getMessage());
                }
            }

            String s2 = "";
            if (c1.isSelected())
            {
                s2 = s2 + "" + c1.getText();
            }
            else if (c2.isSelected())
            {
                s2 = s2 + "" + c2.getText();
            }
            else if (c3.isSelected())
            {
                s2 = s2 + "" + c3.getText();
            }
            else if (c4.isSelected())
            {
                s2 = s2 + "" + c4.getText();
            }

            System.out.println("You clicked on " + s2);
        }
    }

}

class xyz
{
    public static void main (String args[])
    {
        Abc a = new Abc();
        a.setSize(1000, 1000);
        a.setVisible(true);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

Upvotes: 0

Views: 155

Answers (2)

Reimeus
Reimeus

Reputation: 159844

Every iteration of the ResultSet resets the text in the JTextArea component. You could do

jTextArea1.append(rs.getString(2) + System.getProperty("line.separator"));

Upvotes: 2

Jean-François Savard
Jean-François Savard

Reputation: 21004

It display the last result because you are looping over all result :

while (rs.next())
{
    j1.setText(rs.getString(2));
    c1.setText(rs.getString(3));
    c2.setText(rs.getString(4));
    c3.setText(rs.getString(5));
    c4.setText(rs.getString(6));
}

The ResultSet should be retrieved outside of the listener, declare these variable public to your class and initialize them in your constructor :

String sq = "select * from questionans";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sq);

Change that while statement to an if in your listener :

if(rs.next())
{
    j1.setText(rs.getString(2));
    c1.setText(rs.getString(3));
    c2.setText(rs.getString(4));
    c3.setText(rs.getString(5));
    c4.setText(rs.getString(6));
}

And each time you click on submit, it will display the next result of the ResultSet retrieved before.

This will also give better performance as the select statement won't be executed each time you click on submit.

Upvotes: 2

Related Questions