When the web program on non-web program, placed under src
Extension properties; the format key = value, key separated from general use, value does not support Chinese.;
Loading method in which the configuration file named db.properties
- Direct loading bundle
1 private static String driver ; 2 private static String url ; 3 private static String username ; 4 private static String password ; 5 static{ 6 ResourceBundle bundle = ResourceBundle.getBundle("db"); 7 driver = bundle.getString("driver"); 8 url = bundle.getString("url"); 9 username = bundle.getString("username"); 10 password = bundle.getString("password"); 11 }
- Load file stream
. 1 Private static String Driver; 2 Private static String URL; . 3 Private static String username; . 4 Private static String password; . 5 static { . 6 the try { . 7 // 1. classes obtained by the current class loader . 8 ClassLoader Loader = JDBCUtils_V3. Class .getClassLoader (); . 9 // 2. input stream obtained by the method of the loader 10 the InputStream loader.getResourceAsStream iS = ( "the db.properties" ); . 11 // 3. Create the properties object 12 Properties properties = new Properties(); 13 //4.加载输入流 14 properties.load(is); 15 //5.获取配置文件的信息 16 driver = properties.getProperty("driver"); 17 url = properties.getProperty("url"); 18 username = properties.getProperty("username"); 19 password = properties.getProperty("password"); 20 } catch (IOException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace (); 23 } 24 }