【问题标题】:Return ArrayList , Output in combination with get Methods返回 ArrayList ,结合 get 方法输出
【发布时间】:2015-05-15 15:50:15
【问题描述】:

我认为这会起作用,但遗憾的是它没有。我得到了错误 -

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

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

类 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()); 

                } 
   // ...

【问题讨论】:

  • 您必须更具体地了解导致该错误的那段代码:)
  • 这里的“kundenList”是什么意思。您在哪里声明。
  • @Jani 抱歉,这是一个错误,已编辑。但这不是问题。
  • @Arkantos,输出行是问题。
  • @Panther.. 看看 Zlopez 的回答。问题在于 findCustomer 方法中的 while 循环,而不是您提到的输出行中

标签: java arraylist getmethod


【解决方案1】:

错误在这三行:

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

【讨论】:

  • 谢谢,0 个错误,罚款 ;-)
  • 另外我建议你把你的findCustomer() 移到其他班级。 CustomerInfo 是保持某种状态的 java bean,任何试图找到该对象实例的尝试都应该在该类之外,可能在某个 Service 或 DAO 类中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 2013-07-02
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多