This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic