MySQL - If a row exists, return true.

Discussion in 'Plugin Development' started by JjPwN1, Jan 5, 2013.

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

    JjPwN1

    When a player joins the server, I am creating a row for that player, only if the player doesn't already have a row with their username in it. I have tried many ways, but every way I tried to see if the row exists, and return true, then add an else to return false, always returns true or false, even if the row does exist. I am asking if anyone knows a way to successfully do this.

    I have tried many ways, but here's my current code:
    Code:
        public boolean databaseRowExists(String row) throws SQLException{
            try{
                Class.forName("com.mysql.jdbc.Driver");
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://pwncraftpvp.com/pwncraft_mctdm","pwncraft_jjpwn","PASSWORD");
            Statement statement = con.createStatement();
            statement.executeQuery("SELECT * FROM players WHERE username='" + row + "'");
            ResultSet rs = statement.getResultSet();
            if(rs.getString(1) == null){
                return false;
            }else{
                return true;
            }
        }
    This code always returns 'java.sql.SQLException: Illegal operation on empty result set.'
     
  2. Offline

    fireblast709

    return rs.next()
     
  3. Offline

    RealDope

    The way I did it was to create the row as UNIQUE. Then you can use:
    Code:JAVA
    1.  
    2. String statement = "INSERT IGNORE INTO blah blah blah";
    3.  


    If the row already exists, nothing will happen. Otherwise the row is created.

    EDIT: Only skimmed the OP, guess this wasn't really what you were asking lol
     
Thread Status:
Not open for further replies.

Share This Page