Не выводиться ArraylLst java
Не пойму почему не выводиться ArrayList.
Stats class
public void onUpdateReceived(Update update) {
DatabaseHandler databaseHandler = new DatabaseHandler();
Message scanner = update.getMessage();
this.scanner = scanner;
if (scanner != null && scanner.hasText()) {
switch (scanner.getText()){
case "/start":
Long chatId = update.getMessage().getChatId();
this.chatId = chatId;
databaseHandler.recordChatId(chatId);
System.out.println(chatId);
sendMsg(update.getMessage(), "Введите Тэг");
break;
case "admins":
if (update.getMessage().getChatId() == Const.TG_ADMIN_PANEL){
sendMsg(update.getMessage(), "Вы вошли в админку");
DatabaseHandler.listclass listclass = new DatabaseHandler.listclass();
List<DatabaseHandler.test> arrayList = listclass.listChatId();
for (int i = 0; i < arrayList.size(); i++){
System.out.println(i);
}
} else sendMsg(update.getMessage(), "Вы не админ");
break;
default:
member(update);
}
}
Database class
Import java.sql.*;
import java.util.ArrayList;
public class DatabaseHandler extends Configs {
static class test {
private int id;
private int chatid;
int getChatid() {
return chatid;
}
void setChatid(int chatid) {
this.chatid = chatid;
}
int getId() {
return id;
}
void setId(int id) {
this.id= id;
}
}
static Connection dbconnection;
public Connection getDbconnection() throws SQLException {
String conectionString = dbURL + dbName;
dbconnection = DriverManager.getConnection(conectionString, dbUsername, dbPass);
return dbconnection;
}
public static class listclass {
public ArrayList<test> listChatId() {
ArrayList<test> table = new ArrayList<>();
try {
String sql = "SELECT * FROM test";
Statement Stmt = dbconnection.createStatement();
ResultSet resultSet = Stmt.executeQuery(sql);
while (resultSet.next()) {
test test = new test();
test.setChatid(resultSet.getInt(2));
test.setId(resultSet.getInt(1));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return table;
}
}
}
Ответы (1 шт):
Автор решения: Виталий
→ Ссылка
ArrayList не заполняется, поэтому остается пустым, нужно в метод listChartId() добавить одну строку, которая отмечена ниже
public ArrayList<test> listChatId() {
ArrayList<test> table = new ArrayList<>();
try {
String sql = "SELECT * FROM test";
Statement Stmt = dbconnection.createStatement();
ResultSet resultSet = Stmt.executeQuery(sql);
while (resultSet.next()) {
test test = new test();
test.setChatid(resultSet.getInt(2));
test.setId(resultSet.getInt(1));
table.add(test); // эта строка добавлена
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return table;
}
Поправить цикл в методе onUpdateReceived() на
for (DatabaseHandler.test item : arrayList) {
System.out.println(item);
}
А также добавить метод toString() в класс test, например такой
public String toString() {
return "id = " + id + ", chatid" = chatid;
}

