[Java Tut]use mySQL to store your config

Discussion in 'Resources' started by nil0bject, Sep 18, 2011.

?

want more java tutes?

  1. yes

    89.3%
  2. no

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

    nil0bject

    stepeth the first!
    reference: http://dev.mysql.com/usingmysql/java/, http://zetcode.com/databases/mysqljavatutorial/

    part deuce!
    if you ARE NOT using craftBukkit 1000 or higher {
    put the .jar file into your projects build path.​
    [eclipse: right click your project, properties, java build path, libraries, add external jars]​
    } else if you are using craftbukkit>1000 {
    put the craftbukkit .jar file into your projects build path.​
    [eclipse: right click your project, properties, java build path, libraries, add external jars]​
    }


    3!
    locate your code for reading and writing to your config file
    [eg:]
    Code:
    this.getConfiguration().setProperty("", "");
    this.getConfiguration().getProperty("");
    import the sql library
    Code:
    import java.sql.*;
    add some code to create a couple new config properties for enabling mysql support and recording the mysql server details
    Code:
    cfg.setProperty("mysql.enable", false);
                cfg.setProperty("mysql.address", "localhost");
                cfg.setProperty("mysql.port", "3306");
                cfg.setProperty("mysql.database", "");
                cfg.setProperty("mysql.user", "");
                cfg.setProperty("mysql.pass", "");
    quatro this up!
    check if mysql is enabled and then try to register the mysql jdbc driver
    Code:
    if (cfg.getBoolean("mysql.enable", false)) {
                Statement stmt = null;
                ResultSet rs = null;
                try {
                            Class.forName("com.mysql.jdbc.Driver");
                } catch (Exception e) {
                            e.printStackTrace();
                }
    
    funf?~?!?
    try connecting to the mysql server and database using the supplied details
    Code:
    try {
                Connection con = DriverManager.getConnection("jdbc:mysql://"+cfg.getString("mysql.address")+":"+cfg.getProperty("mysql.port")+"/"+cfg.getString.getString("mysql.database")+"?user="+cfg.getString("mysql.user")+"&password="+cfg.getString("mysql.pass"));
    
                stmt = con.createStatement();
    six, not quite seven!
    try getting all the data/rows from your table and
    Code:
    try {
                rs = stmt.executeQuery("SELECT * FROM myTableName"); //replace myTableName
    assigning the results to variables
    Code:
                while (rs.next()) {
                            String firstValue = rs.getString(1);
                            String secondValue = rs.getString(2);
                            //etc, etc, etc
                }
    if the table doesn't exist yet, creat a new one
    Code:
    } catch (Exception e) {
                //replace myTableName
                stmt.executeUpdate("CREATE TABLE  " + cfg.getString("mysql.database") + ".myTableName(  firstValue text NOT NULL,  secondValue text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1");
    }
    THE END!!!
    and then finish off the original if and try statement
    Code:
                } catch (Exception e) {
                            e.printStackTrace();
                }
    } else {
    //load the config file normally...
    }
    thanks
    I used this technique with my plugin, SpoutPlayerSkins. Hopefully it's of some use to someone else.
    nb. i typed all the code out from memory. it's possible i have made a syntax or spelling error. pls tell me.

    ** reserved for replies to common questions **

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  2. Offline

    Kanlaki101

    I was told that the jdbc connector was already included in the .jar for Bukkit.
    Can you confirm this?

    And if so, will this setup here still work the same?
    This tutorial is might helpful.
     
  3. Offline

    nil0bject

    why, yes it is! i just checked the maven build script
    part deuce! fixed.

    yes, everything works exactly the same
     
  4. Offline

    nil0bject

    Does anyone need an update to this? Or request a new topic?
     
  5. Offline

    DrAgonmoray

    why would you store configurations in MySQL ._.
     
  6. Offline

    nil0bject

    all your data is in one spot. makes life easier.
     
  7. I would like it if you would update this :)
     
  8. Offline

    Goblom

    Datdenkikniet whats with the year old necropost ? There are plenty of MySQL based tutorials out on these forums.
     
  9. Goblom ow, didn't know that (could you link me one?)
     
  10. Offline

    Goblom

Thread Status:
Not open for further replies.

Share This Page