MySQL help needed

Discussion in 'Bukkit Help' started by Neilnet, Jun 28, 2014.

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

    Neilnet

    Error: http://pastebin.com/G6zZhNMi

    Code:
        public void playerProfileExists(Player player) throws SQLException {
            final String queryCheck = "SELECT * FROM `playerdata` WHERE player = ?";
            final PreparedStatement ps = c.prepareStatement(queryCheck);
            ps.setString(1, player.getName());
            final ResultSet resultSet = ps.executeQuery();
     
            final String name = resultSet.getString("player");
     
            if(name == null){
                System.out.println("Player doesn't exist");
            }else{
                System.out.println("Player exists!");
            }
        }
     
  2. Offline

    Stealth2800

    From looking at the stacktrace, I'd assume the issue is caused by you trying to get a string from an empty result set.
    Code:java
    1. final String name = resultSet.getString("player");


    Check to see if there's anything in the result set before doing any operations on it.
    Code:java
    1. ResultSet resultSet = ...;
    2. int count = 0;
    3. while (resultSet.next()) {
    4. count++;
    5. // Do stuff (put the getString here)
    6. }
    7.  
    8. if (count == 0) {
    9. // Empty
    10. }
     
    Neilnet likes this.
  3. Offline

    Neilnet

  4. Offline

    Stealth2800

    Neilnet You're missing a resultSet.next() call after executing the statement, as shown in the tutorial you linked. ResultSet#next also returns a boolean value, false if there are no more rows. You could probably also use that to check if the result set is empty.
     
    Neilnet likes this.
  5. Offline

    Neilnet



    Yeah,

    This did the trick:

    Code:java
    1. public void playerProfileExists(Player player) throws SQLException {
    2. final String queryCheck = "SELECT * FROM `playerdata` WHERE player = ?";
    3. if(player != null){
    4. PreparedStatement ps = c.prepareStatement(queryCheck);
    5. ps.setString(1, player.getName());
    6. ResultSet resultSet = ps.executeQuery();
    7. if(resultSet.next()){
    8. String name = resultSet.getString("player");
    9. System.out.println("Player " + name + " exists!");
    10. }
    11. else{
    12. System.out.println("Player doesn't exist");
    13. }
    14. }
    15. }


    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page