您的位置:首页 > 运维架构

Properties文件的读取。

2016-05-12 12:04 295 查看
db.properties

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:ORCL
jdbc.user=root
jdbc.password=

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtility {
private static Properties properties =new Properties() ;
private static String driver = null;
private static String url = null;
private static String user = null;
private static String pwd = null;
static {
try {
// 加载配置文件
properties.load(DBUtility.class.getClassLoader().getResourceAsStream(
"day01/v3/db.properties"));
driver = properties.getProperty("jdbc.driver");
url = properties.getProperty("jdbc.url");
user = properties.getProperty("jdbc.user");
pwd = properties.getProperty("jdbc.password");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static Connection openConnection() throws SQLException {
return DriverManager.getConnection(url, user, pwd);
}
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
System.out.println("关闭连接时发生异常");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: