Check if a record exists

Discussion in 'Plugin Development' started by Jozeth, Sep 16, 2013.

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

    Jozeth

    How do I check if something exists on a database.

    For example if a warp named "test" exists under the "name" column? With it throwing an exception ("java.sql.SQLException: Illegal operation on empty result set.")
     
  2. Offline

    DeMaggo

    Code:java
    1. public static boolean playerExists(String name){
    2. boolean b=false;
    3. try {
    4. Statement sta=con.createStatement();
    5. ResultSet exists=sta.executeQuery(
    6. "SELECT COUNT(ID)" +
    7. " FROM yourtablehere" +
    8. " WHERE name='"+test+"';");
    9. exists.next();
    10. b=exists.getInt(1)>0;
    11. exists.close();
    12. sta.close();
    13. } catch (SQLException e) {
    14.  
    15. e.printStackTrace();
    16. }
    17. return b;
    18. }
     
    Jozeth likes this.
  3. Offline

    Jozeth

    Will this only return the stack trace if it can't reach the database?
     
  4. Offline

    DeMaggo

    Yes. This was only an example i took from a plugin I made. The catch block is not of interest there since this plugin wouldn't even start running if the database could not be reached.
     
    Jozeth likes this.
Thread Status:
Not open for further replies.

Share This Page