aspose file tools*
The moose likes JDBC and the fly likes JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC" Watch "JDBC" New topic
Author

JDBC

anuj khanna
Greenhorn

Joined: Jan 11, 2001
Posts: 20
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
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
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC