java.util.Locale类和java.util.ResourceBundle类的使用-用于读取.properties文件

public static Connection getConn() throws NamingException, SQLException { 

              //获得此 Java 虚拟机实例的当前默认语言环境值
Locale locale = Locale.getDefault();   
             //通过localResource 可以获取到工程下servertype.properties里头参数值
     ResourceBundle localResource = ResourceBundle.getBundle("servertype", locale);      
             //获取键type的值
     String value = localResource.getString("type");   

     Connection conn=null;
    if(value!=null && value.equals("weblogic")){
Context ct = new InitialContext() ;
        DataSource ds = (DataSource) ct.lookup("jdbc/proposal_single_db") ;
        conn = ds.getConnection() ;
    }else if(value!=null && value.equals("tomcat")){
    Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/proposal_db");
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();
    }else if(value!=null && value.equals("jboss")){
    Context initCtx = new InitialContext();
//Context ctx = (Context) initCtx.lookup("java:comp/env");
//Object obj = (Object) ctx.lookup("jdbc/proposal_single_db");
    Object obj = (Object) initCtx.lookup("java:"+"jdbc/proposal_single_db");
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();
conn.setAutoCommit(false);
    }

return conn;
}

猜你喜欢

转载自licongming163.iteye.com/blog/1741817