简体   繁体   中英

Passing resultset value to another class

I'm learning basic java concepts. I'm trying to pass a resultset to another class but not sure how to do it.

This is my action class:

import com.utils.JdbcTemplateProvider;

    public class ExParamAction{

        public void PwdExpiryDays() throws SQLException {
            ExParam exParam= new ExParam();
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            String sql = "select pwd_expiry_days from parameter_tools";
            try {
                connection = JdbcTemplateProvider.getJdbcTemplate().getDataSource()
                        .getConnection();
                preparedStatement = connection.prepareStatement(sql);
                ResultSet rs = preparedStatement.executeQuery();
                while (rs.next()) {
                    exParam.setPwdExpiryDays(rs
                            .getInt("PWD_EXPIRY_DAYS"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {

                if (preparedStatement != null) {
                    preparedStatement.close();
                }

                if (connection != null) {
                    connection.close();
                }

            }

How do I pass the exParam.setPwdExpiryDays(rs.getInt("PWD_EXPIRY_DAYS")) to another class?

My ExParam class:

public class ExParam {

    private Integer pwdExpiryDays;
    public Integer getPwdExpiryDays() {
        return pwdExpiryDays;
    }

    public void setPwdExpiryDays(Integer pwdExpiryDays) {
        this.pwdExpiryDays = pwdExpiryDays;
    }
}

Create global field :

    public class ExParamAction{
          ExParam exParam= new ExParam();
.......

and create getter for pwdExpiryDays :

public int getPwdExpiryDays(){
     return exParam.getPwdExpiryDays();
}

in this part of your code.

   while (rs.next()) {

      exParam.setPwdExpiryDays(rs.getInt("PWD_EXPIRY_DAYS"));
     //the other class should accept the "ExParam" objet as a parameter and you can then pass it. 
      ExampleClass.staticMethod(exParam); //for static methods use
      //Or
      exampleClassInstance.exampleMethod(exParam); //for general use

   }

A mini sample:

 Class A{

   void method(B b){...} //B here might be your exParam. or the result you execute.

 }

 Class B{  ... }

after passing the object you can do things in other class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM