wood burning stoves*
The moose likes JDBC and the fly likes getXXX(int i) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "getXXX(int i) method" Watch "getXXX(int i) method" New topic
Author

getXXX(int i) method

jlrober
Greenhorn

Joined: Jun 25, 2001
Posts: 10
in a previous posting it was brought to my attention that the getString(1) method may be called only once on a value with out throwing an "NoDataFound" exception when using certain databases. Has anyone else encountered this problem? If so which type of database?
My experience is limited to Oracle 8 and jdbc 2. the preceding combination allows me to call the getXXX() method on the same value until the cows come home!
the original post was http://www.javaranch.com/ubb/Forum3/HTML/001177.html
Jamie
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"jlrober",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

It is generally recommended that you call the getXXX() method only once for the current position of the resultset. Even if you do not find any problems now, you will face it when you change your database. At that time you may need to change your code just because of this. Also it is recommended that you retrieve fields in the correct order like getXXX(1) then getXXX(2) .. and so on. Also always try to use column names rather than the column number. This is because when you install the application to the actual machine and create the tables, you are restricting yourself to create the fields in the same order. Also in future if you remove a field in the middle then the field order will change again. In this case, you have to change your code again.

Hence as a general rule, call the getXXX() method only once and call the fields by the name in the correct order. I hope this will help in using the application for multiple databases and will have better portability.

Hope you understand it.
Originally posted by jlrober:
in a previous posting it was brought to my attention that the getString(1) method may be called only once on a value with out throwing an "NoDataFound" exception when using certain databases. Has anyone else encountered this problem? If so which type of database?
My experience is limited to Oracle 8 and jdbc 2. the preceding combination allows me to call the getXXX() method on the same value until the cows come home!
the original post was http://www.javaranch.com/ubb/Forum3/HTML/001177.html
Jamie

MIchael Kmiec
Greenhorn

Joined: Jun 05, 2001
Posts: 9

Originally posted by Mohamed Yousuff:

Also always try to use column names rather than the column number. This is because when you install the application to the actual machine and create the tables, you are restricting yourself to create the fields in the same order. Also in future if you remove a field in the middle then the field order will change again. In this case, you have to change your code again.

One thing to keep in mind when referencing columns with the getXXXX( String columnName ) syntax - when you build your SQL statement (or PreparedStatement or whatever) that you reference the column names explicity. Instead of:
SELECT * FROM MY_TABLE
Use:
SELECT COL_NAME_ONE, COL_NAME_TWO FROM MY_TABLE
This actually serves two purposes: 1.) It's easier to maintain the SQL and the getXXXX() calls in parallel; and 2.) It's more efficient to perform SELECTs in this manner - the database doesn't work as hard, and why return information you may not use?

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getXXX(int i) method
 
Similar Threads
how to connect servlet with mssql
Oracle JDBC Connectivity problem
Number of tables in a database
SQLException: No Listener
MSSQL - Date