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
Joined: Apr 20, 2000
Posts: 3252
posted
0
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
Joined: May 05, 2000
Posts: 13974
posted
0
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.
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>