• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JDBC

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's wrong in the code.i'm not able to put the result in a string object.

import java.sql.*;
class DataBaseConnect {
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL=new String("jdbc dbc:db1");
Connection con=DriverManager.getConnection(sourceURL);
Statement statement=con.createStatement();
ResultSet rs=statement.executeQuery("select productdescription from products where productid>1");
String str=rs.getString("productdescription");
while (rs.next())
{
System.out.println(str);
}
}
}

 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get an exception? If so, please quote the exception next time. Anyway. It looks like it should work if you move the rs.getString() inside your while loop.
- Peter
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String str=rs.getString("productdescription");


You can't do a getString() until you do a next(). The ResultSet isn't pointing to a row until the first next() is executed.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this instead
<code>
ResultSet rs=statement.executeQuery("select productdescription from products where productid>1");
while (rs.next())
{
String str=rs.getString("productdescription");
System.out.println(str);
}
</code>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic