*
The moose likes JDBC and the fly likes problem in extracting values from ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem in extracting values from ResultSet " Watch "problem in extracting values from ResultSet " New topic
Author

problem in extracting values from ResultSet

honey singh
Ranch Hand

Joined: Apr 26, 2002
Posts: 44
I'm using the result set to extract data from the database..
now when I execute the query and take the result from one of the column of the table
String a=rs.getString("f_access");
and now without altering the resultset I again access the rest of the columns
System.out.println(rs.getString("trans_no"));
System.out.println(rs.getString("stud_id"));
System.out.println(rs.getString("f_name"));
System.out.println(rs.getString("f_size"));
System.out.println(rs.getString("trans_date"));
System.out.println(rs.getString("f_access"));
including the column "f_access" again...
but it gives me the following error
java.sql.SQLException: [Oracle][ODBC]Invalid column number <1>.
honey singh
Ranch Hand

Joined: Apr 26, 2002
Posts: 44
I'm writing more of my code to make myself clear
the structure of the table is
TRANS_NO NOT NULL VARCHAR2(4)
STUD_ID VARCHAR2(6)
F_PATH VARCHAR2(100)
F_NAME VARCHAR2(50)
F_FILE BLOB
F_SIZE VARCHAR2(10)
TRANS_DATE DATE
A_TYPE VARCHAR2(1)
F_ACCESS VARCHAR2(4000)
F_DUPLICATE NUMBER(3)
and the code is:--
cmd="select trans_no,stud_id,f_name,f_size,trans_date,f_access from transaction where a_type='S'";
rs=stmt.executeQuery(cmd);
rs.next();
String a=rs.getString("f_access");
System.out.println(a);///I checked the value here of a is "333"
if(a.equals("333"))
{
System.out.println(rs.getString("trans_no"));
System.out.println(rs.getString("stud_id"));
System.out.println(rs.getString("f_name"));
System.out.println(rs.getString("f_size"));
System.out.println(rs.getString("trans_date"));
System.out.println(rs.getString("f_access"));
}
and the excpetion is as given above....
now if I take out the step
********String a=rs.getString("f_access");*******
then it gives me the result...
thanx for the reply...
Piyush Daiya
Ranch Hand

Joined: Jun 13, 2002
Posts: 67
hi Bib,
cmd="select trans_no,stud_id,f_name,f_size,trans_date,f_access from transaction where a_type='S'";
rs=stmt.executeQuery(cmd);
rs.next();
String a=rs.getString("f_access");
System.out.println(a);///I checked the value here of a is "333"
if(a.equals("333"))
{
System.out.println(rs.getString("trans_no"));
System.out.println(rs.getString("stud_id"));
System.out.println(rs.getString("f_name"));
System.out.println(rs.getString("f_size"));
System.out.println(rs.getString("trans_date"));
System.out.println(rs.getString("f_access"));
}
and the excpetion is as given above....
now if I take out the step

Y dont u replace System.out.println(rs.getString("f_access")); with System.out.println(a);
You have already assigned rs.getString("f_access") to a, so why not use it.
Hope this helps.

Regards,
Piyush


"A scientist is not person who gives right answers but a person who asks right questions"
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Bib Bob"
Change your name to comply with the naming convention or your account will be locked.
"harmanjeet s" was unacceptable (but definitely the closest)
"IT Revolution" wasn't acceptable.
"Bib Bob" is not acceptable.
Read the naming policy. Your name must not be obviously fictitious and must be your first name, a space, then your last name.
It appears that the moderators are spending too much time telling you to change your name and this is restricting the time they have to answer yours and others questions. We're all volunteers around here.
We want to help you and we want to answer your questions, so how about cutting us some slack and following just about the only rule we have.
thanks,
Dave.
[ July 14, 2002: Message edited by: David O'Meara ]
Alex Sanchez
Greenhorn

Joined: Mar 21, 2002
Posts: 4
You know, from what I know you can only access a column from a ResultSet once. Can anybody confirm this? When I tryd to access the same column 2 times I get an exception the second time, even though the Resultset is still alive. I don't know if this is fixed with JDBC 3.0 (sdk 1.4.0).
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Alex, you are correct. A resultset's column can only be accessed once. At least as far as JDBC 2.0 is concerned.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Bosun Bello:
Alex, you are correct. A resultset's column can only be accessed once. At least as far as JDBC 2.0 is concerned.
To expand on this point, you are guaranteed to be able to access the field only once. Many implementations allow multiple reads of the same field many times. If you are trying to be implementation independant then it is best to read a column only once. With some drivers, you can call rs.getXXX( i ) until the cows come home(even though it is more efficient and less taxing on DB resources to make the call once)! You should also read the columns in the order that they appear in your query. ex. rs.getString( 1 ) then rs.getString( 2 ) ....
Jamie
[ July 15, 2002: Message edited by: Jamie Robertson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in extracting values from ResultSet