mysql隐藏用户名字,如何在GitHub(Java)上隐藏MySQL连接字符串-用户名和密码

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){

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值