This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not retreiving from db

 
sreela nath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello


can you please help me clear my doubt!!!

In my java class i have written code to connect to database and it is working also. But when i tried to query the database it is not retrieving anything. i have created a table "emp" with an attribute "name".

when i write query in my java class as "select * from emp" , it is not giving me any result eventhough i have created one row. No exception shown
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we can't see your code or your database, it's impossible for anybody here to tell you what's going wrong.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please provide us with the relevant code that opens the connection to the database and executes the query? Please rember to TellTheDetails when posting a question.
 
Campbell Ritchie
Sheriff
Posts: 51418
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
sreela nath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir,
I am sorry that i did not clearly explain my doubt..i am new to this site. That is why..



This is the code i wrote. I am using Oracle 10g xe database and my operating system is windows8. Eclipse juno is my environment.


In my Run SQL Command Line i wrote query to display all tables in my database and their schemas...I found Countries table in hr schema...
In my java code when i tried "select * from hr.Countries"..it worked properly and displayed all country names.

Then i created emp table in hr schema with "name" as attribute.
create table hr.emp(name varchar(10));
insert into emp values('sreela');

But when i query the database it is not retreiving any value...


Can you please help me..If the data i provided is not sufficient, please ask me ok..

Regards
Sreela
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your problem:


Remember, every time you call rs.next(), it moves forward one row in the result set, unless you're already at the last row, in which has it moves to nothing and returns false.

On the first line, you move to the first row of the RS--the one and only row in that table--and print out that row, which probably gives you something like oracle.jdbc.driver.ResultSet@12345678. Then your while loop calls rs.next() again, moving you past the first (and only) row and on to the next row, which doesn't exist, so, on to nothing. You've only inserted 1 row, so there is no next row, so while is false and you skip the body of the loop altogether.

If you had 2 rows, that would print out only the 2nd one. If you had 3 rows, it would print out the 2nd and 3rd.
 
sreela nath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir ,i understood what you said. But what should i do to print the one row that is in my table.

When i omitted the rs.next() statement, it is showing exception java.sql.SQLException:ResultSet.next was not allowed



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sreela nath wrote:Sir ,i understood what you said. But what should i do to print the one row that is in my table.


One row or one mililon rows, the exact same code can work. Call rs.next() until you run out of rows, and extract and print the columns from each row as you encounter it.

When i omitted the rs.next() statement, it is showing exception java.sql.SQLException:ResultSet.next was not allowed


I didn't say you should get rid of rs.next(). Go back and look at what I said, and look at your code. Your problem was that your first call to rs.next() had what you wanted, but you didn't extract the column data from it.
 
sreela nath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry sir


The exception shown is "resulset not called"

In previous post i typed it wrongly...


Regards
Sreela
 
Swastik Dey
Rancher
Posts: 1639
6
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
sreela nath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sir

I tried what you said.But now also no value retreived.






Also system.out.println(rs.next()) is printing false..So it means that no data is being retreived know..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not paying attention to what I said.



The above code completely ignores the data in the first row that's returned.

If the first println() is printing false, then no data was returned, and that's a separate problem you'll have to work on, but deal with that later.

That code says, "First tell me whether any rows were retrieved, then, if at least 2 rows were retrieved, print out the 'name' column from the second row."


 
sreela nath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir

I am understanding what you are saying.

This won't show any value.That i do understand as i have called rs.next() twice.But the problem that confuses me is, then why the first System.out.println(rs.next()) is returning false..So some other problem is also there na...


I had also tried the code you said:
That is



Then also no output.

Sir, I am so happy that i am getting your support very much..Thanks alot. But please don't lose your patience hearing my doubts
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic