2015-03-13 53 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