Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database Starnge behaviour

 
Essam AbdelAziz
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ALL,

i'm trying to access some data from
my database but i have A strange behavior and hope to help me to understand
what is happening

when i try to retrieve column data form DB and print it

it will be printed

but when i try to assign it to a String

i get this exception "NO Data Found"

Query=" select * from CPM_DEST";

st = conn.createStatement();

rs=st.executeQuery(Query);


while(rs.next())

{


System.out.println(rs.getString("DESTNAME")); //printwith no problem


String x=(String)rs2.getString("DESTNAME");//here i get the exception

}



Whta's happining..?

Thanks®ards

essam
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. what is rs2 ?
2. you don't need to cast as getString returns a String
 
Essam AbdelAziz
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. what is rs2 ?

sorry it's just amistake in writing .it's supposed to be rs

2. you don't need to cast as getString returns a String

just i'm trying to find asolution you can remove teh casting
but the problem still
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only read each value from the Resultset once, after that nothing is guaranteed. It's in the API somewhere but I'd have to go look.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Essam AbdelAziz9",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
You can only read each value from the Resultset once, after that nothing is guaranteed. It's in the API somewhere but I'd have to go look.


Yeah Dave i got you point inside jdbc basic on java.sun.com
"each column should be read only once"
here is a URL http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html look at last line in first paragraph inside the heading of Using the getXXX Methods
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can check the API for ResultSet

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic