Routray
Routray

Reputation: 75

Unable to print SQL statement in console output

Can anyone suggest what i am doing wrong here, trying to print the SQL query output in console:

import java.io.*;  
import java.sql.*;  

public class RetrieveFile {  
public static void main(String[] args) {  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@123.34.54.100:35120/test1", "user1", "*****");  

PreparedStatement ps=con.prepareStatement("select APPLY from MSG where MID='1234'");  
ResultSet rs=ps.executeQuery();  
rs.next();//now on 1st row  


con.close();  

System.out.println("success" + rs);  
}catch (Exception e) {e.printStackTrace();  }  
}  
}  

Actual Response:- successoracle.jdbc.driver.OracleResultSetImpl@66d33a

Expected Response:- Success NOW(Which SQL query should retrieve)

Upvotes: 1

Views: 1918

Answers (2)

Constantin Müller
Constantin Müller

Reputation: 1300

Your problem is, that it seems that your ResultSet implementation doesn't override the public String toString() method. Because of this the parent's class (Object) default implementation is used. And the default implementation prints the class's name, in your example oracle.jdbc.driver.OracleResultSetImpl and the object's hash code 66d33a.

Instead iterate over the result set and printing each entry line by line:

while(rs.next()) {

    for (int column = 1; column <= numberOfColumns; column++) {

        if(column > 1) System.out.print(", ");
        System.out.print(rs.getString(column));
    }
}

Upvotes: 1

Vanitha Kumar
Vanitha Kumar

Reputation: 196

You will have to iterate throught he ResultSet object to get the columns returned in your query.

while(rs.next()){
System.out.println("success" + (rs.getString(1)); }

Upvotes: 0

Related Questions