我应该如何从我的数据库中获取所有消息并 return 它
How should I get all the messages from my database and return it
我正在用 Java 制作一个聊天应用程序,我正在将所有消息发送到特定的聊天室。问题是我不知道如何将它们全部放在一个数组中或类似的东西中,以便它可以一次 returned 全部提供给用户。我试过只做一个 while 循环来保持 return 一个数组,但它只有 returns 一次。我想尝试将所有值附加到一个数组中,但我不太确定该怎么做。我见过的所有示例都只是将现有数据(如现有数组)添加到另一个现有数组。请帮忙,我不知道该怎么做。
这是我的代码
@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);
}
}
您在循环结果集时填充 List
。
…
List< String > messages = new ArrayList<>() ;
while( rs.next() ){
message = rs.get… ;
messages.add( message ) ;
}
return List.copyOf( messages ) ;
从循环中删除 return
语句。调用 return
结束循环。
如果您必须return数组而不是列表:
return messages.toArray( new String[0] ) ;
我正在用 Java 制作一个聊天应用程序,我正在将所有消息发送到特定的聊天室。问题是我不知道如何将它们全部放在一个数组中或类似的东西中,以便它可以一次 returned 全部提供给用户。我试过只做一个 while 循环来保持 return 一个数组,但它只有 returns 一次。我想尝试将所有值附加到一个数组中,但我不太确定该怎么做。我见过的所有示例都只是将现有数据(如现有数组)添加到另一个现有数组。请帮忙,我不知道该怎么做。
这是我的代码
@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);
}
}
您在循环结果集时填充 List
。
…
List< String > messages = new ArrayList<>() ;
while( rs.next() ){
message = rs.get… ;
messages.add( message ) ;
}
return List.copyOf( messages ) ;
从循环中删除 return
语句。调用 return
结束循环。
如果您必须return数组而不是列表:
return messages.toArray( new String[0] ) ;