2015-03-13 54 views
-2

我認爲這會起作用,但可悲的是它沒有。我得到的錯誤 -返回ArrayList,與get方法結合使用的輸出

在ArrayList類型的方法Add(CustomerInfo)不 適用於參數(字符串)

我的目標是返回Arraylist,並與訪問獲取方法。當我使用的字符串爲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()); 

       } 
    // ... 
+0

你必須要更具體的片約的代碼是給你的錯誤:) – Arkantos 2015-03-13 08:22:01

+0

什麼是「kundenList」這裏的意思。你在哪裏申報。 – Ajit 2015-03-13 08:22:01

+0

@賈尼抱歉,這是一個錯誤,編輯。但這不是問題。 – Panther 2015-03-13 08:23:06

回答

3

該錯誤是在這三行:

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

正如你可以在上面看到,resultSet.getString()方法返回一個String對象,但你的ArrayList是CustomerInfo類型的對象容器,所以你需要創建一個新的CustomerInfo對象,從結果集,然後將值填充其字段這個對象添加到您的ArrayList是這樣的:

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

customerList.add(custInfo); 
+0

謝謝,0錯誤,finee ;-) – Panther 2015-03-13 08:30:59

+0

另外我建議你將'findCustomer()'移動到其他類。 'CustomerInfo'是java bean來保存某個狀態,並且任何嘗試找到該對象的實例都應該在該類之外,可能位於某個Service或DAO類中 – Arkantos 2015-03-13 08:33:04