APPU cool
APPU cool

Reputation: 55

why is my resultset containing only one result when it was supposed to get two results

I executed the same query in my db interface directly and got two rows as result... I thought i should be getting same here too... but it is returning only one and for the second it says column index out of range 2>1 !!!

session= request.getSession();
String name=(String)session.getAttribute("user");

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mutualfund", "root", "");


Statement statement = connection.createStatement();
String qry="SELECT `account_type_id` FROM `cust_accounts` WHERE `cust_id`=\""+name+"\"";
ResultSet rs =  statement.executeQuery(qry) ;


if(!rs.next())
{
    out.println("Server Down.. Please try again later ");
} 
else {
    String one=rs.getString(1);
    String two=rs.getString(2);
}

Upvotes: 0

Views: 158

Answers (1)

Russell Uhl
Russell Uhl

Reputation: 4581

Your code isn't asking for two rows, it's asking for two columns, and your query only supplies one.

if (!rs.next()) {
  out.println("nope");
} else {
  String one = rs.getString(1);
  String two = "";
  if (rs.next())
    two = rs.getString(1);
  }
}

Upvotes: 1

Related Questions