Plugin that uses MySQL or SQLite

Discussion in 'Plugin Development' started by homerbond005, Apr 5, 2011.

Thread Status:
Not open for further replies.
  1. Hi!
    The title says it all:
    I would like to program a Bukkit plugin in which I can use the MySQL database. I have already looked at some source codes and searched many forums, but I can not find a suitable tutorial. I have already programmed a few plugins, but I have no idea how I can use the MySQL Database.
    HomerBond005
     
  2. Offline

    Mixcoatl

    It's actually substantially easier now. Bukkit now includes built-in database persistence using Avaje EBeans. Essentially, you decorate the classes and fields that are persistent with Java annotations that describe their database characteristics and the ORM (Object Relational Mapper) figures out how to read and write the data for you.
    I haven't attempted to configure Bukkit to use MySQL but I probably will do so at some point just for my own personal edification. I don't presently see a disadvantage to using SQLite, although for a maintenance side, MySQL is probably a better choice of solution. Either way, none of your code, theoretically, should have to change, to use either approach.
     
  3. A problem with a couple of SQLite drivers is that the entire SQLite database locks up during operation, so I would recommend going for a MySQL database.

    Here's how to enable MySQL in Java for your project:

    First, download the java mysql connector from here: http://www.mysql.com/products/connector/
    Then, extract the bin jar to <your_server_folder>/lib. (create it if it doesn't exist)
    Next, for convenience, rename it to "mysql.jar".
    Now, to enable your projects to use this, edit the MANIFEST.MF file in your .jar OR if your IDE allows it, use a custom manifest file and add this at the bottom of it:
    Code:
    Class-Path: ../lib/mysql.jar
    As you can see, the class path is relational to the plugins folder.
     
  4. Thanks for your help. But where do I find this Manifest.mf? I found MANIFEST.MF in the mysql-conector jar-file. But there's no Class-Path. Only "Bundle-Classpath: ."
    After that, what do I have to write in my plugin? (like PHP?)

    @Mixcoatl: That would be interesting. But I don't know how to use this Avaje EBeans.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  5. It's in your own projects jar.
     
  6. Ahh! In the meta-inf folder!
    Okay, I've found it. But now: What do I have to write in my Class-file in the jar? (Connection, query, disconnection)
     
  7. Offline

    Mixcoatl

    Indeed. It's actually quite fun to not have to do all the work of writing database code.
    It's easier than you could possibly imagine. Just add the appropriate annotations to your database entity classes, expose those classes from the plug-in, and use the object returned by your plug-in's getDatabase method to access the data. Take a look at HomeBukkit for a complete code example.
     
  8. Okay...
    Is there anywhere an easy tutorial?
     
  9. Offline

    Mixcoatl

    HomeBukkit is the best example I can suggest. It's very simple. Only a few files. And it covers most of the important stuff. You can read the documentation on the Avaje site, too, if you're interested in more detailed information.
     
  10. Okay... The documentation helped me. You said that ebeans is integrated. How do I config the MySQL server settings? (password, username, port, adress etc.)
     
  11. Offline

    Mixcoatl

    I believe you set these in your bukkit.yml file.
     
  12. Where is this bukkit.yml? Did you mean <worldname>.yml?
     
  13. Offline

    Mixcoatl

    No, there is a file called Bukkit.yml that lives in your root bukkit directory.
    Code:
    database:
        username: bukkit
        isolation: SERIALIZABLE
        driver: org.sqlite.JDBC
        password: walrus
        url: jdbc:sqlite:{DIR}{NAME}.db
    The above is the default one generated when you run the server.
     
  14. Ahh. Okay. I have found it. Sorry for my stupidity. I'll try it. If I have questions I'll ask you, okay?
     
  15. Offline

    Mixcoatl

    No problem. I've got nothing better to do this weekend. ;)
     
  16. Offline

    Sammy

    @Mixcoatl
    Can you help me with your amazing skills ? have a look at my thread I need a "little" push ^^
     
Thread Status:
Not open for further replies.

Share This Page