Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Empty ResultSet Calls While Loop

 
Ranch Hand
Posts: 1940
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using java sql

When I have an empty resultset(record count = 0)

The while(rs.next()) loop is still being called and looping thru

 
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you checking record count, and how are determining that it went through the loop? How many times did it go through the loop? Do you have debugging print statements, and if so, can you post them also?

I'm assuming that your SerialNumberV8 constructor does not call rs.next().
 
Steve Dyke
Ranch Hand
Posts: 1940
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Where are you checking record count, and how are determining that it went through the loop? How many times did it go through the loop? Do you have debugging print statements, and if so, can you post them also?

I'm assuming that your SerialNumberV8 constructor does not call rs.next().



First, answer to second question: No

Here is the console out:

[8/5/19 15:18:40:519 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): Step1a: 881805-401+
[8/5/19 15:18:40:520 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): sql: Select t1.recseq, t1.partno, t1.userfd3, Case When (userfd3 > 0 And t2.serialno Is Not NULL) Then 'S' When (userfd3 > 0 And t2.serialno Is NULL) Then 'M' Else 'N' End As dsstatus From webprddt1.tsotagtbl t1 Left Join webprddt1.qmskcitmmr t2 On t2.serialno = t1.recseq Where t1.cono = '1259602' And t1.partno = '881805-401+'
[8/5/19 15:18:40:535 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): Step1b: 007614888
[8/5/19 15:18:40:535 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): Step1c:
[8/5/19 15:18:40:541 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): Step2:
[8/5/19 15:18:40:541 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): Step3: 1
[8/5/19 15:18:40:542 CDT] 00000099 SystemOut     O SerialNumberRequirement(2): serialNumbersCreated: 1

Note: the colineitemV8.getCdaitx() value is 881805-401+
Note The value at this line: SerialNumberRequirement(2): Step1b: 007614888. This is actually a record in the database with the correct CO# but the partno value is 881805-429+
Also is I run the SQL Statement(console line item#2 independently I get zero records)
 
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran your posted code snippet through a formatter and something seems off.  I would expect a compile error at the line with rs.close() and stmt.close() since both rs and stmt would be out of scope at that point.

Also, you are creating a new SerialNumberV8 object using a reference to a ResultSet.  What are you doing with the reference?  Are you storing it?
 
Rancher
Posts: 4177
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code that has the output statements in it?
That way we can see exactly what's going on, and how the output you have shown ties up with the code.

A call to next on an empty result set will never return true, so something else is going on.

What happens when you run that query in the database directly?

Also, that query ought to be a PreparedStatement, and the cono and partno variables bound in.
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Also, that query ought to be a PreparedStatement, and the cono and partno variables bound in.


This. You now have SQL injection waiting to happen.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!