[TUT] How To Use JDBC In Your Plugins (MySQL Databases)

Discussion in 'Resources' started by cheese5505, Oct 28, 2011.

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

    cheese5505

    I was looking around, and i couldn't find a good tutorial on how to use MySQL in your plugins. Eventually, i found out. So i'm gonna show you how!

    First, we need to make 3 variables:
    Code:java
    1. String user = "YOUR_USER";
    2. String pass = "YOUR_PASSWORD";
    3. String url = "jdbc:mysql://localhost:3306/database"; //localhost: Your MySQL host, 3306: Your MySQL port (3306 is the main), database: Your MySQL database
    4.  

    Now, we need to make the function/whatever you call it:
    Code:java
    1. public void SampleFunction() throws SQLException { //Change "SampleFunction" to your own function name (Can be anything, unless it already exists)
    2. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
    3. PreparedStatement sampleQueryStatement = conn.prepareStatement("YOUR QUERY"); //Put your query in the quotes
    4. sampleQueryStatement.executeUpdate(); //Executes the query
    5. sampleQueryStatement.close(); //Closes the query
    6. conn.close(); //Closes the connection
    7. }
    8.  

    Now, we need to add this into the onEnable() (Or a command, etc)
    Code:java
    1. public void onEnable() {
    2. try {
    3. create_tablesConnect(); //Runs the function
    4. } catch (SQLException e) {
    5. e.printStackTrace();
    6. }
    7. }
    8.  

    So, thats about it for using JDBC in your plugins. In the future, i will be making a more advanced tutorial, where you can get your user, pass and url from a config file. Thanks for reading! I hope this helped you. If i missed something or have a request, tell me in the comments!
    Github Examples
     
    mine-care and acuddlyheadcrab like this.
  2. Offline

    thehutch

    Thanks been looking to implement MYSQL into some of my plugins, however 2 things do we need a library/external jar for this if so where do we get it? and secondly any tutorials on queries or how to add and retrieve stuff from the database :D Thank you
     
  3. Offline

    cheese5505

    1: You only need to import *looks at code*
    Code:java
    1. import java.sql.Connection;
    2. import java.sql.DriverManager;
    3. import java.sql.PreparedStatement;
    4. import java.sql.SQLException;

    2: Ill add that, thanks!

    Also, if anyone needs help, and if there getting errors, check this out: http://dev.bukkit.org/paste/4178/ I know theres probally a better way, i just do it this way anyways :3

    Also, ill be doing some other tutorials, like saying /create and it creates some tables.
     
  4. Offline

    lmc

    I advise you to look into database pooling also, more efficent.
     
  5. Offline

    cheese5505

    I Probally Will.
     
  6. Offline

    herghost

    Hi cheese, are you planning on continuing with this tut? its bloody useful :)

    For anyone that gets stuck trying to add a Auto increment, you must also set KEY:

    Code:
    public void create_tables() throws SQLException
        {
            Connection conn = DriverManager.getConnection(url, user, pass);
            PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE users (p_id int NOT NULL AUTO_INCREMENT KEY)");
            sampleQueryStatement.executeUpdate();
            sampleQueryStatement.close();
            conn.close();
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    cheese5505

    I will be, but right now is a busy time. I'm going to college, but I'll still update.
    Thanks! I'll add that in, zh00 get cr3dit
     
  8. Offline

    Fishrock123

    Can someone explain to me what exactly I should be putting here?
     
    FlamingArmor likes this.
  9. Offline

    cheese5505

    For user var, put your MySQL username, for pass, put your MySQL password, and for URL, replace "localhost" with your MySQL hosname, and replace "database" with your MySQL database name.
     
  10. Offline

    Fishrock123

    Uh. Never mind I guess. I clear have no idea how to make sense of this.
     
    FlamingArmor likes this.
  11. Offline

    beatcomet

Thread Status:
Not open for further replies.

Share This Page