-3

I have a problem with getting the value from a list. For example, I create an ArrayList from a ResultSet. I want to compare the values of this ResultSet to another ResultSet. My approach is to convert the ResultSets to ArrayLists and then compare them.I already used String.append() but failed to obtain the string.
How to get the value from from the ArrayList?
Can a simple solution be provided?

my code:

main class:

String my_query = "SELECT * FROM test.test_application";

Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Boolean result = false;

DatabaseConnection cc = new DatabaseConnection();
connection = cc.connect();

if (connection != null) {
    pstmt = connection.prepareStatement(my_query);
    rs = pstmt.executeQuery();

    TestingModel tm = new TestingModel();
    Gson gs = new Gson();
    ArrayList<TestingModel> list = new ArrayList<TestingModel>();

    while(rs.next()) {
        tm.setClientID(rs.getInt(1));
        tm.setClientName(rs.getString(2));
        tm.setClientAddress(rs.getString(3));
        tm.setClientTelpNumber(rs.getString(4));
        tm.setClientEmail(rs.getString(5));
        list.add(tm);
    }
    System.out.println(gs.toJson(list));

    StringBuilder string = new StringBuilder();

    for (int i = 0; i < list.size(); i++) {
        System.out.println(string.append(list.get(i).toString()));
    }
}

model:

public class TestingModel {

    private int clientID;
    private String clientName;
    private String clientAddress;
    private String clientTelpNumber;
    private String clientEmail;
    // setter and getter
}
Shankha057
  • 1,296
  • 1
  • 20
  • 38
Albertus Bobby
  • 75
  • 2
  • 14

1 Answers1

0

What you can do is to override the equals(Object obj) method in your TestingModel class. There,you can use the String.equals(String s) or String.equalsIgnoreCase(String s) depending on your need and also check for equality of other data members.

In you main class, simply iterate through the 'Lists that you have obtained from the ResultSets and call the equals method.

References:

Shankha057
  • 1,296
  • 1
  • 20
  • 38