12.11.2007

database.properties实例

1. DataBase.properties的内容
2.
3.
4. driver=com.mysql.jdbc.Driver
5. url=jdbc:mysql://127.0.0.1:3306/jpetstore
6. username=root
7. password=dongguoh
8.
9. DataBase.xml的内容
10.
11.
12.
13.
14. PropertiesTest
15. "driver">com.mysql.jdbc.Driver
16. "url">jdbc:mysql://127.0.0.1:3306/jpetstore
17. "username">root
18. "password">dongguoh
19.
20.
21. 下面是测试类
22.
23. package DataBase;
24. import java.io.FileInputStream;
25. import java.io.FileNotFoundException;
26. import java.io.IOException;
27. import java.util.Iterator;
28. import java.util.Properties;
29. import java.util.Set;
30.
31.
32. public class TestProperties {
33.
34. public static void main(String[] args) {
35. TestProperties test=new TestProperties();
36. test.luanch();
37. test.luanchXML();
38.
39. }
40. private void luanch(){
41. Properties ppt=new Properties();
42. try {
43. String path=this.getClass().getResource("/").getPath();
44. path=path+"DataBase/DataBase.properties";
45. FileInputStream fis=new FileInputStream(path);
46. ppt.load(fis);
47. fis.close();
48. Set set=ppt.keySet();
49. Iterator it=set.iterator();
50. System.out.println("*********显示的读取DataBase.properties显示的内容*****");
51. while(it.hasNext()){
52. String id=(String)it.next();
53. System.out.println(id+"="+ppt.getProperty(id));
54. }
55.
56. System.out.println("另外一种显示方式");
57. ppt.list(System.out);
58. } catch (FileNotFoundException e) {
59. System.out.println("找不到DataBase.properties这个文件,或者是路径发生错误");
60. } catch (IOException e) {
61. System.out.println("加载DataBase.properties文件时出错!!");
62. }
63. }
64. private void luanchXML(){
65. Properties ppt=new Properties();
66. try {
67. String path=this.getClass().getResource("/").getPath();
68. path=path+"DataBase/DataBase.xml";
69. System.out.println(path);
70. FileInputStream fis=new FileInputStream(path);
71. ppt.loadFromXML(fis);
72. fis.close();
73. Set set=ppt.keySet();
74. Iterator it=set.iterator();
75. System.out.println("*********显示的读取DataBase.xml 显示的内容*****");
76. while(it.hasNext()){
77. String id=(String)it.next();
78. System.out.println(id+"="+ppt.getProperty(id));
79. }
80. } catch (FileNotFoundException e) {
81. System.out.println("找不到DataBase.xml 这个文件,或者是路径发生错误");
82. } catch (IOException e) {
83. System.out.println("加载DataBase.xml 文件时出错!!");
84. }
85. }
86.
87. }
88.
89.
90.
91. 结果:
92.
93. *********显示的读取DataBase.properties显示的内容*****
94. password=dongguoh
95. url=jdbc:mysql://127.0.0.1:3306/jpetstore
96. driver=com.mysql.jdbc.Driver
97. username=root
98. 另外一种显示方式
99. -- listing properties --
100. url=jdbc:mysql://127.0.0.1:3306/jpetstore
101. password=dongguoh
102. driver=com.mysql.jdbc.Driver
103. username=root
104. /E:/MyJavaProject/Ibatis/WebRoot/WEB-INF/classes/DataBase/DataBase.xml
105. *********显示的读取DataBase.xml 显示的内容*****
106. password=dongguoh
107. url=jdbc:mysql://127.0.0.1:3306/jpetstore
108. driver=com.mysql.jdbc.Driver
109. username=root
110.

没有评论: