jdbcresultsetminecraftbungeecord

How can I get a JDBC4ResultSet from a PreparedStatement?


I'm coding a global bansystem for a Minecraftserver for Bungeecord (in Java). When a player, who is banned, tries to join, he should get kicked with the reason. I try to get the reason from a MySQL-table.

Here's the important Code:

PreparedStatement psn = null;
psn = Methoden.getConnection()
        .prepareStatement("SELECT Grund FROM BanList WHERE Name = '" + pn + "'");
if (psn != null) {
    ResultSet rsn = psn.executeQuery();
//  JDBC4ResultSet rsn = (JDBC4ResultSet) psn.executeQuery();
    if (rsn.next() == true) {
        ev.setCancelReason(
                "\2474[\2476Minepedia\2474]\n\247bDu wurdest aus folgendem Grund gebannt:\n\247c"
                        + rsn.toString()
                        + "\n\247bWenn du denkst, dass es ein Versehen war, dann schreib bitte eine Email an \247cminepedia@web.de\247b!");
        ev.setCancelled(true);
    }
}

It doesn't matter if I use ResultSet or JDBC4ResultSet, I always get com.mysql.jdbc.JDBC4ResultSet@65cda838.


Solution

  • Applying the .toString() method to a ResultSet object will return a string representation of the ResultSet object itself, not the contents of the ResultSet. In your case, the ResultSet object's string representation is "com.mysql.jdbc.JDBC4ResultSet@65cda838".

    You will want to use rsn.getString(1) or rsn.getString("Grund") to retrieve the value returned when you do rsn.next().

    By the way, you're using your PreparedStatement incorrectly as well. You should be doing

    prepareStatement("SELECT Grund FROM BanList WHERE Name = ?);
    

    followed by

    psn.setString(1, pn);
    

    before you do the .executeQuery().