Help with using MySQL

Discussion in 'Plugin Development' started by Cephot, Feb 1, 2016.

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

    Cephot

    Hey guys, so today I've been trying to use MySQL in my plugin. My plugin is a karma plugin, so when you do the command it will get the senders/targets uuid and update that players karma amount. I've been looking at tutorials, guides and source code all day but I haven't had any luck. If you know a good tutorial I could use or if someone could just give me a quick explanation of how to use MySQL in a plugin, I would greatly appreciate it.
     
  2. Offline

    mythbusterma

    @Cephot

    The first rule of using SQL in a plugin is to justify it. It is 9 times out of 10 the wrong option for most plugins, and most people use it make their plugins seem better without adding any real value.

    I can't think of a reason a simple plugin like this would need SQL, other than to use it across multiple servers, in which case, the best way to learn how to use SQL (and in particular, MySQL) is to read the JDBC documentation from Oracle:

    http://dev.mysql.com/doc/connector-j/en/
     
    Konato_K likes this.
  3. Offline

    SjamonDaal

    This plugin is for a server Network so yeah, multiple servers.
     
  4. Offline

    JTGaming2012

    Just search on Youtube :D I'm sure someone has a nice tutorial on there
     
  5. Offline

    MarinD99

    From personal experience, thenewboton has one of the best tutorials and explanations of how to use SQL.
     
  6. Follow this tutorial: (wrong link)
    Pick and choose what you need but I recommend going through it all.

    EDIT: Use this code instead, helping tips put in place :)
    MySQL Handler Code (open)

    -code removed-

    EDIT2: Removed code.
     
    Last edited: Feb 4, 2016
  7. Offline

    WolfMage1

    Im sorry but, That code just has a lot of things wrong with it,

    1, Why create methods to access createStatement(), when you could just put it in the code you're using
    2, Almost always you'd use PreparedStatements over Statements.
    3, You want to do an if not null check on your conn.close() and anything that uses .close() otherwise it will throw an NPE
    4, Your variables should be private if they have no reason to be public, and even then you should access them using getters and setters (if you need to access them outside the class).
     
    Last edited: Feb 2, 2016
    Konato_K likes this.
  8. Offline

    mythbusterma

    @_zircon_

    Please don't post code like that. That code is awful.

    In addition to what Wolf said, there is no reason any of that should be static. A connection is a stateful variable and has no reason to be static.

    @JTGaming2012

    I have yet to find a good tutorial on YouTube. TheNewBoston is barely tolerable in quality. Read the link I posted, look at repositories for examples, and don't cut and paste that people claim "makes it easier."
     
    Konato_K likes this.
  9. Offline

    Konato_K

    I have never seen TheNewBoston, so I can't really have a say on whether his tutorials are good or not, but to say "one of the best" you need to know a lot of tutorials
     
  10. Offline

    MarinD99

    With all due respect, what makes you think I haven't seen a lot of tutorials?
     
  11. Offline

    Konato_K

    @MarinD99 Nothing, I just don't think we should encourage people to just use one source of tutorials, but several, and not only videos, written tutorials, books, stackoverflow, idk.
     
    MarinD99 likes this.
  12. Offline

    MarinD99

    That, I agree with. My apologies for not being thorough enough.
     
  13. Just the connection : link

    Now you will have to create all the statemets, i would use PreparedStatements :

    Code:
    try(PreparedStatement statement = mysql.getConnection().prepareStatement("QUERY"));
    
     
  14. Thanks for the suggestions. I cannot change the code to suit around (2) because I have a lot of classes that I don't want to mess up. I have added if not null checks on closeStatement() and closeConnection(). They all need to be public because all of the methods are in my MySQLHandler class and variables need to be static to be used in static methods.

    Instead of saying "That code is awful", give me tips like @WolfMage1 did. the connection variable has to be static because the connect() method is used in other classes. Also, the DBNAME, DBUSER and DBPASS variables are used in the connect() method as well (which needs to be static).
     
  15. Offline

    mythbusterma

    @_zircon_

    None of that needs to be static. Using something in another class is not a reason to have it static. My suggestion is to not have it be static, and I'm saying it's awful because there's actually no reason to post it.

    Make the connect method non-static and require that the class be instantiated, using those three as instance variables (and possibly arguments to its constructor).

    You really need to read this: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

    Consider these:
    https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
    https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

    And the rest of the sections are good reading as well.

    Yes, you would have to change other code to fix it, but you'll find the fixed code is a lot cleaner and more maintainable.
     
    Konato_K likes this.
  16. Offline

    Konato_K

    IIRC keeping passwords in fields is a bad idea.
     
  17. Offline

    mythbusterma

    @Konato_K

    Make sure they're hashed, then you're good to go.
     
  18. Offline

    mcdorli

    With a modern hash. Some of them was considered insecure in the 80's.
     
    Konato_K and mythbusterma like this.
Thread Status:
Not open for further replies.

Share This Page