Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Insert scores into remote MySQL database without hardcoding login credentials

Tags:

java

mysql

jdbc

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?

like image 697
Marv Avatar asked Mar 19 '26 09:03

Marv


1 Answers

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.

like image 191
Brian S Avatar answered Mar 23 '26 08:03

Brian S