Tutorial Using MySQL In your Plugins!

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

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

    bfgbfggf

    How to add tables. Needs the example of two columns
    Code:
                MySQL MySQL = new MySQL(Host, port, db, user, pass);
                Connection c = null;
                c = MySQL.open();
    MySQL works. but how to add tables :D where add CREATE TABLE IF NOT EXISTS. and how define a column
    please of example
     
  2. Offline

    gomeow

    bfgbfggf
    It seems to me you don't know any MySQL syntax. Before you can use MySQL in your plugins, you need to learn the language
     
  3. Offline

    NoLiver92

    This is what i used to create my table inside of my database. this code is assumed that you have successfully connected to the database. hope this helps

    Code:
    try
            {
                statement = c.createStatement();
                //ResultSet res = statement.executeQuery("SELECT count(*) FROM information_schema.TABLES WHERE (TABLE_SCHEMA = 'test') AND (TABLE_NAME = 'Example');");
               
                statement.executeUpdate("CREATE TABLE IF NOT EXISTS " + DBTableName + " (Username VARCHAR(20), Slot0 TEXT(100), Slot1 TEXT(100), Slot2 TEXT(100));");
                plugin.getLogger().info("Database Table Has Been Created");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     
    bfgbfggf likes this.
  4. Offline

    bfgbfggf

    hyyym. I have a table
    Code:
                    s.executeUpdate("CREATE TABLE IF NOT EXISTS "
                            + Tabela
                            + " (Player VARCHAR(20), Admin VARCHAR(20), Power INT(4), Reason TEXT(100));");
    works but i cannot add anything to table
    I use: INSERT INTO table (a, b, c) VALUES (1,2,3)
    Code:
                    s.executeUpdate("INSERT INTO "
                            + Tabela
                            + " ( Player, Admin, Power, Reason ) VALUES ("+Power+","+Power+","+Power+","+Power+");");
    It works. But only with numbers
    That not work:
    Code:
                    s.executeUpdate("INSERT INTO "
     
                            + Tabela
     
                            + " (Player, Admin, Power, Reason) VALUES ("+PlayerW+","+PlayerA+","+Power+","+Reason+");");
    if i change "PlayerA,PlayerW,Reason" to random int. work....
    Same with
    INSERT INTO table SET a=1, b=2, c=3

    EDIT

    Solved... :D just add '
    Code:
                    s.executeUpdate("INSERT INTO "
     
                            + Tabela
     
                            + " (Player, Admin, Power, Reason) VALUES ('"+PlayerW+"','"+PlayerA+"','"+Power+"','"+Reason+"');");
     
  5. Offline

    xXSniperzzXx_SD

    When you had:
    Code:
    MySQL MySQL = new MySQL("host.name", port, "user", "pass");
    Connection c = null;
    You forgot the database
    Code:
    MySQL MySQL = new MySQL("host.name", port, database, "user", "pass");
    Connection c = null;
     
  6. Offline

    lukaszmtw

    How can I check if the result is not null? For example i haven't got row with player="test". How can I check if there is a row with player="test"? If there isn't a row with player="test" I want to make it.
     
  7. Offline

    xXSniperzzXx_SD

    lukaszmtw

    Did anyone ever answer your question cause i need to know this too...
     
  8. Offline

    -_Husky_-

  9. Offline

    xXSniperzzXx_SD

    -_Husky_-
    K thanks cause atm i'm stuck with this error
    http://gyazo.com/0cbb4d99a4cdd0092fadf157c1e2f174
    and that line is:
    if(res.getString("Player") == null) {

    -_Husky_-
    By any chance are you there? Cause the person i'm making the plugin for wants MySql... and that's the only part I'm having difficulty with...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2015
  10. Offline

    bfgbfggf

    hyyym xXSniperzzXx_SD

    while (res3.next()) {
    String d1 = res3.getString("col1");
    if (d1 == null) {
    //blah blah
    } else {
    //blah blah
    }
    }
    ?
     
  11. Offline

    xXSniperzzXx_SD

    bfgbfggf

    Wouldnt
    if(res.getString("Player") == null) {
    work too?

    Cause thats whats getting me the error
     
  12. Offline

    MrTwiggy

    How do you deal with the issue of having a database set up and then wanting to add more fields? Example:

    Let's say you have DB called 'tokens', like in the OP, where you want to store how many tokens a player has, and you get a few hundred entries. But then later on, you add in 'credits', and you want to be able to store how many credits a player has along with how many tokens. So ideally, you would want a database where a typical query would look like

    Code:
    statement.executeUpdate("INSERT INTO tokens (PlayerName, Tokens, Credits) VALUES (1,2,3);");
    
    As I understand it, MySQL uses a fixed table system, where the 'slots' (don't know that much about MySQL or the proper terms, so stay with me here) are predefined, like PlayerName is a String with max length of X, Tokens is an integer, etc. So how do you handle adding/removing these 'slots'?
     
  13. Offline

    -_Husky_-

    xXSniperzzXx_SD

    Had no internet on my holiday, apologies.

    Will post the code when I get a chance to get on my computer.
     
  14. Offline

    Polunom

    gomeow -_Husky_- How do I create a table with arguments: "Player Name", "Kills", "Deaths"? What statement do I use?

    Nevermind, I got that. How do I increment Kills by 1?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2015
  15. Offline

    xMinecraft

    -_Husky_-
    15:48:00 CONSOLE: [SEVERE] Could not pass event PlayerJoinEvent to xMinecraft v3.12
     
  16. Offline

    bfgbfggf

    xMinecraft
    hyym maybe try while (res.next())
    ?
    And for save player in DB use nickname.
     
  17. Offline

    xMinecraft

    bfgbfggf and where put this?:
    Code:java
    1. else {
    2. statement.executeUpdate("INSERT INTO `cuentas` (`nick`, `vida`, `hambre`, `exp`, `armadura`, `x`, `y`, `z`, `grupo`) VALUES ('" + jugador + "', '" + vida + "','"+hambre+"','"+exp+"','"+casco+":"+pechera+":"+pantalon+":"+botas+"','"+x+"','"+y+"','"+z+"', 'jugador')");
    3. }

    if is false..
     
  18. Offline

    bfgbfggf

    Hymm maybe just use another syntax. Try that :p
    INSERT INTO `cuentas` (...) VALUES (...) ON DUPLICATE KEY UPDATE

    Edit: if that not work, or you don't want to use that.
    Then just check one of string. And if is null then...
    But that syntax is better option.
     
  19. Offline

    xMinecraft

    bfgbfggf no..
    Emm I try add register if not exist a register of player..
    For this I use if(res.next()) and I don't understand what's bad on the code?
     
  20. Offline

    -_Husky_-

  21. Offline

    teunie75

    Is this in the wrong section?
     
  22. Offline

    xMinecraft

  23. Offline

    Hoolean

    Never really looked into SQL, so I'm sure this will be a great help when I inevitably do!

    Thanks! ^_^
     
  24. Offline

    -_Husky_-

    MrBluebear3 likes this.
  25. Offline

    xMinecraft

    -_Husky_-
    15:48:00 CONSOLE: [SEVERE] Could not pass event PlayerJoinEvent to xMinecraft v3.12

    -_Husky_-

    Code:
    2013-05-09 03:22:45 [SEVERE] Could not pass event PlayerJoinEvent to xMinecraft v3.12
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at net.minecraft.server.v1_5_R3.PlayerList.c(PlayerList.java:204)
            at net.minecraft.server.v1_5_R3.PlayerList.a(PlayerList.java:100)
            at net.minecraft.server.v1_5_R3.PendingConnection.d(PendingConnection.java:129)
            at net.minecraft.server.v1_5_R3.PendingConnection.c(PendingConnection.java:44)
            at net.minecraft.server.v1_5_R3.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
            at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:29)
            at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
            at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
            at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
            at net.xminecraft.plugin.ayuda.onPlayerLogin(ayuda.java:37)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:601)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
            ... 14 more
    
    The line 37:

    String casco = jugador.getInventory().getHelmet().getType().name();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2015
  26. Offline

    -_Husky_-

    Check if the players armour is null before trying to get the type & name?
     
  27. Offline

    xMinecraft

    How?

    String casco = null;

    Before the code?
     
  28. Offline

    np98765

    Moved to Resources!
     
    hawkfalcon, MrBluebear3 and -_Husky_- like this.
  29. Offline

    -_Husky_-

    Code:java
    1. ItemStack casco = jugador.getInventory().getHelmet();
    2. String helmet = "";
    3. if(casco != null) {
    4. helmet = casco.getType().name();
    5. } else {//nothing}


    np98765

    Thanks.
     
  30. Offline

    xMinecraft

    -_Husky_-

    And I replace on the insert and update the name casco for helmet?

    -_Husky_-

    Thanks man!!!
    My plugin works!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2015
Thread Status:
Not open for further replies.

Share This Page