Storing data from database into a List

Advertisements

I’m trying to add data from a database into a List. I’m getting 1 error when adding the elements to the list and another when returning the list.

Error 1: I’m adding data into the list using an Array and it’s givivng me "Incompatable types: String[] cannot be converted to String". This error occured even after changing "rs.getString()" to " "rs.getObject().toString()".

Error 2: This error appears when I’m returning the list. It’s giving me the "cannot find symbol" error.

Here is the code:

public List<String> findAttendees()throws SQLException{
        try {
            List<String> attendeeList = new ArrayList<>();
            String[] row = new String[5];
            
            // registring driver
            Class.forName(driver);
            
            // setting connection
            Connection conn = DriverManager.getConnection(url, user, password);
            
            // sql query
            String sql = "SELECT * FROM registryDB.registry";
            
            // creating statement
            PreparedStatement prepStmt = conn.prepareCall(sql);
            
            // exexuting query
            ResultSet rs = prepStmt.executeQuery(sql);
            
            while( rs.next() ){ //fetching rows from coloums 
                row[0] = rs.getObject(1).toString();
                row[1] = rs.getObject(2).toString();
                row[2] = rs.getObject(3).toString();
                row[3] = rs.getObject(4).toString();
                row[4] = rs.getObject(5).toString();
                
                attendeeList.add(row); // adding to list
            }
            
            System.out.println(attendeeList);
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(registerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return attendeeList;
}

I have tried changing "rs.getString()" to "rs.getObject().toString()" it still doesn’t change the error.

>Solution :

You are seeing the first error because you are trying to add row, a String[], to attendeeList, a List<String>. If you want to add your string array to your array list, you have to declare the type of the list to be String[]

List<String[]> attendeeList = new ArrayList<>();
String[] row = new String[5];
attendeeList.add(row); // works

The second error you see is due to the attendeeList variable’s scope. You can solve it by declaring attendeeList before your try-catch block

List<String[]> attendeeList = new ArrayList<>();
try {
    // ...    
} catch (ClassNotFoundException ex) {
    // ...
}        
return attendeeList;

Another issue that you will run into with this code is the reuse of the row variable. When you call list.add(row), you’re adding a reference to that object to the array list, not the contents of the object itself. This means that if you later change the contents of the object, those changes will be reflected inside of the list as well. You can resolve this by making a new string array for each db row.

Leave a ReplyCancel reply