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() .
source share