MySQL database integration with your plugin. - Historical

Applied By NicoPlayzTV: Jun 10, 2016 at 7:54 PM

MySQL database integration with your plugin.
nt to add it to ou
[​IMG]
r project.

You add it the same way you added Craftbukkit.jar into your Java Build path. So first select Java build path and then click on libraries, and then click on add external jars. Locate to the place you downloaded the JDBC connector and click OK.

Coding
My favourite part, the coding!

We must create a few variables that can let us connect to the database. This is what we need to create. (Note, these are field variables not local variables).

Code (Java):
        //DataBase vars.
        final String username="YOUR DB USERNAME"; //Enter in your db username
        final String password="YOUR DB PASSWORD"; //Enter your password for the db
        final String url = "jdbc:mysql://db4free.net:3306/DataBaseName"; //Enter URL w/db name

        //Connection vars
        static Connection connection; //This is the variable we will use to connect to database
Now we need to connect to the database using the Connection variable we made that is provided by Java. We do this by assigning properties to our connection variable in the onEnable and then trying to connect. This is the code we need to use:

Again, put this code in the onEnable(); method because we're trying to connect as soon as we start the server.
Code (Java):
try { //We use a try catch to avoid errors, hopefully we don't get any.
                    Class.forName("com.mysql.jdbc.Driver"); //this accesses Driver in jdbc.
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    System.err.println("jdbc driver unavailable!");
                    return;
                }
                try { //Another try catch to get any SQL errors (for example connections errors)
                    connection = DriverManager.getConnection(url,username,password);
                    //with the method getConnection() from DriverManager, we're trying to set
                    //the connection's url, username, password to the variables we made earlier and
                    //trying to get a connection at the same time. JDBC allows us to do this.
                } catch (SQLException e) { //catching errors)
                    e.printStackTrace(); //prints out SQLException errors to the console (if any)
                }
Now it's time to let our plugin close the connection, since we don't want useless connections still lingering around. We're going to do this by closing the connection variable in the onDisable() method. So when the server closes, we don't need the connection to still be there anymore.

Code (Java):
        public void onDisable() {
                // envoke on disable.
                try { //using a try catch to catch connection errors (like wrong sql password...)
                        if(connection!=null && !connection.isClosed()){ //checking if connection isn't null to
                        //avoid recieving a nullpointer
                                connection.close(); //closing the connection field variable.
                        }
                }catch(Exception e){
                                e.printStackTrace();
                   
                        }
   
                }

UNFINISHED BIT, PLEASE BE PATIENT. I WILL ADD THIS INFORMATION ASAP.