• 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

Exception in SQL getInt(....)

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Java-Professionals,

i,ve got a little code:



Please could you tell me why the exception is thrown. The only thing I know that it has something to do with the getInt(....) method and the query.

Many thanks for your advice and best regards,
Thomas
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does getInt() accept a string? I know that it accepts a whole number value as the column index as stated here
https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html#getInt-int- so this would work because we are passing the index of the value to getInt.
This does suggest that the ID column has an index of 1.

However the API documentation states:

API Docs wrote:The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row.
Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient.
Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.


Perhaps you need to first use findColumn to get the index of the column as noted here https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html#findColumn-java.lang.String-.
Then use getInt() with the index found using findColumn().

What is the error message/exception message that you are getting aside from "Error"?
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Thomas.

I hope that you will find everyone welcoming and helpful. I know that there are some people on this site are very well versed in Java and who are more then willing to help out.

By the way, you may find this resource helpful https://coderanch.com/wiki/659976/Ocpjp-Wall-Fame.
From that resource you can see what resources others used and how they prepared. This could save you time, money and frustration.
 
Rancher
Posts: 4801
50
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When catching an Exception (in this case a SQLException) it is advisable to do:

if you are simply logging things to System.out.
That will show you the exact error, and include the stack trace showing you where exactly it occurred.

In this case, the error will be something along the lines of "Before start of ResultSet".
This is because a ResultSet is always returned with the cursor before the first row.
You need to call ResultSet.next() to get it to read the row, so you can then use the various getXXX() methods.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas: Welcome to CodeRanch. Dave is correct as to the cause. This is something that they will try to trick you with if you are taking the cert exam.

Pete: Using column names is often good practice as it makes the code clearer and more resistant to change. It's also good to list the columns in the query rather than select *. Both for readability and performance (you probably don't need them all)
 
Thomas Carpenter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ladies and Gentlemen,

thank you for your heartly welcome and your competent replies!
No i understand this piece of code.
It's a really diabolic trick, hope I will pass my exam!.

Thanks a lot and best regards,
Thomas
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thomas Carpenter wrote:. . . It's a really diabolic trick . . .

. . . but, as Jeanne has hinted, that sort of thing happens in the exam. You must be prepared for that sort of thing.

And welcome to the Ranch (again).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic