Solved Error occurring when adding data to MySQL

Discussion in 'Plugin Development' started by Ogami Shiro, Dec 8, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Ogami Shiro

    This error will only occur if you try to add data to MySQL. With SQLite, this error does not occur.

    Error:
    Code:
    [18:19:15 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin test v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[patched_1.12.2.jar:git-Paper-1620]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:152) ~[patched_1.12.2.jar:git-Paper-1620]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:685) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1492) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1297) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1620]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?]
            at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1620]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1620]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.RuntimeException: java.sql.SQLException: Before start of result set
            at maxmeitner.test.db.impl.MySQLDatabase.getTest(MySQLDatabase.java:53) ~[?:?]
            at maxmeitner.test.db.Core.getTest(Core.java:32) ~[?:?]
            at maxmeitner.test.Commands.onCommand(Commands.java:31) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[patched_1.12.2.jar:git-Paper-1620]
            ... 15 more
    Caused by: java.sql.SQLException: Before start of result set
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[patched_1.12.2.jar:git-Paper-1620]
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[patched_1.12.2.jar:git-Paper-1620]
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[patched_1.12.2.jar:git-Paper-1620]
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[patched_1.12.2.jar:git-Paper-1620]
            at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790) ~[patched_1.12.2.jar:git-Paper-1620]
            at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2475) ~[patched_1.12.2.jar:git-Paper-1620]
            at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java) ~[?:?]
            at maxmeitner.test.db.impl.MySQLDatabase.getTest(MySQLDatabase.java:46) ~[?:?]
            at maxmeitner.test.db.Core.getTest(Core.java:32) ~[?:?]
            at maxmeitner.test.Commands.onCommand(Commands.java:31) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[patched_1.12.2.jar:git-Paper-1620]
            ... 15 more

    MySQLDatabase
    Code:
    package maxmeitner.test.db.impl;
    
    import com.zaxxer.hikari.HikariConfig;
    import com.zaxxer.hikari.HikariDataSource;
    import maxmeitner.test.db.Database;
    import org.bukkit.entity.Player;
    import java.sql.*;
    
    public class MySQLDatabase  implements Database {
    
        private static HikariDataSource src;
    
        public MySQLDatabase(String host, int port, String database, String user, String password) throws SQLException {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database);
            config.setUsername(user);
            config.setPassword(password);
            config.addDataSourceProperty("cachePrepStmts", true);
            config.addDataSourceProperty("prepStmtCacheSize", 250);
            config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
    
            src = new HikariDataSource(config);
    
            try(Connection cn = connect(); Statement st = cn.createStatement()) {
                st.executeUpdate("CREATE TABLE IF NOT EXISTS test (id INTEGER AUTO_INCREMENT PRIMARY KEY, player TEXT NOT NULL, test1 TEXT NOT NULL)");
            }
        }
    
        private Connection connect() throws SQLException{
            return src.getConnection();
        }
    
        @Override
        public void setTest(Player player, String test1) {
            try(Connection c = connect()){
                c.createStatement().executeUpdate("UPDATE test SET test1 = '" + test1 + "' WHERE player = '" + player.getName() + "'");
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    
    
        @Override
        public String getTest(Player player) {
            try(Connection c = connect()){
                if(c.createStatement().executeQuery("SELECT COUNT(*) FROM test WHERE player = '" + player.getName() + "'").getInt(1) != 0){
                    return c.createStatement().executeQuery("SELECT test FROM test1 WHERE player = '" + player.getName() + "'").getString(1);
                } else {
                    c.createStatement().executeUpdate("INSERT INTO test (player, test1) VALUES ('" + player.getName() + "', 'notSelected')");
                    return "notSelected";
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    
        @Override
        public void close() {
            src.close();
        }
    }
    Core
    Code:
    package maxmeitner.test.db;
    
    import maxmeitner.test.Test;
    import maxmeitner.test.db.impl.MySQLDatabase;
    import maxmeitner.test.db.impl.SQLiteDatabase;
    import org.bukkit.entity.Player;
    import java.io.File;
    import java.sql.SQLException;
    
    public class Core {
        private Database db;
    
        public Core() {
            try {
                if (Test.getPlugin().getConfig().getBoolean("MySQL.enabled")) {
                    db = new MySQLDatabase(
                            Test.getPlugin().getConfig().getString("MySQL.host"),
                            Test.getPlugin().getConfig().getInt("MySQL.port"),
                            Test.getPlugin().getConfig().getString("MySQL.database"),
                            Test.getPlugin().getConfig().getString("MySQL.user"),
                            Test.getPlugin().getConfig().getString("MySQL.password")
                    );
                } else {
                    db = new SQLiteDatabase(new File(Test.getPlugin().getDataFolder(), "test.db"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    
        public String getTest(Player player){
            return db.getTest(player);
        }
    
        public void setTest(Player player, String test1){
            db.setTest(player, test1);
        }
    
        public void close(){
            if(db != null) db.close();
        }
    }
    Database
    Code:
    package maxmeitner.test.db;
    
    import org.bukkit.entity.Player;
    
    public interface Database {
    
        void close();
    
        void setTest(Player player, String test1);
    
        String getTest(Player player);
    
    }
    I found bugs.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 8, 2022
Thread Status:
Not open for further replies.

Share This Page