如何在GitHub(Java)上隐藏MySQL连接字符串-用户名和密码 [英] How to hide MySQL Connection String- Username and Password on GitHub (Java)

查看:272
本文介绍了如何在GitHub(Java)上隐藏MySQL连接字符串-用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决当前困境的方法.因此,我正在做一个创建简单POS系统的小项目,并且使用Oracle MySQL数据库存储诸如用户密码,商品名称,价格等信息.我使用Amazon AWS作为主机.当我在代码中连接到它时,我使用

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");

一些用户名,somepassword和Amazon主机url是我的代码中的真实值,出于明显的原因,我只是在使用它.

Some username, somepassword, and amazon host url are the real values in my code, I am just using this for obvious reason.

现在,如果我将代码上传到github,那么我的MySQL连接将变为公共状态,人们可以连接到它.我如何隐藏此信息,但仍将我的代码上传到github?我一直在网上寻找,但是如果有人可以帮助我解决这个问题,那我将只能看到有关PHP的解决方案.

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类提供了从属性文件中获取数据并将数据存储到属性文件中的方法.而且,它可以用来获取系统的属性.

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.

属性文件的优点

如果从属性文件中更改了信息,则不需要重新编译:如果从属性文件中更改了任何信息,则无需重新编译Java类.它用于存储经常更改的信息.

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.

要从属性文件中获取信息,请将属性文件名称创建为.dbconfig.properties

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,当推送到公共存储库以获取有关gitinore的更多参考时,文件将忽略您的dbconfig.properties: https://git-scm.com/docs/gitignore

.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文件

.gitignore file

 /resources/dbconfig.propreties/

用于从Java文件中的属性文件读取数据的Java类

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

这篇关于如何在GitHub(Java)上隐藏MySQL连接字符串-用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆