Results 1 to 6 of 6
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    165
    Rep Power
    7

    Default Trying to get the value from a method in the Data Object Access Implementation class

    I have the following stuff defined in the controller of Spring 4. With the help of two beans beanONE and beanTWO I am basically connecting to two different databases.

    Problem I am facing:

    In EmployeeDaoImpl class, inside getemployeeDetails method, I am able to see a value that I need which is getting printed using the following statement
    Java Code:
    System.out.println(employeeRecord.getEmployeeValue());.
    I want to use this value in the SQL which is used to query a different database. I mean, I would like to use the value obtained in the following SQL
    statement
    Java Code:
    private String selectEmployeeInfo_SQL = "SELECT * FROM DATABASETWO.companydetails WHERE emp_value = <<VALUE HERE>>";
    One might think that I should be able to call the getemployeeDetails method inside the getCompanyShowDocument method and use the value. But I have some concerns related to that:

    1) In order for me to call the getemployeeDetails method inside the getCompanyShowDocument method, I would
    have to pass the empID_ which was already passed as a web service parameter in the controller. So, the method getemployeeDetails has been already executed by the time I am in the EmployeeDaoImpl class.

    2) Also, the getemployeeDetails method has a return type of list so I am wondering should I also consider changing it to String or int after my first concern mentioned above is resolved?

    My Controller Code:

    Java Code:
    @RequestMapping(value="/showEmpDetails", method=RequestMethod.GET)
            public String showEmpDetails
            (
            	@RequestParam(value="doc_id", defaultValue="0") String doc_id,
            	@RequestParam(value="emp_id", defaultValue="1234") String empID_
            ) 
            {
        		String jsonString = "";
        		
        		try {
                    // Bean 1 defined for an Oracle database		
        			EmployeeDao empDao = (EmployeeDao) context.getBean("empDao");
        			
        			// Bean 2 defined for different Oracle database
        			EmployeeDao empForValueDao = (EmployeeDao) context.getBean("empValueDao");
        			
        			List<ShowDescription> beanONE = empDao.getCompanyShowDocument(doc_id);
        			List<ShowDescription> beanTWO = empForValueDao.getemployeeDetails(empID_);
        			
        			
        		} catch(Throwable th) {
        			//Some code
        		}
        
        		
        		return JsonString;
            }
    In XML I have the beans defined as follows :

    Java Code:
    <bean id="empDao" class="abc.edg.myproj.dao.impl.EmployeeDaoImpl">	
        	<property name="dataSource"><ref bean="DB_ONE"/></property>
        </bean>	
        
        <bean id="empValueDao" class="abc.edg.myproj.dao.impl.EmployeeDaoImpl">	
        	<property name="dataSource"><ref bean="DB_TWO"/></property>
        </bean>
    EmployeeDao: My DAO interface is defined as follows :

    Java Code:
      public interface EmployeeDao
        {
          List<ShowDescription> getCompanyShowDocument(String doc_id) throws DaoException;
          List<ShowDescription> getemployeeDetails(String empID) throws DaoException;
        
        }
    EmployeeDaoImpl : My implementation class is as follows:

    Java Code:
    public class EmployeeDaoImpl implements EmployeeDao
        {
        	public void setDataSource(DataSource dataSource) 
        	{		
        		jdbcTemplate = new JdbcTemplate(dataSource);									
        	}
        	
          public List<ShowDescription>  getCompanyShowDocument(String docId) throws DaoException
        	{
        					
        		DataSource ds = null;
        		Connection conn = null;
        		PreparedStatement pstmt1 = null;
        		ResultSet rs1 = null;
        		
        		
        		List<ShowDescription> empShowDocs = new ArrayList<ShowDescription>();			
        		try {
        			ds = jdbcTemplate.getDataSource();
        			conn = ds.getConnection();
        			
        			pstmt1 = conn.prepareStatement(selectEmployeeInfo_SQL);
        			pstmt1.setString(1, docId);					
        							
        			rs1 = pstmt1.executeQuery();			
        			while(rs1.next()) {				
        				
        				//some stuff here
        			 				
        			}
        		} catch(Throwable th) {// catching exception
        		} finally {
        			//Closing resources here
        		}		
        		
        		return empShowDocs;
        	}
        	
        	public List<ShowDescription>  getemployeeDetails(String empID) throws DaoException
        	{
        			
        		
        		DataSource ds = null;
        		Connection conn = null;
        		PreparedStatement pstmt2 = null;
        		ResultSet rs2 = null;
        		List<ShowDescription> employeeValue = new ArrayList<ShowDescription>();
        		
        		try {
        			ds = jdbcTemplate.getDataSource();
        			conn = ds.getConnection();
        			pstmt2 = conn.prepareStatement(selectValueSQL);
        			pstmt2.setString(1, empID);					
        				
        			rs2 = pstmt2.executeQuery();			
        			while(rs2.next()) {				
        				ShowDescription employeeRecord = new ShowDescription();	
        				employeeRecord.setEmployeeValue(rs2.getString("value"));
        				employeeValue.add(employeeRecord);
        				
        				System.out.println("I am inside getemployeeDetails method");
        				System.out.println(employeeRecord.getEmployeeValue()); // Prints the value 
        				employeeValueAsStaticVariable = employeeRecord.getEmployeeValue();
        			}
        		} catch(Throwable th) {
        			//some code
        		} finally {
        			// Closing all resources here
        		}		
        		
        		return employeeValue;
        	}
        
        	public static void main(String[] args) 
        	{
        		
        	}
        	
        		
        	private String selectValueSQL = "SELECT * FROM DATABASEONE.empdetails WHERE emp_id = ? ";
        			                       
        	private String selectEmployeeInfo_SQL = "SELECT * FROM DATABASETWO.companydetails WHERE emp_value = 2";
        										
        			       
        			
        
        	private JdbcTemplate jdbcTemplate;	
        	public static String employeeValueAsStaticVariable;
        	
        }
    ShowDescription class if needed for reference is below:

    Java Code:
    import com.fasterxml.jackson.annotation.JsonView;
        
        import abc.def.project.json.Views;
        
        public class ShowDescription {
        
        	// Adding getters and setters 
        	public String getEmployeeValue() {
        		return employeeValue;
        	}
        	public void setEmployeeValue(String employeeValue) {
        		this.employeeValue = employeeValue;
        	}
        	
        	// Some other getters and setters defined in getCompanyShowDocument method goes here	
        	
        			
        	//Declaring variables for getting value
        	@JsonView(Views.Normal.class)
        	private String employeeValue;
        	
        	
        }
    Last edited by Jack_Tauson_Sr; 11-10-2017 at 05:17 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Trying to get the value from a method in the Data Object Access Implementation cl

    Why not simply do the queries in the other order?

    I also don't see where in the getCompany... method you actually use the emp_value you need?

    I do have even more questions, though.

    If the two tables are in different databases then why have both queries in the same DAO? That confuses me, and I suspect is confusing you.
    A single instance of that DAO presumably won't work for both method calls, so that looks like a bug in the making.

    Now you could do this by having both DataSources in the one DAO. That might be preferable as then the outside code (your controller etc) won't have to know that there are two databases involved.
    Also the DAO should be injected into the Controller, Autowired would probably be simplest.
    Getting the beans as you are doing makes unit testing a pain.

    I still don't know exactly what this code is supposed to be doing. What happens to these two Lists?
    How are they related?
    What is the user asking for?
    It looks like (from the naming) they want employee details for the given employee and company?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    165
    Rep Power
    7

    Default Re: Trying to get the value from a method in the Data Object Access Implementation cl

    Quote Originally Posted by Tolls View Post
    Why not simply do the queries in the other order?

    I also don't see where in the getCompany... method you actually use the emp_value you need?

    I do have even more questions, though.

    If the two tables are in different databases then why have both queries in the same DAO? That confuses me, and I suspect is confusing you.
    A single instance of that DAO presumably won't work for both method calls, so that looks like a bug in the making.

    Now you could do this by having both DataSources in the one DAO. That might be preferable as then the outside code (your controller etc) won't have to know that there are two databases involved.
    Also the DAO should be injected into the Controller, Autowired would probably be simplest.
    Getting the beans as you are doing makes unit testing a pain.

    I still don't know exactly what this code is supposed to be doing. What happens to these two Lists?
    How are they related?
    What is the user asking for?
    It looks like (from the naming) they want employee details for the given employee and company?
    Answering your questions below :

    I also don't see where in the getCompany... method you actually use the emp_value you need?

    Answer: In the getCompanyShowDocument method, I am using the SQL selectEmployeeInfo_SQL where I have the value 2 defined as follows for emp_value:

    Java Code:
    private String selectEmployeeInfo_SQL = "SELECT * FROM DATABASETWO.companydetails WHERE emp_value = 2";
    So, I am trying to get this "2" value somehow from the getemployeeDetails method. Right now I am not using the value because I am still trying to figure out the correct way to do it.

    If the two tables are in different databases then why have both queries in the same DAO?
    Answer: As a beginner to Spring, I didn't know that we can't do two queries in one DAO. Good to know.

    Do you think the approach mentioned here by someone could work:

    One DAO two different DataSources - is that possible - Spring Forum

    But here, I am not sure how I will get the value from the getemployeeDetails method so that I could use it in the selectEmployeeInfo_SQL because they are defining just to connect with two datasources and inject it in one Dao.


    I still don't know exactly what this code is supposed to be doing. What happens to these two Lists?
    How are they related?
    What is the user asking for?

    Answers: So, the first list was already there with the webservice corresponding to the getCompanyShowDocument method. This list is basically used for displaying the response in JSON format. However, there isn't any JSON format related stuff is involved with the second bean or second list.I added the second method getemployeeDetails and basically followed the same things (list oriented) as it was there for the first list. I am not sure if list is even needed for the second method or not. I was just trying to see whether two beans works simultaneously or not and I noticed that I was getting the value through this line
    System.out.println(employeeRecord.getEmployeeValue ()); and hence thought I could somehow get this value through the list approach.

    In the controller, I tried the following steps:

    I tried to print the contents for the beanTWO in the following manner :System.out.println(beanTWO); but it printed the following :

    abc.edg.myproj.domain.ShowDescription@4de0d4f

    Basically by printing the above, I was expecting if I could pass the value like the following in the getCompanyShowDocument method like the following :

    Java Code:
     List<ShowDescription> beanTWO = empDao.getCompanyShowDocument(doc_id,beanONE)
    Do you think this is a correct approach?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Trying to get the value from a method in the Data Object Access Implementation cl

    That doesn't explain to me what the service is supposed to be doing.
    That's telling me the current technical details.

    Note, I'm not necessarily suggesting using 2 separate DAO classes...I did follow that up with the possibility of using one DAO with two Data Sources.

    I will say again, there does not seem to be any reason to be using the ShowDescription class when you are getting the employeeValue values.
    Since there is a single field being returned then why not return a List<String>?

    Then call the getEmployeeEtc method before the getCompanyDetails method.
    Pass the List<String> in, and use that in your SQL.

    If the List<String> isn't huge then I would suggest building an IN statement rather than querying the database however many times.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    165
    Rep Power
    7

    Default Re: Trying to get the value from a method in the Data Object Access Implementation cl

    Quote Originally Posted by Tolls View Post
    That doesn't explain to me what the service is supposed to be doing.
    That's telling me the current technical details.

    Note, I'm not necessarily suggesting using 2 separate DAO classes...I did follow that up with the possibility of using one DAO with two Data Sources.

    I will say again, there does not seem to be any reason to be using the ShowDescription class when you are getting the employeeValue values.
    Since there is a single field being returned then why not return a List<String>?

    Then call the getEmployeeEtc method before the getCompanyDetails method.
    Pass the List<String> in, and use that in your SQL.

    If the List<String> isn't huge then I would suggest building an IN statement rather than querying the database however many times.
    Thanks Tolls for your continued guidance on this and for your valuable time. You are right, the ShowDescription class wasn't needed.

    In fact, I didn't even use List<String> but only String to get the only one string related value and I was able to accomplish what I have been trying to for past few days.

    Thanks again. Appreciated !

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Trying to get the value from a method in the Data Object Access Implementation cl

    Oh right.
    If there's only going to be one result then you should be using:
    Java Code:
    if (rs2.next()) {
        return rs2.getString("value");
    }
    return null; // ?? not sure what your plan is if the value does not exist
    instead of doing a while loop.
    This shows, in the code, that you are expecting only the one value to be returned.

    As the comment says, you do need to figure out what to do if the value cannot be found.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Best way to access and display bean object data?
    By noodle_variation_187-X in forum JavaServer Pages (JSP) and JSTL
    Replies: 29
    Last Post: 06-21-2012, 02:46 PM
  2. Access object created in another class
    By BalintD in forum New To Java
    Replies: 5
    Last Post: 01-04-2012, 11:06 AM
  3. Unable to access getter method in an object class I made
    By EnSlavingBlair in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 10:52 AM
  4. Replies: 2
    Last Post: 06-16-2011, 09:39 AM
  5. Replies: 0
    Last Post: 04-04-2010, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •