How can you retrieve data from the ResultSet?

First JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs.E.g.ResultSet rs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");Second:String s = rs.getString("COF_NAME");The method getString is invoked on the ResultSet object rs , so getString will retrieve (get) the value stored in the column COF_NAME in the current row of rs

Showing Answers 1 - 2 of 2 Answers

Sreekanth Chinthalapalli

  • Jun 2nd, 2005
 

Statement stmt = conn.createStatement(); 
ResultSet rs = stmt.executeQuery(SELECT COF_NAME, PRICE FROM COFFEES"); 
while (rs .next() ) 

//Iam assuming there are 3 columns in the table. 
System.out.println ( rs.getString(1)); 
System.out.println(rs.getString(2)); 
System.out.println(rs.getString(3)); 

 
//don't forget to close the resultset, statement & connection 
 
rs.close(); //First  
stmt.close(); //Second 
con.close(); //Last  
System.out.println("You are done"); 
 

  Was this answer useful?  Yes

Siladitya Chatterjee

  • Sep 29th, 2005
 

import java.sql.*;class JDBCtableread{ public static void main(String args[])throws Exception { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:dsn2";//dsn name Connection con=DriverManager.getConnection(url); System.out.println("Successful Connection"); Statement s=con.createStatement(); ResultSet r=s.executeQuery("select * from table2 where name='siladitya'");//table name while(r.next()) { System.out.println(r.getString(1)); System.out.println(r.getString(2)); System.out.println(r.getString(3)); }//while con.close(); }//try catch(SQLException sq) { System.out.println("error:"+sq.getMessage()); }//catch }//main}//class/* save as JDBCtableread.java and create a dsn with name dsn2create a table table2 and read the data of the tables*/

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions