返回ArrayList,输出与get方法结合使用

时间:2015-03-13 08:16:25

标签: java arraylist getmethod

我认为这会奏效,但遗憾的是它没有。我收到了错误 -

  

类型ArrayList中的方法add(CustomerInfo)不是   适用于参数(String)

我的目标是返回Arraylist并使用get方法进行访问。当我为Arraylist使用字符串时,我无法使用arr.get(i) .userID ,... .FirstName ..

Class CustomerInfo.java

  public class CustomerInfo {
    private static Connection conn = null;
    private static ResultSet resultSet = null;
    public String UserID;
    public String FirstName;
    public String SecondName;


    public ArrayList<CustomerInfo> findCustomer (String userID) throws SQLException {

            conn = null;
            PreparedStatement pstmt = null;

            try {

                JDBCConnection jdbcConn = new JDBCConnection();
                conn = jdbcConn.openConnection();

                ArrayList<CustomerInfo> customerList  new ArrayList<CustomerInfo();

                String sql = "SELECT USERID FROM TAB0025 WHERE USERID = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, userID);

                resultSet = pstmt.executeQuery();


                while (resultSet.next()) {

                customerList.add(resultSet.getString("USERID"));
                customerList.add(resultSet.getString("FIRSTNAME"));
                customerList.add(resultSet.getString("SECONDNAME"));


                this.UserID = resultSet.getString("USERID");
                this.FirstName = resultSet.getString("FIRSTNAME");
                this.SecondName  resultSet.getString("SECONDNAME");

                }

                return customerList;

            } catch (Exception e) {
                throw e;
            }

            finally {
                conn.close();
            }


        public String getUserID() {
            return this.UserID;
        }

        public String getFirstname() {
            return this.FirstName;

        }

        public String getSecondName() {
            return this.SecondName;

        }

    }

类InputReader.java

    // ...

    if (CustomerInfo.ExsistUserID(this.UserID)) {

                    CustomerInfo edit = new CustomerInfo();
                    ArrayList<CustomerInfo> arr = new ArrayList<CustomerInfo>();

                    arr = edit.findCustomer(this.UserID);


                    System.out.println("UserID: "+ arr.get(0).getUserID() + "  First Name: "arr.get(0).getFirstName() + " Second Name: " arr.get(0).getSecondName()); 

                } 
   // ...

1 个答案:

答案 0 :(得分:3)

错误在以下三行:

customerList.add(resultSet.getString("USERID"));
customerList.add(resultSet.getString("FIRSTNAME"));
customerList.add(resultSet.getString("SECONDNAME"));

如上所示,resultSet.getString()方法返回String对象,但您的ArrayList是CustomerInfo类型对象的容器,因此您需要创建一个新的CustomerInfo对象,使用ResultSet中的值填充其字段,然后将该对象添加到ArrayList中,如下所示:

custInfo = new CustomerInfo();
custInfo.UserID = resultSet.getString("USERID");
custInfo.FirstName = resultSet.getString("FIRSTNAME");
custInfo.SecondName = resultSet.getString("SECONDNAME");

customerList.add(custInfo);