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!
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
@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.
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.
@metmad22 Open connection -> Insert -> Close connection. Is there anything you need help with specifically? What have you tried so far with the code?
@Gater12 Thank you for reaching out to me. When I try to connect to the host, it's replacing the host with the server's localhost ip (I think). How is that possible and how can I fix this? I'm using this library https://bukkit.org/threads/using-mysql-in-your-plugins.132309/
@metmad22 Have you made sure the ip and port are correct for your database? Also, your user and pass are correct and have privileges?
@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.
@Aces_Charles I just did right now to test it. Same error @Aces_Charles I used the same way as this: https://bukkit.org/threads/tut-how-to-use-jdbc-in-your-plugins-mysql-databases.43445/ and still got the error. I didn't even add a value or create a table in the database. Just on it's own, it's denying the access to connect for some reason.
@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.
@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?
@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).
@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
@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?
@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.
@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?
@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.
@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?
@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.