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

    Default Accessing value from one method and use it in the SQL

    I have two methods (both connecting to different databases) defined in the following code :

    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);					
    					
    			System.out.println("I am inside getCompanyShowDocument method and checking the value of employeeValueAsStaticVariable");
    			System.out.println(employeeValueAsStaticVariable); // Prints Null
    			
    			rs1 = pstmt1.executeQuery();			
    			while(rs1.next()) {				
    				
    				//some stuff here
    				
    				empShowDocs.add(nlpShowDocRecord);
    				
    				
    			}
    		} catch(Throwable th) {
    			throw new DaoException(th.getMessage(), th);
    		} finally {
    			//Closing resources here
    		}		
    		
    		return empShowDocs;
    	}
    	
    	public List<ShowDescription>  getEmployeeNumber(String empID) throws DaoException
    	{
    		
    		
    		stopWatch.start();						
    		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 getEmployeeNumber method to get the Value");
    				System.out.println(employeeRecord.getEmployeeValue()); // Prints the value 
    				employeeValueAsStaticVariable = employeeRecord.getEmployeeValue();
    			}
    		} catch(Throwable th) {
    			throw new DaoException(th.getMessage(), th);
    		} finally {
    			// Closing all resources here
    		}		
    		
    		return employeeValue;
    	}
    
    	public static void main(String[] args) 
    	{
    		// TODO Auto-generated method stub
    		ClassPathXmlApplicationContext context = null;
    		try {
    			
    															
    		} catch(Throwable th) {
    			th.printStackTrace();
    		} finally {
    			context.close();
    		}
    	}
    	
    		
    		
    	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;
    	
    }
    My ShowDescription class is as follows just in case needed for reference where I have defined getter and setter
    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 which are used in getCompanyShowDocument method	
    	
    			
    	//Declaring variables for getting value
    	@JsonView(Views.Normal.class)
    	private String employeeValue;
    	
    	
    }
    So basically in my selectemployeeInfo_SQL, I want to use the value (for empl_value) obtained from the statement
    System.out.println(employeeRecord.getEmployeeValue ());(which is inside the getEmployeeNumber method).

    But for some reason I am not able to get this value outside of the while loop. I was trying something like defining static variable
    public static String employeeValueAsStaticVariable but it doesn't work if I try to print the value using this variable somewhere outside
    the method.

    Could anyone tell me what is the best approach to get the value and use init in the SQL I mentioned above? Thanks

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

    Default Re: Accessing value from one method and use it in the SQL

    You're going to need to describe exactly what it is you are trying to achieve.
    Not the technical aspect of the immediate problem, but the actual requirements that these two methods are supposed to cover.

    There's nowhere in the above showing how the two are being called either.

    I will say that your method names are suspect, as getEmployeeNumber is not returning any numbers, but returns a list of ShowDescription (whatever that is).
    Similarly getCompanyShowDocument is returning the same thing (which is itself odd, as it is looking at a different table in a different database), and not whatever a ShowDocument might be.

    Note, don't post a load of extra code, try and explain the requirements, because at first glance the above looks incorrect.
    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
    164
    Rep Power
    7

    Default Re: Accessing value from one method and use it in the SQL

    Quote Originally Posted by Tolls View Post
    You're going to need to describe exactly what it is you are trying to achieve.
    Not the technical aspect of the immediate problem, but the actual requirements that these two methods are supposed to cover.

    There's nowhere in the above showing how the two are being called either.

    I will say that your method names are suspect, as getEmployeeNumber is not returning any numbers, but returns a list of ShowDescription (whatever that is).
    Similarly getCompanyShowDocument is returning the same thing (which is itself odd, as it is looking at a different table in a different database), and not whatever a ShowDocument might be.

    Note, don't post a load of extra code, try and explain the requirements, because at first glance the above looks incorrect.
    Okay, I have tried to modify and include more relevant details and created a new post in the Spring section. I hope it helps you in understanding my problem.

    https://www.java-forums.org/spring/9...tml#post419490

    Let me know if there is something I need to clarify more. Thanks

Similar Threads

  1. Accessing a method from another class
    By mcnikal in forum New To Java
    Replies: 6
    Last Post: 04-01-2014, 10:04 PM
  2. Method Accessing
    By Army in forum New To Java
    Replies: 6
    Last Post: 01-11-2013, 05:55 AM
  3. Accessing a method in another class
    By denno in forum New To Java
    Replies: 14
    Last Post: 04-07-2011, 05:59 AM
  4. Accessing a method in a different class
    By adjit in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 10:11 PM
  5. Need help with accessing files in a method
    By t0nydanzuh in forum New To Java
    Replies: 6
    Last Post: 10-03-2010, 06:05 PM

Posting Permissions

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