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);
}
}
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
rs.next() returns true if the next row exists.