MySQL

Discussion in 'Spigot Plugin Development' started by GamemodeSurvival, Oct 8, 2019.

  1. How to check if something exists? I know you can do WHERE EXSITS but is there just a way to return true or false?
     
  2. electronicboy

    IRC Staff

    You do a select, if you get a result, it exists
     
  3. Select statement and check if the result set has a value by using ResultSet#hasNext()
     
  4. Jup, what he actually means, you run the query something like this:
    Code (Text):
    SELECT username FROM players WHERE uuid = ?
    Than you need to check on num_rows which counts the selected rows. If one row exists, is there a player with that UUID
     
  5. There is no hasNext()
     
  6. I think you are looking for getFetchSize()
     
  7. Anyone has a SQL util class? xD
     
  8. It is just what like everyone is saying. Like the answer is already given 2 times.
    If you don't know how you need to implement SQL into your plugin read this thread:
    https://www.spigotmc.org/wiki/mysql-database-integration-with-your-plugin/

    Once you have the proper knowledge, what you are asking for is this:
    https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
    Once you are retrieving something from the database it gives a Resultset with something or nothing in it.
    And yes, like Kieraaan said you should check for ResultSet#next()
     
  9. I know how, I'm just saying does anyone have one because there is more to sql than just that.
     
  10. Yeah if you google HikariCP, it's the commonly used AP for database connection pooling including SQL.
     
    #10 jusjus112, Oct 9, 2019
    Last edited: Oct 9, 2019
  11. The easiest way to write SQL is through the JDBC. It usually isn’t worth the hassle to go through an ORM if you have one specific edge case it can’t handle. It’s hard to generalize SQL for every possible use case, so you’re better off literally just interacting with SQL through the JDBC (often with a connection/pool underneath like HikariCP or something) rather than using a util.
     
  12. I'm working on an SQL library, feel free to contact me if you want to help me finish it.
     
  13. Basic SQL, or something like Hibernate?


    @OP, it might be useful to look into something like Hibernate with HikariCP as a pool manager. A little complicated when you're trying to learn it at first, but it definitely pays off in the long run.
     

  14. https://github.com/camdenorrb/SweetSqlJ

    https://github.com/camdenorrb/SweetSqlJ/blob/master/src/main/java/me/camdenorrb/sweetsqlj/todo

    Scroll all the way down in the todo list to see basically what I'm going for

    Basically the idea is to make sql interactions as simple as interacting with a collection

    If you want to contribute let me know ahead of time so I can push my current code
     
    • Informative Informative x 1
    • Friendly Friendly x 1
  15. Code (Java):

    public boolean exists(UUID uuid) {
    PreparedStatement ps = DB.getCon().prepareStatement(“SELECT * FROM user WHERE uuid = ?);
    ps.setString(1, uuid);
    ResultSet resultSet = ps.executeQuery();
    return resultSet.next();
    }
    I typed this on my phone but you’ll need to add try catch statement and stuff but this is just the overall idea of how it will work
     
  16. Kind of, but with an ORM twist.