Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Result Set in jdbc

  Asked By: Egidius    Date: Aug 20    Category: Java    Views: 665
  

i get an highlighted error, while retriving column value for column "FirstName"
. But instead i write the following query by by using every columns name in
SELECT clause it works..but i am using SELECT *

can anyone tell me the root cause of exception

the code is like this...

package com.struts.friends;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collection;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class FriendAction extends Action {

/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
System.out.println("I'm in FriendAction");
FriendForm friendForm = (FriendForm) form;
ActionErrors errors=new ActionErrors();
try {
Connection con=null;
con = LoginAction.getConnect();
ResultSet rs1 = null;
Statement stmt = con.createStatement();
Collection friends = new Vector();
System.out.println("*********"+request.getAttribute("personId"));
int personid =
Integer.parseInt(request.getAttribute("personId").toString());
System.out.println("++++++++++++++++personid:"+personid);
String query = "select * from Friends where friendid in"+
"(select friendid from personfriends,person where"+
" personfriends.personid="+personid+")";
System.out.println("Query2:"+query);
rs1 = stmt.executeQuery(query);
while(rs1.next()){
Friend friend = new Friend();
friend.setFriendId(rs1.getInt("FriendID"));
System.out.println("FriendID:"+friend.getFriendId());
friend.setAddress(rs1.getString("Address"));
System.out.println("Address:"+friend.getAddress());
friend.setBirthDate(rs1.getDate("BirthDate"));
System.out.println("Bdate:"+friend.getBirthDate());
friend.setCompany(rs1.getString("Company"));
System.out.println("Company:"+friend.getCompany());

// on next line i get SQLException invalid descriptor index but column name
& data type match exactly

System.out.println("1stName:"+rs1.getString("FirstName"));
System.out.println("LastName:"+rs1.getString("LastName"));
friend.setEmail(rs1.getString("Email"));
friend.setFirstName(rs1.getString("FirstName"));
friend.setLastName(rs1.getString("LastName"));
friend.setMobile(rs1.getInt("Mobile"));
System.out.println("Mobile:"+friend.getMobile());
friend.setPhone(rs1.getString("Phone"));
friends.add(friend);
}
//friendForm.setFriends(friends);
return mapping.findForward("success");
}catch(Exception e){
e.printStackTrace();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("errors.noFriends"));
return mapping.findForward("success");
}
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Result Set in jdbc Or get search suggestion and latest updates.




Tagged: