• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Not able to use ResultSet properly

 
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic