• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return multiple fields aas 1 object

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a query which has 5 fields after the select statement. Each of these fields are going to make one row in the query result.
So for example I may have fields
first last age occupation empid
Now each of these fields will have corresponding getXXX() and setXXX() in the JavaBean.
I want to return 1 object which holds all 5 fields. So when I return the one object, I'll get "first last age occupation empid"
not just first or last or age, etc.
So let's say for example I have this data
Joe Schmoe 22 programmer 112
Ann Lann 44 manager 113
Some Name 36 president 114
Now I want to return an object which contains all 5 fields. If I say in a Bean
getObject()
{
return row;
}
the row should return all 5 fields, not just one field. return row from the above getObject() will be
Ann Lann 44 manager 113 (it returns all the 5 fields)
Can someone help me implement this in a Java Bean? Do I need a constructor in my bean?
Please help
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need the constructor......

//This is normal bean or (accessor class)
public class Emp{
private long empId=0;
private String empName=null;
private String empDesg=null;
public void setEmpId(long empId){
this.empId=empId;
}
public void setEmpName(String empName){
this.empName=empName;
}
public void setEmpDesg(String empDesg){
this.empDesg=empDesg;
}
public long getEmpId(){
return this.empId;
}
public String getEmpName(){
return this.empName;
}
pulbic String getEmpDesg(){
return this.empDesg;
}
}
//This is Entity or session ....class
class EmpClass....{
//This is main function for getting the only one specified row
public Emp getObject(long empId) throws FinderException {
Connection dbConnection=null;
Statement st=null;
ResultSet rs=null;
String strQry=null;
Emp empTb=null;
try{
dbConnection = getConnection();
strQry = " SELECT EMPID,EMPNAME,EMPDESG "+
" FROM EMPTB WHERE USERID = "+empId;
st = dbConnection.createStatement();
rs = st.executeQuery(strQry);
if(!rs.next()){
throw new FinderException("RECORD IS NOT FOUND INTHE TABLE");
}else{
empTb = new Emp();
empTb.setEmpId(rs.getString(1));
empTb.setEmpName(rs.getString(2));
empTb.setEmpDesg(rs.getString(3));
return empTb;
}
}catch(Exception e){
throw new FinderException(e.getMessage());
}finally{
..
rs.close();
st.close();
dbConnection.close();
}
}
}//End class
 
DAYANAND BURAMSHETTY
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by DAYANAND BURAMSHETTY:
No need the constructor......

//This is normal bean or (accessor class)
public class Emp{
private long empId=0;
private String empName=null;
private String empDesg=null;
public void setEmpId(long empId){
this.empId=empId;
}
public void setEmpName(String empName){
this.empName=empName;
}
public void setEmpDesg(String empDesg){
this.empDesg=empDesg;
}
public long getEmpId(){
return this.empId;
}
public String getEmpName(){
return this.empName;
}
pulbic String getEmpDesg(){
return this.empDesg;
}
}
//This is Entity or session ....class
class EmpClass....{
//This is main function for getting the only one specified row
public Emp getObject(long empId) throws FinderException {
Connection dbConnection=null;
Statement st=null;
ResultSet rs=null;
String strQry=null;
Emp empTb=null;
try{
dbConnection = getConnection();
strQry = " SELECT EMPID,EMPNAME,EMPDESG "+
" FROM EMPTB WHERE EMPID= "+empId;
st = dbConnection.createStatement();
rs = st.executeQuery(strQry);
if(!rs.next()){
throw new FinderException("RECORD IS NOT FOUND INTHE TABLE");
}else{
empTb = new Emp();
empTb.setEmpId(rs.getString(1));
empTb.setEmpName(rs.getString(2));
empTb.setEmpDesg(rs.getString(3));
return empTb;
}
}catch(Exception e){
throw new FinderException(e.getMessage());
}finally{
..
rs.close();
st.close();
dbConnection.close();
}
}
}//End class


 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic