• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC

 
anuj khanna
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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);
}
}
}

 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • 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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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.
 
Matts Smith
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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>
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic