各种语言都有自己所支持的配置文件,后缀名“.properties”结尾的就是其中之一。

在java连接数据库时,采取读取配置文件的方式,来获取数据库连接。

新建jdbc.properties文件,内容如下:

userName=root
pwd=Lmq2582609/.
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=UTF-8

java的连接代码:

//Properties 是一个类:
Properties prop=new Properties();
InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");//读取文件
prop.load(in);//加载文件
userName = prop.getProperty("userName");
pwd = prop.getProperty("pwd");
url = prop.getProperty("url");
driver = prop.getProperty("driver");
Class.forName(driver);// 驱动
conn = DriverManager.getConnection(url, userName, pwd);// 获取连接

 

相关文章:

  • 2021-09-07
  • 2022-01-14
  • 2021-12-29
  • 2022-01-20
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-23
  • 2022-12-23
  • 2021-11-30
  • 2021-08-12
  • 2022-01-17
  • 2021-08-01
相关资源
相似解决方案