Recover Java Database Connection

I use the following code to display records from my database in the form of a swing. However, when I click on the button, it shows only the first database record (the table in the 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); } } 
+4
source share
4 answers

A JDBC ResultSet object can be selected to indicate the location before the start of the result line of the query being executed. the first rs.next () points to the first returned string. Subsequent calls to rs.next () move it forward in the resulting lines. Therefore, 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 line exists.

+1
source

this is

 if(rs.first()) ?? 

to try

 while (rs.next()) 

This will help iterate over the result set.

+3
source

Well, you really wrote the code that way. Here is the Javadoc ResultSet#first() link:

Moves the cursor to the first row in this ResultSet.

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

You see, all this moves the cursor to the first line. Nothing more.

Basically you need to replace this with ResultSet#next() in a while . Then it will go through all the lines as long as the line exists.

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

But there is another problem. If you make only this change, then the code will be displayed only for the data of the last row, since each time you reuse the same text components for this. You want to display each line separately in the new fields of your user interface. I do not do Swing, so I cannot give a detailed answer from above, but at least I know that you would like to use JTable for this.

See also:


However, there are other problems in your code.

  • Calling Class#forName() not needed every time. Only once during application launch is enough.
  • Not calling close() for each of the ResultSet , Statement and Connection inside the finally block will result in a resource leak. If you continue to do this for a long time, the database will end the connection sooner or later, and your application will no longer be able to connect and break it. Correct it accordingly.
  • Just ex printing does not provide decent information. It will print only the type of exception and the message, which is not very useful for debugging. Instead, you want to do ex.printStackTrace() .
+3
source

if (rs.first ())

This checks if you find the first record in the result set.

+1
source

Source: https://habr.com/ru/post/1313994/


All Articles