properties 配置文件及自定义 JDBCUtils 工具类

一、properties 配置文件

相关介绍:

  开发中获得连接的4个参数(驱动、URL、用户名、密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可。

通常情况下,我们习惯使用properties文件,此文件我们将做如下要求:

  1、文件位置:任意,建议src下

  2、文件名称:任意,扩展名为properties

  3、文件内容:一行一组数据,格式是“key=value”.

    (1)key命名自定义,如果是多个单词,习惯使用点分隔。例如:jdbc.driver

    (2)value值不支持中文,如果需要使用非英文字符,将进行unicode转换。

二、properties 文件的创建与编写

1、properties文件的创建

  src路径下建立database.properties(其实就是一个文本文件)

2、properties文件的编写(内容如下)

  driverClass=com.mysql.jdbc.Driver

  url=jdbc:mysql://localhost:3296/mybase

  username=root

  password=123

三、加载配置文件

  采用加载properties文件获得流,然后使用Properties对象进行处理。

  通过类加载器获取流对象

 1         /*
 2          *  加载properties配置文件
 3          *  IO读取文件,键值对存储到集合
 4          *  从集合中以键值对方式获取数据库的连接信息,完成数据库的连接
 5          */
 6         public class PropertiesDemo {
 7             public static void main(String[] args) throws Exception{
 8                 FileInputStream fis = new FileInputStream("database.properties");
 9                 System.out.println(fis);
10                 //使用类的加载器
11                 InputStream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("database.properties");
12                 System.out.println(in);
13                 Properties pro = new Properties();
14                 pro.load(in);
15                 System.out.println(in);                 
16             }
17         }

四、通过读取配置文件,自定义  JDBC 工具类

 1         /*
 2          *  编写数据库连接的工具类,JDBC工具类
 3          *  获取连接对象采用读取配置文件方式
 4          *  读取文件获取连接,执行一次,static{}
 5          */
 6         public class JDBCUtilsConfig {
 7 
 8             //私有构造器
 9             private JDBCUtilConfig(){
10             }
11 
12             private static Connection con ;
13             private static String driverClass;
14             private static String url;
15             private static String username;
16             private static String password;
17 
18             static{
19                 try{
20                     readConfig();
21                     Class.forName(driverClass);
22                     con = DriverManager.getConnection(url, username, password);
23                 }catch(Exception ex){
24                     throw new RuntimeException("数据库连接失败");
25                 }
26             }
27             
28             //私有方法
29             private static void readConfig()throws Exception{
30                 InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
31                  Properties pro = new Properties();
32                  pro.load(in);
33                  driverClass=pro.getProperty("driverClass");
34                  url = pro.getProperty("url");
35                  username = pro.getProperty("username");
36                  password = pro.getProperty("password");
37             }
38 
39             //定义静态方法,返回数据库的连接对象
40             public static Connection getConnection(){
41                 return con;
42             }
43 
44             //释放资源
45             public static void close(Connection con,Statement stat){
46 
47                  if(stat!=null){
48                      try{
49                          stat.close();
50                      }catch(SQLException ex){}
51                  }
52 
53                  if(con!=null){
54                      try{
55                          con.close();
56                      }catch(SQLException ex){}
57                  }
58 
59             }
60 
61             //释放资源
62             public static void close(Connection con,Statement stat , ResultSet rs){
63                  if(rs!=null){
64                      try{
65                          rs.close();
66                      }catch(SQLException ex){}
67                  }
68 
69                  if(stat!=null){
70                      try{
71                          stat.close();
72                      }catch(SQLException ex){}
73                  }
74 
75                  if(con!=null){
76                      try{
77                          con.close();
78                      }catch(SQLException ex){}
79                  }
80 
81             }
82         }                                                                 

猜你喜欢

转载自www.cnblogs.com/fanyizhan/p/10096032.html