Reputation: 892
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);
}
}
Upvotes: 4
Views: 684
Reputation: 12358
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.
Upvotes: 1
Reputation: 1108557
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.
That said, there are other problems in your code.
Class#forName()
everytime is unnecessary. Just once during application's startup is enough.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.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.Upvotes: 3
Reputation: 726
is it
if(rs.first()) ??
try
while (rs.next())
This will help u to iterate through the resultset.
Upvotes: 3
Reputation:
if(rs.first())
That checks if you're on the first record in the resultset
Upvotes: 1