• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterate through resultset one row at a time in java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a simple program that is also a swing application. It uses a database for data and an interface to put the data into individual text fields after a button is pressed. Right now I can print one row to the console, but after that I get nothing. The application has a start button, to begin the program, then as each button is pressed another data value appears in a text field. There is also one button to clear all the fields in order to display another row of data one field at a time.

Right now when I begin the program all the appropriate buttons come up to start the program but when I press the start button I get no data from the resultset. The program printed one row to the console like it is supposed to, but I get now other data.

I have literally read hundreds of questions but I can not find a solution to my particular problem.

When I use while (rs.next()) I get all the records printed to the console. I do not want this. I want to be able to print a row one field at a time. Here is my code:




Any help would be greatly appreciated.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you mean by "row field". Do you mean for a single row display each field one at a time? You could have your method return a List of fields.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come you are defining each variable twice, once as a class member variable (aka field), and again as a local variable. The way you have it the fields will never be populated.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Sean!

I see that your code prints data from every record, just like you said. But I don't understand what you want instead.

For example, what's a row? Is that the same as a record? And a field? Is that the same as a column from the database table?

And you say you want to see things one at a time. Would you want to see one thing each time you press the Start button or are there other buttons involved?
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're saying "if rs.next()" when I think you mean "while rs.next()".

But be aware that while looping/printing is fine for proving the retrieval logic, GUI systems like Swing don't just "write to the display". You'll have to capture what you read in a Model and Swing will display and refresh itself from that model.
 
Sean Torongeau
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Welcome to the Ranch, Sean!

I see that your code prints data from every record, just like you said. But I don't understand what you want instead.

For example, what's a row? Is that the same as a record? And a field? Is that the same as a column from the database table?

And you say you want to see things one at a time. Would you want to see one thing each time you press the Start button or are there other buttons involved?



Hi Paul, thanks for the welcome.

A row is a single record with fields (pieces of data) going across. While a column name is the field name (variable). So record 1 (going across) has ten variables, v1, v2, v3, etc.

So, I want to see each variable (field) one at a time starting with the press of the start button to show field v1 then another button to show v2 and another to show v3 etc. until I have shown all field (variables) in the record.

Finally, the last button starts the whole process over again (with the exception of the start button). This button clears all the fields and populates field v1 instead of the start button.
 
Sean Torongeau
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You're saying "if rs.next()" when I think you mean "while rs.next()".

But be aware that while looping/printing is fine for proving the retrieval logic, GUI systems like Swing don't just "write to the display". You'll have to capture what you read in a Model and Swing will display and refresh itself from that model.



Hi Tim,

When I use "while (rs.next()): all my records print to the console. That is 276 records printing out all at once. I want the printing and therefore the populating of fields to stop at one record. The 276 records is only a temporary database, the real one holds over 200,000 records.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Let's see if we can sort things out a bit.

For the confused, a "row" is a "record" in a database. SQL is conceptually a language for operating on tables (in the abstract) and tables have rows. The term "record" actually comes from the terminology we historically used for data items retrieved from a disk file.

So "row" is the proper term here, but if anyone wants to say "record", we'll understand the equivalence, I hope.

As I said, in a GUI, you can't just "print to the display". Swing is based on Model/View/Controller (MVC). One of the reasons why MVC is so popular is that it better supports an environment where windows can be moved on top of each other and moved around on the display. Since pixels have to be re-drawn when you do that, it's convenient to have a reservoir of information to do the re-drawing. The View defines what gets drawn in terms of graphic elements, the Model defines the actual data values that get rendered within those elements. The Controller updates the View when the Model data changes and conversely updates the Model when data is changed in a control in the View.

Normally, Views are made up of a tree of sub-Views, each of which may reference its own mini-model. And there are different types of Controller to handle the different types of sub-View. One for static text display, one for editable text, one for boolean data (checkboxes) and so forth. (sub)Views and (sub)Models are stateful. Controllers are usually stateless, so the same Controller can handle all static text displays in a View, for example. And as a final note, there doesn't have to be a 1-to-1 correspondence between Views and Models. My favorite example being where a spreadsheet and a chart share a Model so that changing sheet data changes the chart.

So, in short, to work properly in Swing, you click the "Read" button, The button fires an Action that reads one record, er, row. Note that you're going to have to have some sort of positional state because if you just open a Connection and read, the same row is going to be returned every time if ORDER BY is in effect. And Ganesha only knows which row you'll get without ORDER BY!

So now you've read in a row. You then need to populate a Model from that row. That may be actual a set of model items, for example strings associated with text display or edit controls. THEN you need to tell Swing to update the display! Again, you cannot just "write to the display". You set up the model and tell Swing. Swing does the actual display work on a thread of its own, not on your thread.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Torongeau wrote:When I use "while (rs.next()): all my records print to the console. That is 276 records printing out all at once. I want the printing and therefore the populating of fields to stop at one record. The 276 records is only a temporary database, the real one holds over 200,000 records.



Well, that's what's happening. You click Start and you see one record.

But that isn't what you said you wanted earlier. At least I don't think it was, my impression was that you wanted to see the records "one at a time". So it seems like you want to have some kind of gap between seeing Record 1 and Record 2, and so on. Is that right? If so, how should that gap work? Wait X seconds before showing the next record? Have the user click the Next button to see the next record?

Although... if you have hundreds of thousands of records, a design which shows them all to the user (whether all at once or one at a time) is basically impractical.
 
Sean Torongeau
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Sean Torongeau wrote:When I use "while (rs.next()): all my records print to the console. That is 276 records printing out all at once. I want the printing and therefore the populating of fields to stop at one record. The 276 records is only a temporary database, the real one holds over 200,000 records.



Well, that's what's happening. You click Start and you see one record.

But that isn't what you said you wanted earlier. At least I don't think it was, my impression was that you wanted to see the records "one at a time". So it seems like you want to have some kind of gap between seeing Record 1 and Record 2, and so on. Is that right? If so, how should that gap work? Wait X seconds before showing the next record? Have the user click the Next button to see the next record?

Although... if you have hundreds of thousands of records, a design which shows them all to the user (whether all at once or one at a time) is basically impractical.



Okay, now I get one record to print one field at a time after each button is pressed. Although the last button is supposed to clear all the fields and put a new variable (v1) in position. But I get a null error saying "this. rs is null".
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, you have two 'rs' variables, one a local variable and one a member variable. This is an accident waiting (or more likely already) happened.
 
Sean Torongeau
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:As I said, you have two 'rs' variables, one a local variable and one a member variable. This is an accident waiting (or more likely already) happened.



Now I fixed all the variables and can get one record to appear starting with the start button and going through the other three button. But, the last button is supposed to clear
all the fields and put a new variable into the first field. This is not happening. I get no errors now but the button does not work as needed. Any ideas? Thanks
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your updated code. Also, you never did show us the button code so we can only imagine what happens when you click on a button.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you've given us doesn't follow typical conventions for that type of query. In general you want to create a class that will hold a single row's worth of information where each database column corresponds to a class field. Then create a statement and then create a result set by executing the statement. Now  loop through the entire result set creating a list of class objects. Close the result set, and close the statement. And finally, return the entire list for the application to process.

I am suspicious that you are trying to access the result set outside of your method which means that resources probably aren't getting closed.
 
Sean Torongeau
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Please post your updated code. Also, you never did show us the button code so we can only imagine what happens when you click on a button.



Okay here is my updated code. the newWord button is the last button to be pressed in order to get a new variable.



Thanks for the help.
Sean
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only place you pull out individual fields from your ResultSet is in your doConnect() method. This gets back to what I said about trying to use 'rs' outside of the method.
 
Sean Torongeau
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The only place you pull out individual fields from your ResultSet is in your doConnect() method. This gets back to what I said about trying to use 'rs' outside of the method.



Okay, everything is working just fine. Thanks for all the help.
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic