4

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.

3
  • 2
    create properties file and add your username, password,dburl in that and add that properties files into .gitignore Commented Dec 13, 2017 at 4:10
  • What do you mean by a properties file? So should I add the username, password, and dburl into a textfile? And then in my code get it by reading the lines in the file? Commented Dec 13, 2017 at 4:11
  • yah! you are correct but it's not textfile 'PROPERTIES' FILE Commented Dec 13, 2017 at 4:13

1 Answer 1

4

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){
}
Sign up to request clarification or add additional context in comments.

10 Comments

OP would be happy to get this solution, but, you didn't added single explanation like why is he suppose to use and what is the property how can he re-use etc. Simply sharing the code to OP may or may not work for OP.
@KalaiselvanA you are again not getting my point. anyway leave it.
@KalaiselvanA So I am trying to use your solution, but whenever I upload my project o github the dbconfig.properties file still gets uploaded onto github. I am not sure why.
@KevinLin can you able to share your .gitignore file
@KevinLin It should be in your root folder, ref:github.com/github/gitignore/blob/master/Java.gitignore
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.