循环时从结果集中获取值

时间:2017-11-08 15:23:28

标签: java spring jdbc

我正在使用spring框架,下面是方法getemployeeDetails

的数据访问对象实现代码

如下所示,在控制台中,我可以看到从以下语句中打印的值:

System.out.println(employeeRecord.getEmployeeValue()); // Prints the value

我想知道,如何从这个方法中获取此值,以便我可以在下面的另一个SQL语句selectEmployeeInfo_SQL中使用它 对于目前具有emp_value硬编码值的列2,如下所示。我需要在同一个类中的另一个方法(我在下面没有提到)中使用selectEmployeeInfo_SQL语句。

  public class EmployeeDaoImpl implements EmployeeDao
    {
        public void setDataSource(DataSource dataSource) 
        {       
            jdbcTemplate = new JdbcTemplate(dataSource);                                    
        }

    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 to get the Value");
                    System.out.println(employeeRecord.getEmployeeValue()); // Prints the value 

            } 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;

    }

以下是ShowDescription类的代码,以供参考:

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;
    }




    //Declaring variables for getting value
    @JsonView(Views.Normal.class)
    private String employeeValue;


}

0 个答案:

没有答案