我无法使用 BotUser 类从我的数据库中获取数据 + 我不知道如何遍历数据库行

I can't get data from my database using BotUser class + i don't know how to Iterate thru database rows

提问人:Timas_bro 提问时间:3/18/2023 更新时间:3/27/2023 访问量:27

问:

我正在尝试使用我的方法从数据库中检索天数,但它显示如下错误: 不兼容的类型。找到:“org.timuzkas.litbot2.Database.BotUser”,必需:“java.lang.Integer”

数据库.java

package org.timuzkas.litbot2;

import java.sql.*;

public class Database {
    public static final Database instatance = new Database();


    private Statement statement;

    private Database() {
        try {
            Connection connection = DriverManager.getConnection("jdbc:sqlite:bot_db.sqlite");
            statement = connection.createStatement();

            statement.execute("CREATE TABLE IF NOT EXISTS days(nick TEXT PRIMARY KEY, day INT)"    );
            statement.execute("CREATE TABLE IF NOT EXISTS discordid(nick TEXT PRIMARY KEY, discord_id TEXT)"    );

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void writeUser(String nick, String discordid) {
        try {
            statement.execute("DELETE FROM discordid WHERE discord_id=" + discordid);
            statement.execute("DELETE FROM discordid WHERE nick=" + nick);
            statement.execute("DELETE FROM days WHERE nick=" + nick);
            statement.execute("REPLACE INTO discordid VALUES('" + nick + "', '" + discordid + "')");
            statement.execute("REPLACE INTO days VALUES('" + nick + "', '" + 0 + "')");
        } catch (SQLException e) { e.printStackTrace();}

    }
    public BotUser getDays(String nick) {
        try {
            ResultSet resultSet = statement.executeQuery(
                    "SELECT * FROM days WHERE nick = '" + nick + "';"
            );
            if (resultSet.next()) {
                return new BotUser(resultSet.getString(1),resultSet.getString(2));
            } else {
                return null;
            }
        } catch (SQLException e) {e.printStackTrace();}

        return null;
    }

    public BotUser getDsId(String nick)  {
        try {
            ResultSet resultSet = statement.executeQuery(
                    "SELECT * FROM discordid WHERE nick = '" + nick + "';"
            );
            if (resultSet.next()) {
                return new BotUser(resultSet.getString(1),resultSet.getString(2));
            } else {
                return null;
            }
        } catch (SQLException e) {e.printStackTrace();}

        return null;

    }
    public BotUser addDay(String nick) {
        try {
            ResultSet resultSet = statement.executeQuery(
                    "SELECT * FROM discordid WHERE nick = '" + nick + "';"
            );
            Integer days = resultSet.getInt(2);
            statement.execute("DELETE FROM days WHERE nick=" + nick);
            statement.execute("REPLACE INTO days VALUES('" + nick + "', '" + days + "')");
        } catch (SQLException e) { e.printStackTrace();}

        return null;
    }

    public void removeUser(String nick) {
        try {
            statement.execute("DELETE FROM discordid WHERE nick=" + nick);
            statement.execute("DELETE FROM days WHERE nick=" + nick);
        } catch (SQLException e) { e.printStackTrace();}

    }

    public static class BotUser {


        private final String nick;

        private final String discordId;


        public BotUser(String nick, String discordId) {
            this.nick = nick;
            this.discordId = discordId;
        }
        public String getNick() {
            return nick;
        }
        public String getDiscordId() {
            return discordId;
        }
    }

    public static class BotUserdays {


        private final String nick;

        private final Integer days;


        public BotUserdays(String nick, int days) {
            this.nick = nick;
            this.days = days;
        }
        public String getNick() {
            return nick;
        }
        public Integer getDays() {
            return days;
        }
    }
}

TimerHandler.java

package org.timuzkas.litbot2;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import org.timuzkas.litbot2.Database;

import javax.xml.crypto.Data;

public class TimerHandler extends ListenerAdapter implements Listener {
    Map<String, Integer> timerbase = new HashMap<String, Integer>();
    Map<String, Long> timerds = new HashMap<String, Long>();
    Map<String, Integer> btimerbase = new HashMap<String, Integer>();
    Map<String, Long> btimerds = new HashMap<String, Long>();

    @Override
    public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
        String command = event.getName();
        if (command.equals("afkc")) {
            EmbedBuilder eb = new EmbedBuilder();
            Integer nine = 0;
            event.reply("Начинаю процесс поиска афк игроков... Это может занять до 5 минут!. Value: " + btimerbase.get("timuzkas") + ". Size: " + timerbase.size()).setEphemeral(true).queue();
            Integer end = 0;
            for (Map.Entry<String, Integer> set :
                    timerbase.entrySet()) {
                try {
                    Integer days = Database.instatance.getDays(set.getKey()).;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                Integer id = Database.instatance.getDsId(set.getKey());
                Database.instatance.addDay(set.getKey());
                if (Days == 9) {

                }
            }
            eb.setTitle("AFK-SORTER REPORT | Litbot");
            eb.setDescription("Капитан! Мы провели поиск и поняли что:\n  Столько людей будут кикнуты завтра: " + nine + ".\n  Столько людей было кикнуто сегодня: " + end + ".\n Все они были оповещены об этом!");
            eb.setColor(new Color(11, 252, 3));
            eb.setFooter("litbot logger v0.7");
            event.getChannel().sendMessageEmbeds(eb.build()).queue();
        }
    }

    public String add(String user, Integer days, Long discorduserid) {
        Database.instatance.writeUser(user, String.valueOf(discorduserid));
        timerbase.put(user, 0);
        timerds.put(user, discorduserid);
        return null;
    }
    public Integer gett() {
        return timerbase.get("timuzkas");
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent evt) {

    }
}

我做错了什么?

我试图从数据库中获取天数值,但我不能。我也使用 HashMap,因为我不知道如何使用数据库行进行循环。我正在使用Sqlite JDBC。

还有 JDA 和 PaperSpigot API

java 语法错误 bukkit discord-jda

评论


答:

0赞 Wave 3/27/2023 #1

我认为异常的原因可能是 和 的错误返回,但奇怪的是编译器没有告诉您这些无效的返回分配。 此外,您可能希望 getDays 方法使用 BotUserdays 而不是 BotUser。getDays(String)getDsId(String)