This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Return multiple fields aas 1 object

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic