public
class
JDBCUtil {
private
static
final
String
dbconfig
=
"dbconfig.properties"
;
// 配置文件路径
private
static
Properties
prop
=
new
Properties();
static
{
try
{
InputStream in = Thread.
currentThread
().getContextClassLoader()
.getResourceAsStream(
dbconfig
);
prop
.load(in);
Class.
forName
(
prop
.getProperty(
"driverClassName"
));
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
}
public
static
Connection getConnection(){
try
{
return
DriverManager.
getConnection
(
prop
.getProperty(
"url"
),
prop
.getProperty(
"username"
),
prop
.getProperty(
"password"
));
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
}
}
dbconfig.properties
driverClassName=
com.mysql.jdbc.Driver
url=
jdbc:mysql://
localhost
:3306/
usermng
?useUnicode=true&characterEncoding=UTF8
username=
root
password=
root