單例模式
此類(lèi)只能存在一個(gè)實(shí)例對(duì)象泛啸,且只能由本身創(chuàng)建绿语,別的類(lèi)調(diào)用需通過(guò)本身創(chuàng)建的公共訪問(wèn)的方法
懶漢模式:
Properties properties
private static LazySingle lazySingle=null;
private LazySingle(){
String file="文件名";
InputStream is=LazySingle.class.getClassLoader().getResourceAsStream(file);
properties.load(is);
}
public LazySingle getLazySingle(){
????if(lazySingle==null){
? ? ? ? synchronized(LazySingle.class){
? ? ? ? ? ? ? ? lazySingle=new LazySingle();
????????????}
????}
return lazySingle;
}
public String getPro(String para){
? ? return properties.getProperties(para);
}
public static void main(String [] args){
? ? LazySingle.getLazySingle.getPro(String para);
}
JNDI
配置xml文件
Tomcat安裝文件夾/conf/context.xml文件中,在根節(jié)點(diǎn)內(nèi)添加resource節(jié)點(diǎn)
<Resource name="jdbc/news"? ? ? ? ? ? ? ? ? ? jdbc/news是自定義名字
? ? ? ? ? auth="Container"? type="javax.sql.DataSource"? maxActive="100"
? ? ? ? ? maxIdle="30" maxWait="10000" username="root"? password="root"
? ? ? ? ? driverClassName="com.mysql.jdbc.Driver"
? ? ? ? ? url="jdbc:mysql://127.0.0.1:3306/newsmanagersystem?characterEncoding=UTF-8"/>
Context context = new InitialContext();
?DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/news");
?connection = dataSource.getConnection();