Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java database connection retrieval

Tags:

java

swing

jdbc

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);
            }

    }   
like image 631
Yoosuf Avatar asked Feb 27 '23 18:02

Yoosuf


1 Answers

is it

if(rs.first()) ??

try

while (rs.next())

This will help u to iterate through the resultset.

like image 125
Prem Rajendran Avatar answered Mar 01 '23 10:03

Prem Rajendran