单例模式

一个类只有一个实例,只能被自身实例化。

  1. 配置properties文件:添加driver、url、user、passwor键
  2. 类本身读取
  Properties prop=null;
  String file="properties文件";
  InputStream is =    ConfigManager.class.getClassLoader().getResourceAsStream(file);
  prop.load(file);
  1. 数据源
  • 配置
    Tomcat目录下conf/context.xml中添加
  • 读取
Context c = new InitialContext();
DataSource d= (DataSource)c.lookup("java:comp/env/jdbc/DB_name");
Connection con= d.getConnection();

你可能感兴趣的:(单例模式)