• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to use ResultSet properly

 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a JDBC- program, which simply

1- creates a table (having two columns).
2- inserts a record.
3- read a value using ResultSet object.

The code i wrote is:


The above code compiles fine, but I am getting a runtime exception saying:



The table gets created, data which I inserted is also there. What is going wrong when I try to
access 1st column of the table using ResultSet object ??
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Insert statements don't return results. The code compiles because Java doesn't know whether you did an insert or select.

Can you add a select statement to the code before the rs.next()?
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Can you add a select statement to the code before the rs.next()?

Thank you i did, and it worked.
Then I think I should have used
stmt.execute("insert into Jr_ramanujam values ('ashish',12)");


in stead of:
stmt.executeQuery("insert into Jr_ramanujam values ('ashish',12)");


am i right here??
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:Then I think I should have used
stmt.execute("insert into Jr_ramanujam values ('ashish',12)");


in stead of:
stmt.executeQuery("insert into Jr_ramanujam values ('ashish',12)");

Yes. Good point. I got distracted by the resultset part and didn't pick up on that!
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Yes. Good point. I got distracted by the resultset part and didn't pick up on that!


Thank you Jeanne, I am new to database programming.Can you please suggest me any good book for sql,
which teaches about sql commands,say, which one returns a result and which does'nt ??
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirvikalp Rao wrote:Thank you Jeanne, I am new to database programming.Can you please suggest me any good book for sql,
which teaches about sql commands,say, which one returns a result and which does'nt ??

The JavaDoc covers that. And Oracle's JDBC tutorial trail is excellent.

As far as books:
If you are using MySQL, Murach's MySql is good. In general, Persistence in the Enterprise covers an introduction to a number of topics including JDBC.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic