Solved Database input

Discussion in 'Spigot Plugin Development' started by HockeyFreak062, Jul 9, 2021.

  1. Hey, I'm trying to add a player in my database when they join.
    When they join the player is created in the 'prisons' table but not in the 'punish' table.
    There is no error in the console.

    My code:
    Code (Java):

    public void createPlayer(Player p){
        try {
            UUID uuid = p.getUniqueId();
            if (!exists(uuid)){
                PreparedStatement ps2 = plugin.SQL.getConnection().prepareStatement("INSERT IGNORE INTO prisons (NAME, UUID, XP, LEVEL, MONEY, COAL, IRON, LAPIS, GOLD, DIAMOND, EMERALD) VALUES (?,?,?,?,?,?,?,?,?,?,?) ");
                ps2.setString(1, p.getName());
                ps2.setString(2, uuid.toString());
                ps2.setInt(3, 0);
                ps2.setInt(4, 1);
                ps2.setInt(5, 100);
                ps2.setInt(6, 0);
                ps2.setInt(7, 0);
                ps2.setInt(8, 0);
                ps2.setInt(9, 0);
                ps2.setInt(10, 0);
                ps2.setInt(11, 0);
                ps2.executeUpdate();

                PreparedStatement ps3 = plugin.SQL.getConnection().prepareStatement("INSERT IGNORE INTO punish (UUID, MUTES, KICKS, BANS) VALUES (?,?,?,?)");
                ps3.setString(1, uuid.toString());
                ps3.setInt(2, 0);
                ps3.setInt(3, 0);
                ps3.setInt(4, 0);
                ps3.executeUpdate();

                return;
            }
        } catch (SQLException e){
            e.printStackTrace();
        }
    }
     
  2. Two things:
    - If the data that will be insered is all default (ignoring the UUID and Name) you should put those defaults straigth in the statement.
    - Could you provide some photos of the SQL tables? And also check if the table is in the same database.
     
  3. I'm new to database coding. I don't really understand what you mean with the first point.

    Here is a photo of the table 'punish'
    [​IMG]
     
  4. I will reformule it:
    If every time a player enters the server for the first time it generates a set of default values to the SQL you could do this in the command statement:
    Code (Java):
    PreparedStatement ps2 = plugin.SQL.getConnection().prepareStatement("INSERT IGNORE INTO prisons
    (NAME, UUID, XP, LEVEL, MONEY, COAL, IRON, LAPIS, GOLD, DIAMOND, EMERALD)
    VALUES (?,?,0,1,100,0,0,0,0,0,0) "
    );
                ps2.setString(1, p.getName());
                ps2.setString(2, uuid.toString());
    As for the table it seems alright, is it in the same database as the other table that works?
     
    • Useful Useful x 1
  5. Ahhhh like that, thanks! i'll change it.
    And yes it is the same database as the other one...
     
  6. Great!
    Could you show the code behind the plugin.SQL.getConnection() ?
     
  7. It's just a localhost for now tho

    Code (Java):

    private String host = "localhost";
    private String port = "3306";
    private String database = "arkadianetwork";
    private String username = "root";
    private String password = "";

    private Connection connection;

    public boolean isConnected(){
        return(connection == null ? false : true);
    }

    public void connect() throws ClassNotFoundException, SQLException{
        if (!isConnected()){
            connection = DriverManager.getConnection("jdbc:mysql://" + host +
                    ":" + port + "/" + database + "?useSSL=false", username, password);
        }
    }

    public void disconnect() {
        if (isConnected()){
            try {
                connection.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    public Connection getConnection() {
        return connection;
    }
     
     
  8. Sorry but I can not see anything wrong with your code.
    I am not still that great with SQL but I believe that someone more experienced than me will solve your problem.
    For a last experiment, try switch the statements places: Make ps3 execute as if it was ps2 and see if it works
     
  9. This doesn't work :(
     
  10. I believe you miss understood, let me show you in code:
    Code (Text):

    PreparedStatement ps3 = plugin.SQL.getConnection().prepareStatement("INSERT IGNORE INTO punish (UUID, MUTES, KICKS, BANS) VALUES (?,?,?,?)");
                ps3.setString(1, uuid.toString());
                ps3.setInt(2, 0);
                ps3.setInt(3, 0);
                ps3.setInt(4, 0);
                ps3.executeUpdate();

    PreparedStatement ps2 = plugin.SQL.getConnection().prepareStatement("INSERT IGNORE INTO prisons
    (NAME, UUID, XP, LEVEL, MONEY, COAL, IRON, LAPIS, GOLD, DIAMOND, EMERALD)
    VALUES (?,?,0,1,100,0,0,0,0,0,0) ");
                ps2.setString(1, p.getName());
                ps2.setString(2, uuid.toString());
    I meant executing ps3 first than ps2
     
  11. This is what I did :( But does the same as first...
    Could it be because it only makes the player when they first join the server? Or am I saying something stupid now?
     
  12. Ngl, but it was a bit xD
    Both are executed inside the same function when the players joins, but just for the record, could you show the PlayerJoinEvent?
     
  13. Code (Java):


    @EventHandler
    public void onJoin(PlayerJoinEvent e){
        Player p = e.getPlayer();
        Main.main.data.createPlayer(p);
        reloadScoreBoard.reloadScoreboard(p);
    }
     
  14. Seems right, I have no idea atp
     
    • Friendly Friendly x 1
  15. Ahh no problem thanks for your time!
     
  16. I found the problem. It was only looking for new joining players. Not to players already registered to the server. So i changed my minecraft account to a new account and it got added....