I have been trying to find a solution to a dilemma I currently have. So I am doing a small project creating a simple POS system and I use Oracle MySQL database for storing information such as user passwords, item names, prices, etc. I am using Amazon AWS for the host. When I connect to it in my code I use
Connection conn=DriverManager.getConnection("amazon host url","some username","somepassword");
Some username, somepassword, and amazon host url are the real values in my code, I am just using this for obvious reason.
Now, if I were to upload my code to github then my MySQL connection would become public and people could connect to it. How can I hide this information, but still upload my code onto github? I have been looking online, but I can only see solutions regarding PHP, if anyone could help me out with this problem that would be great.
解决方案
Properties File
It can be used to get property value based on the property key. The Properties class provides methods to get data from properties file and store data into the properties file. Moreover, it can be used to get properties of system.
Advantage of properties file
Recompilation is not required if the information is changed from the properties file: If any information is changed from the properties file, you don't need to recompile the java class. It is used to store information which is to be changed frequently.
To get information from the properties file, create the properties file Name as .dbconfig.properties
#DB Properties
db.driver="driverclassname"
db.url=jdbc:mysql://localhost:3306/YOURDBNAME
db.username=USERNAME
db.password=PASSWORD
.gitignore a file will ignore your dbconfig.properties while pushing to the public repository for further reference about gitinore ref : https://git-scm.com/docs/gitignore
.gitignore file
/resources/dbconfig.propreties/
the java class to read the data from the properties filein java file
private ResourceBundle reader = null;
try{
reader = ResourceBundle.getBundle("dbconfig.properties");
Connection conn=DriverManager.getConnection(reader.getString("db.url"),reader.getString("db.username"),reader.getString("db.password"));
}catch(Exception e){
}