How should I get all the messages from my database and return it

68
August 21, 2021, at 04:40 AM

I'm making a chat app with Java and I'm getting all the messages to a specific chatroom. The issue is I don't know how to put them all in one array or something so it can be returned all at once to the user. I've tried just making a while loop to keep return an array, but it only returns once. I would like to try to just append all the values into one array, but I'm not really sure how to do that. All the examples I've seen, just add existing data like an existing array to another existing array. Please help, I'm not sure what to do.

This is my code

@RequestMapping(value={"/get/messages", "xyz"}, method={RequestMethod.POST,RequestMethod.PUT, RequestMethod.GET})
public String[] getMessages(@RequestBody GETMessages getMessages) throws JSONException, IOException {
    String chatroomID = getMessages.getChatroomID();
    String sessionID = getMessages.getSessionID();
    String username = getUserFromSessionID(sessionID);
    int userStatus = getUserStatusFromSessionID(sessionID);
    int userChatStatus = getUserChatStatusFromUsername(username, chatroomID);
    if(userStatus == 0 || userStatus == 2){
        if(userChatStatus == 0){
            Connection connection = null;
            Statement st = null;
            ResultSet rs = null;

            String message = "";
            String user = "";
            String timeday = "";
            try{
                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://HOST/DBNAME", "USERNAME", "PASSWORD");
                st = connection.createStatement();
                rs = st.executeQuery("SELECT * FROM messages WHERE chatroom = '" + chatroomID +"'");
                while(rs.next()){
                    message = rs.getString(2);
                    user = rs.getString(3);
                    timeday = rs.getString(5);
                    String combined = message + "~" + "user" + "~" + timeday;
                    String[] returnMessage {combined}
                    System.out.println(combined);
                    return(returnMessage);
                }
                String[] returnedMessage = {""};
                return(returnedMessage);
            }
            catch(Exception ex){
                System.out.println("Expection : " + ex.toString());
                String[] returnMessage = {"Expection : " + ex.toString()};
                return(returnMessage);
            }
        }else if(userChatStatus == 1){
            String[] returnMessage = {"You are still pending to join this chatroom"};
            return(returnMessage);
        }else if(userChatStatus == 3){
            systemReport(username, "Tried to access chatroom: " + chatroomID + " while being banned from the chat");
            String[] returnMessage = {"You are banned from this chat"};
            return(returnMessage);
        }else{
            String[] returnMessage = {"We are having issues verifying you are allowed to access this chat"};
            return(returnMessage);
        }
    }else if(userStatus == 1){
        systemReport(username, "Tried to acces chatroom: " + chatroomID + " while banned");
        String[] returnMessage = {"You are not allowed to acces  the server"};
        return(returnMessage);
    }else{
        String[] returnMessage = {"We are having issues making sure your allowed to login"};
        return(returnMessage);
    }
}
Answer 1

You populate a List as you loop the result set.

…
List< String > messages = new ArrayList<>() ;
while( rs.next() ){
    message = ts.get… ;
    messages.add( message ) ;
}
return List.copyOf( messages ) ;

Remove the return statement from inside your loop. Calling return ends the loop.

If you must return an array rather than a list:

return messages.toArray( new String[0] ) ;
READ ALSO
Is there a way of mirroring an inheritance relationship using spring-boot-starter-data-elasticsearch?

Is there a way of mirroring an inheritance relationship using spring-boot-starter-data-elasticsearch?

I am new to Elasticsearch and spring-boot-starter-data-elasticsearch

104
Laravel livewire - System failed to continue execution after validation (success or not)

Laravel livewire - System failed to continue execution after validation (success or not)

Inside my livewire blade file I have this form, and inside this form there is a button:

90
failed to set the attribute in javascript

failed to set the attribute in javascript

I have a web app, frontend using HTML5, backend using Django

46
Creating a performant image skeleton with next/image

Creating a performant image skeleton with next/image

I'm trying to implementing a performant skeleton loader for Nextjs's image component

97