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.
@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/
From personal experience, thenewboton has one of the best tutorials and explanations of how to use SQL.
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 (Move your mouse to reveal the content) MySQL Handler Code (open) MySQL Handler Code (close) -code removed- EDIT2: Removed code.
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).
@_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."
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
@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.
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"));
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).
@_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.