暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

java 数据库配置文件_web 工程读取数据库配置文件的方法

原创 zxd435690974 2023-07-27
231

首先在web工程src目录下新建一个database.properties 文件

内容如下:

user=root

password=root

databaseType=com.mysql.jdbc.Driver

url=jdbc:mysql://192.168.2.232:3306/oa? seUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull

这里的内容随自己的合适而变化,这里不多说了;

在新建一个读取.properties文件新类:

package com.junhai.tamsys.util;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Properties;

public class DatabaseConfigure {

private Properties property;

private FileInputStream inputFile;

private FileOutputStream outputFile;

public DatabaseConfigure() {

property = new Properties();

}

public DatabaseConfigure(String filePath) {

property = new Properties();

try {

inputFile = new FileInputStream(filePath);

property.load(inputFile);

} catch (FileNotFoundException e) {

System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/*

* 重载函数,得到key的值 @param key 取得其值的键 @return key的值

*/

public String getValue(String key) {

if (property.containsKey(key)) {

return property.getProperty(key);

} else

return "";

}

/*

* 重载函数,得到key的值

*

* @param fileName propertys文件的路径+文件名 @param key 取得其值的键 @return key的值

*/

public String getValue(String fileName, String key) {

try {

String value = "";

inputFile = new FileInputStream(fileName);

property.load(inputFile);

inputFile.close();

if (property.containsKey(key)) {

value = property.getProperty(key);

return value;

} else

return value;

} catch (FileNotFoundException e) {

e.printStackTrace();

return "";

} catch (IOException e) {

e.printStackTrace();

return "";

} catch (Exception ex) {

ex.printStackTrace();

return "";

}

}

/*

* 清除properties文件中所有的key和其值

*/

public void clear() {

property.clear();

}

/*

* 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替, 当key不存在时,该key的值是value

* @param key 要存入的键 @param value 要存入的值

*/

public void setValue(String key, String value) {

property.setProperty(key, value);

}

/*

* 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。 @param fileName 文件路径+文件名称 @param

* description 对该文件的描述

*/

public void saveFile(String fileName, String description) {

try {

outputFile = new FileOutputStream(fileName);

property.store(outputFile, description);

outputFile.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

public static void main(String[] args) {

DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");

System.out.println(test.getValue("user"));

System.out.println(test.getValue("databaseType")+";"+test.getValue("url"));

}

}

这样就可以通过key得到相应的value了;

想在这里多说一点是路径问题,java工程和web 工程读取.properties路径是不一样的,我在这里就花了不少时间。

JAVA工程: DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");这样读取就可以了:

web工程这样读取:DatabaseConfigure  dc = new DatabaseConfigure(Thread.currentThread().getContextClassLoader()

.getResource("").getPath()+"database.properties");

weixin_39520149

关注

1

0

打赏首先在web工程src目录下新建一个database.properties 文件

内容如下:
user=root
password=root
databaseType=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.2.232:3306/oa? seUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull
这里的内容随自己的合适而变化,这里不多说了;
在新建一个读取.properties文件新类:
package com.junhai.tamsys.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class DatabaseConfigure {
private Properties property;
private FileInputStream inputFile;
private FileOutputStream outputFile;
public DatabaseConfigure() {
property = new Properties();
}
public DatabaseConfigure(String filePath) {
property = new Properties();
try {
inputFile = new FileInputStream(filePath);
property.load(inputFile);
} catch (FileNotFoundException e) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 重载函数,得到key的值 @param key 取得其值的键 @return key的值
*/
public String getValue(String key) {
if (property.containsKey(key)) {
return property.getProperty(key);
} else
return "";
}
/*
* 重载函数,得到key的值
*
* @param fileName propertys文件的路径+文件名 @param key 取得其值的键 @return key的值
*/
public String getValue(String fileName, String key) {
try {
String value = "";
inputFile = new FileInputStream(fileName);
property.load(inputFile);
inputFile.close();
if (property.containsKey(key)) {
value = property.getProperty(key);
return value;
} else
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
/*
* 清除properties文件中所有的key和其值
*/
public void clear() {
property.clear();
}
/*
* 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替, 当key不存在时,该key的值是value
* @param key 要存入的键 @param value 要存入的值
*/
public void setValue(String key, String value) {
property.setProperty(key, value);
}
/*
* 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。 @param fileName 文件路径+文件名称 @param
* description 对该文件的描述
*/
public void saveFile(String fileName, String description) {
try {
outputFile = new FileOutputStream(fileName);
property.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");
System.out.println(test.getValue("user"));
System.out.println(test.getValue("databaseType")+";"+test.getValue("url"));
}
}
这样就可以通过key得到相应的value了;
想在这里多说一点是路径问题,java工程和web 工程读取.properties路径是不一样的,我在这里就花了不少时间。
JAVA工程: DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");这样读取就可以了:
web工程这样读取:DatabaseConfigure  dc = new DatabaseConfigure(Thread.currentThread().getContextClassLoader()
.getResource("").getPath()+"database.properties");
weixin_39520149
关注
1
0
打赏
0

0

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论