I'm writing a small game in Java and I want to save user scores into a remote MySQL database. I'm using JDBC right now and I've got the connection working. I can insert and read as desired. The login credentials are hardcoded right now though:
public class DatabaseHandler {
private final String URL = "jdbc:mysql://_ip_goes_here_:3306/";
private final String DB_NAME = "db_name";
private final String USERNAME = "username";
private final String PASSWORD = "password";
//Rest of class
}
I can imagine that one could easily decompile the class file and extract this Information, thus gaining the ability to write into the database as they see fit.
Is there a better option to archive connection or would you use some kind of middleman between the .jar and the database? How would one realize this?
This kind of thing is where a properties file comes in handy. This page has examples on both reading and writing those, incidentally with the kind of parameters that you're dealing with:
http://www.mkyong.com/java/java-properties-file-examples/
Hopefully that amount of separation is sufficient for your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With