-2

これでうまくいくと思ったのですが、残念ながらうまくいきません。エラーが発生します-

タイプ ArrayList のメソッド add(CustomerInfo) は、引数 (文字列) には適用されません

私の目的は、 を返しArraylist、get メソッドでアクセスすることです。に String を使用している場合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()); 

                } 
   // ...
4

1 に答える 1

3

エラーは次の 3 行にあります。

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

上記のように、resultSet.getString()メソッドはStringオブジェクトを返しますが、ArrayList は type のオブジェクトのコンテナである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);
于 2015-03-13T08:24:23.660 に答える