Tutorial Using MySQL In your Plugins!

Discussion in 'Resources' started by -_Husky_-, Mar 1, 2013.

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

    -_Husky_-

    Sorry, got people mixed up.

    Will check over this tonight when I have access to a computer (1 hour or so). If I haven't replied, tag me a few times. :p
     
  2. Offline

    Mang0eZPvP

  3. Offline

    -_Husky_-

    Whipping up code right now.

    Code:java
    1. public boolean tableExists(String table) {
    2. ResultSet rs = mysql.querySQL("SHOW TABLES LIKE '" + table + "';");
    3. if (rs != null) return true;
    4. return false;
    5. }


    This will use the updated version of the MySQL utility, specifically the querySQL.

    https://github.com/Huskehhh/MySQL/blob/master/src/code/husky/mysql/MySQL.java#L90

    If you don't want to use that method, just do a query with that syntax.

    Hope it helps, :)
     
  4. Offline

    Mang0eZPvP

    i was asking to see if a item exists in a table. sorry
     
  5. Offline

    -_Husky_-

  6. Offline

    Mang0eZPvP

  7. Offline

    FakeSwinThry

    Help - It NOT FOUND!
    [​IMG]

    [​IMG]

    I have imported:
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;

    What i do bad?
     
  8. Offline

    hawkfalcon

    Still sexy.
     
    -_Husky_- likes this.
  9. Offline

    1Achmed1

    java.sql.Connection is an unneeded import.
    You may need to import the SQL classes separately, they don't always show up for me.
     
  10. Offline

    -_Husky_-

    You need to catch the SQLException. ;)
     
  11. Offline

    gomeow

    FakeSwinThry
    Your code actually needs to be in the method...
     
    -_Husky_- likes this.
  12. Offline

    1Achmed1

    What -_Husky_- and gomeow are saying, is
    A. You must put this In a method
    B. You need to surround this with try and catch, and catch with SQLException.

    Code:java
    1.  
    2. public void sqlQuery() {
    3. try {
    4.  
    5. } catch (SQLException e) {
    6. e.printStackTrace();
    7. }
    8. }
    9.  


    I'd also recommend prepared statements ;)
     
  13. Offline

    FakeSwinThry

    Sorry I am noob...
    Where i must put this?

    Please, check my code (only one class + imported MySQL) http://pastebin.com/eUKcGat5

    [​IMG]
    I remove java.sql.Conection:
    [​IMG]

    Or can somebody write Example code?
    ps: Insert work

    Very thanks.
     
  14. Offline

    1Achmed1

    First off, you get an error on SQLException because there is nothing in your try statement that will throw the exception. Secondly, you can't call Connection on "c" because "c" was already set as a connection variable in the SQL classes. To fix this, please return the the first post.
     
  15. -_Husky_- for some reason, the MySQL class doesn't exist?
    (in:
    Code:
    MySQL sql = new MySQL(plugin, "host.name", "port", "database", "user", "pass");

    nvm, forgot to download the class :S
    Butttt waaaitt, how do I make it a jar?
    NVM, I fixed it :s
    One little thingie: how would I set up a database?
     
  16. Offline

    -_Husky_-

    CREATE DATABASE databasenamehere;
     
  17. Offline

    CeramicTitan

    How would you create a table?

    Code:
    CREATE TABLE IF NOT EXISTS table_name => now how do i get it to create with values in it? OR can't i?
     
  18. Offline

    gomeow

  19. Offline

    rippin

    -_Husky_- How would you do queries asynchronously? Like returning the resultset?
     
  20. Offline

    mba2012

    So, I'm trying to use this with my plugin, and I keep getting a NullPointerException error on startup. I made the change detailed at the end of the first post, but it Eclipse is telling me that the MySql.open() method doesn't exist. Everything else appears to be working, so I'm stuck. Thanks guys.
     
    Vizerala likes this.
  21. Offline

    Vizerala

    I'm getting the same problem, been trying to connect for ages. Just won't work
     
  22. Offline

    gomeow

    mba2012
    You can't use it as a static method
     
  23. Offline

    mba2012

    Well, is there any other way of using it?
     
  24. Offline

    gomeow

    Non-statically
     
  25. Offline

    mba2012

    Can you explain how? I'm using the connection in a separate class, so do I need to instantiate a new connection?
     
  26. Offline

    gomeow

    Yes. Read the first post, it shows which arguments to use and how to make it function correctly.
     
  27. Offline

    nopresnik

    Love this library!

    I am not too fluent with MySQL and I am making a ban plugin for my own server and I seem to be having a few issues.

    I have a function to check whether the user is banned or not,
    Code:java
    1. public boolean isBanned(String username) {
    2. try {
    3. Statement s = plugin.c.createStatement();
    4. ResultSet res = s.executeQuery("SELECT * FROM bans WHERE username = '" + username.toString() + "';");
    5. res.next();
    6. if (res.getString("username") == null) {
    7. return false;
    8. } else {
    9. return true;
    10. }
    11. } catch (SQLException ex) {
    12. Logger.getLogger(API.class.getName()).log(Level.SEVERE, null, ex);
    13. }
    14. return false;
    15. }


    And this is the command that executes it.
    Code:java
    1. if (command.getName().equalsIgnoreCase("isbanned")) {
    2. if (plugin.api.isBanned(args[0])) {
    3. sender.sendMessage("True");
    4. return true;
    5. } else {
    6. sender.sendMessage("False");
    7. return true;
    8. }
    9. }


    Whenever I run the command and the user is not banned on the server, I get this stacktrace.
    Code:
    java.sql.SQLException: Illegal operation on empty result set.
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935jn
    ks]
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935jnk
    s]
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935jnk
    s]
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935jnk
    s]
            at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935
    jnks]
            at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d50
    0-b2935jnks]
            at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935j
    nks]
            at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616) ~[buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935j
    nks]
            at com.nopmc.nopbans.API.API.isBanned(API.java:24) [NopBans.jar:?]
            at com.nopmc.nopbans.Commands.BanCommand.onCommand(BanCommand.java:35) [NopBans.jar:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500-b2935jn
    ks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d500
    -b2935jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) [buk.jar:git-Bukkit-1.6.4-R2.0-18
    -g071d500-b2935jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:529) [buk.jar:git-Bukkit-1.6.4-R
    2.0-18-g071d500-b2935jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d5
    00-b2935jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d50
    0-b2935jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:535) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d50
    0-b2935jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:447) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d
    500-b2935jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [buk.jar:git-Bukkit-1.6.4-R2.0-18-g071d50
    0-b2935jnks]
    [20:24:59 INFO]: False
    It still works 100%, but the console errors are quite annoying!

    I would appreciate it if someone could help me out in finding what is wrong here.
    Thanks in advance!
     
  28. Offline

    -_Husky_-

    nopresnik

    Can we see line 24 (and surrounding code/method) of API.java?

    Thanks, Husky.
     
  29. Offline

    nopresnik

    -_Husky_-
    Here you go.
    Code:java
    1. public boolean isBanned(String username) {
    2. try {
    3. Statement s = plugin.c.createStatement();
    4. ResultSet res = s.executeQuery("SELECT * FROM bans WHERE username = '" + username.toString() + "';");
    5. res.next();
    6. /*Line 24*/ if (res.getString("username") == null) {
    7. return false;
    8. } else {
    9. return true;
    10. }
    11. } catch (SQLException ex) {
    12. Logger.getLogger(API.class.getName()).log(Level.SEVERE, null, ex);
    13. }
    14. return false;
    15. }


    Thanks.
     
  30. Offline

    -_Husky_-

    Try giving this a go.

    https://gist.github.com/Huskehhh/7865279

    Hope it helps, Husky.
     
Thread Status:
Not open for further replies.

Share This Page