tags:

views:

61

answers:

4

I am using the code below to display the records from my database in the swing form. However, when I click on the button it only shows the first record of the database (table in database has 5 records)

private void btnnextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:StudentDetails");
            Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet rs=stm.executeQuery("Select * from NurseryStDetails");

                if(rs.first())
                {
                    txtstID.setText(rs.getString(1));
                    txtname.setText(rs.getString(2));
                    txtaddress.setText(rs.getString(3));
//                    int i=rs.getInt(4);
//                    String s=String.valueOf(i);
//                    txtage.setText(rs.getString(s));
//                    int j=rs.getInt(5);
//                    String t=String.valueOf(j);
                    txtage.setText(rs.getString(4));
                    txttelephone.setText(rs.getString(5));
                    txtgNIC.setText(rs.getString(6));
                    txtgname.setText(rs.getString(7));
                }
            }
        catch(Exception ex)
            {
                System.out.println("Exception caught"+ex);
            }

    }   
+3  A: 

is it

if(rs.first()) ??

try

while (rs.next())

This will help u to iterate through the resultset.

prem
no then it is displayin only the last record of the table. previously it shwd the first record only
Yoosuf
You are updating the same field again and again... and hence it shows only the last value.May be you need to show array of textfields for your requirement. You can dynamically create textfields inside while loop or store the result values in array and pass it over to swing client where you have array of text fields
prem
+1  A: 

if(rs.first())

That checks if you're on the first record in the resultset

Rob Cooney
+4  A: 

Well, you've indeed written the code that way. Here's a cite of the Javadoc of ResultSet#first():

Moves the cursor to the first row in this ResultSet object.

Returns: true if the cursor is on a valid row; false if there are no rows in the result set.

You see, all it does it moving the cursor to the first row. Nothing more.

You basically need to replace this by ResultSet#next() in a while loop. It will then iterate through all rows as long as there's a row.

while (rs.next()) {
    // ...
}

But there's another problem. If you make only that change, then the code would end up to display only the data of the last row since you're reusing the same text components for that everytime. You want to display each row separately in new fields in your UI. I don't do Swing, so I can't give a detailed answer from top of my head, but I at least know that you would like use to use JTable for this.

See also:


That said, there are other problems in your code.

  1. Calling Class#forName() everytime is unnecessary. Just once during application's startup is enough.
  2. Not calling close() on each of ResultSet, Statement and Connection inside a finally block will cause a resource leak. If you continue running this for a long term, the DB will run out of connections sooner or later and your application will not be able to connect it anymore and break. Fix it accordingly.
  3. Just printing the ex doesn't give worthy information. It'll only print the exception type and message which isn't very helpful during debugging. You'd like to do ex.printStackTrace() instead.
BalusC
+1  A: 

JDBC ResultSet object can be taken to be pointing to a location before the start of the result rows of the query being executed. the first rs.next() makes it point to the first row returned.subsequent calls to rs.next() moves it forwards in the resultant rows. Hence to display all the results use the concept

while(rs.next()){
  //use rs to get the details of the current row.
}

rs.next() returns true if the next row exists.

frictionlesspulley