MySQL logging

Discussion in 'Plugin Development' started by metmad22, Apr 11, 2015.

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

    metmad22

    Hello there, I've seen a couple of threads about sending data from a plugin over to a MySQL database. I've tried using some but have had no luck at all.

    I was wondering if someone can show me some code on how to send a player's name when a player runs a command. Example: /logme and it would add my name (metmad22) to the table.

    Thanks!
     
  2. Offline

    Evaluations

    Google. You shouldn't need to be spoon fed when there's literally thousands of examples and tutorials on adding values to a table.

    I'll tell you this much:
    INSERT INTO table
     
  3. Offline

    metmad22

    @Evaluations
    Hello, kind sir.
    As far as I see, I did not specify to be spoon fed. I won't be copying any code. I simply want to see a command be registered in a database. From there on, I will modify the code to my needs.
    Google. Great tool indeed. I'm pretty sure I used it to submit this thread. I also looked around for doing this as I stated in my initial post. "I've seen a couple of threads about sending data from a plugin over to a MySQL database".

    I already know what the "INSERT INTO table" does. That was not my question either.

    Again, thank you for your help.
     
  4. Offline

    SeiRruf

    This is something I am also interested in.

    I've tried examples online, as I am sure did metmad22. The issue? I can't seem to get the plugin to connect to a remote MySQL database, even when I specify a compatible remote one via host.

    A very simple hello-world plugin that connects to the db and drops the command issuers name into a sample table would be great. In PHP I have no problem doing this, but java still isn't my cup of tea. The whole connecting to the DB and sending the MySQL query part. More specifically, a remote DB on another server.

    Many thanks in advanced! Should I find the solution before someone else posts it, I'll be sure to share.
     
  5. Offline

    Gater12

    @metmad22
    Open connection -> Insert -> Close connection.

    Is there anything you need help with specifically?
    What have you tried so far with the code?
     
  6. Offline

    metmad22

    Last edited: Apr 12, 2015
  7. Offline

    Gater12

    @metmad22
    Have you made sure the ip and port are correct for your database? Also, your user and pass are correct and have privileges?
     
  8. Offline

    metmad22

    @Gater12
    Yes, many times. I've tried adding a value from the user manually and it works.
     
  9. Offline

    Aces_Charles

    @metmad22

    I would suggest trying to connect to the database just using JDBC. It may be a problem with the library you are trying to use.
     
  10. Offline

    metmad22

    Last edited by a moderator: Apr 12, 2015
  11. Offline

    mythbusterma

    @metmad22

    1. Don't perform SQL queries on the main thread of the server unless you want to lag the hell out of the server
    2. Why do you need SQL for this anyway? It seems more practical to dump this information to a text file, unless you specifically need SQL features
    3. Your permissions for the database are not set up correctly, probably the wrong host.
     
    SeiRruf likes this.
  12. Offline

    metmad22

    @mythbusterma

    I got everything working. Just a question, how do I make an other thread? Do I extend my class to a Thread? And if so do I put my MySQL code in a runnable?
     
  13. Offline

    mythbusterma

    @metmad22

    You could make your class another thread, although that's certainly not the way I would recommend going about doing that.

    The simplest way is to make a BukkitRunnable and run it asynchronously with the Bukkit scheduler, and yes, put database accessing code in the Runnable.

    Make sure your data is thread safe as well, the easiest way to do so is usually just to make it immutable (i.e. cannot be changed).
     
  14. Offline

    metmad22

    @mythbusterma

    immutable? That's the first time I hear it. How would I make it unchangeable?
     
  15. Offline

    mythbusterma

    @metmad22

    Use primitive fields and make them final.
     
  16. Offline

    metmad22

    @mythbusterma

    Could you please give me a simple example of it? I tried doing it, but it's repeating errors in the console. I think I'm doing this wrong. Thanks
     
  17. Offline

    mythbusterma

  18. Offline

    metmad22

    @mythbusterma

    Nevermind. I just fixed it. Sorry!
    Also 1 more thing. If the database goes offline, the whole server chat (as I am sending parts of it to the db) gets stopped. And when the db gets back online, logs the data.

    How could I make it so if the database is offline, ignore the requests and stop sending the data until it's back on?
     
  19. Offline

    mythbusterma

    @metmad22

    If the database is offline, a test query will fail to return. Run a simple test query to ensure connectivity (you can also try pinging the server somehow, which would probably be ideal). Don't execute queries if the test fails.

    You really should have your SQL server up 24/7 though.
     
  20. Offline

    metmad22

    @mythbusterma

    It's online 24/7. However, the plugin will be running on a big server (100+ players). If by any chance something happens to the database as it's hosted on a custom computer, the whole server's tasks related to the plugin will delay until the db is back online. That's a big issue. I'm not too familiar with it, so I was wondering how I could make a function to check this and ignore requests to connect to the db if it's not online. I'm not really sure how to ping it. Could you maybe give a path so I could try and code this? Thanks a lot!

    Oh and I have my delay from the runnable set to 20 ticks (1 second). Is that fine or should I modify it to something else?
     
  21. Offline

    mythbusterma

    @metmad22

    Why delay it? Just run it immediately after, make sure it's asynchronous. Also, I wouldn't worry about the reliability of the SQL server, it's far more reliable than the MineCraft server is.
     
    Konato_K and nverdier like this.
  22. Offline

    metmad22

    @mythbusterma

    This is what I have.
    Code:
            final boolean finalRecord = record;
            Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() {
                @Override
                public void run() {
                    if (finalRecord) mysqlMethod(player, text, type, xyz);
                }
    
            });
    I fixed the runnable to this.

    I'm pretty sure that's right. Now I still want to make a way to check if the server is online just incase, as data gets filtered through a VPN while connecting to the database. Could you please tell me how?
     
  23. Offline

    mythbusterma

    @metmad22

    Why in the world would you run it through a VPN? That's just a waste of bandwidth.
     
  24. Offline

    metmad22

    @mythbusterma

    Because the server is hosted on a custom computer and the VPN is always on. It however gets disconnected sometimes. That is my issue. I can not disable the VPN sadly, so I need an alternative way of doing this if possible.
     
  25. Offline

    metmad22

    @mythbusterma

    So do you have any idea how I could ping a MySQL server?
     
Thread Status:
Not open for further replies.

Share This Page