zoukankan      html  css  js  c++  java
  • 【对数据库操作的封装成工具类之jdbc】

    mysql对数据库加载驱动、数据库连接、释放资源的操作封装成工具类

        资源文件的使用:

        使用何种数据库驱动,连接数据库使用的URL、用户名及密码都放在资源文件当中,该资源文件的后缀名为".properties",且该文件的命名一般都为db.properties,放置在src目录下。用该资源文件的优点是能够较大程度的解除代码对数据库连接等的耦合,如果更换数据库或用户名及密码,那么底层的代码也要进行修改,耦合性很大,不利于项目的维护。

        1.加载资源文件

        首先要考虑的是如何把资源文件的内容加载进来?其最重要的是如何获取到资源文件的路径,我们应该考虑到:资源文件是不能用绝对路径来获取的,因为将来我们要部署到服务器上,其路径已经不再是我们现在项目的路径了,而且用相对路径也是不可行的。但项目src目录下的所有文件都会被编译存储到bin目录下,而我们只要找到当前类在bin中的位置就可以找到资源文件的位置了。

        首先获取到当前类的字节码文件,然后再获取到类加载器:

    ClassLoader classLoader = JDBCUtils.class.getClassLoader();

        其次利用类加载器获取资源文件作为流:

    InputStream is = classLoader.getResourceAsStream("db.properties");

        创建资源文件对象,让它加载流,所有的信息全部都加载进Properties对象中,而且它会自动解析资源文件中的键值对:

    properties = new Properties();

    properties.load(is);

      注意:最好将资源文件中的键在工具类中再次包装成常量,以免当键改变时,代码中所有用到键的地方全都要改掉,这样可维护性较差。

      1 package utils;
      2 
      3 import java.io.IOException;
      4 import java.io.InputStream;
      5 import java.sql.Connection;
      6 import java.sql.DriverManager;
      7 import java.sql.PreparedStatement;
      8 import java.sql.ResultSet;
      9 import java.sql.SQLException;
     10 import java.sql.Statement;
     11 import java.util.Properties;
     12 
     13 public class JDBCUtils {
     14     
     15     private static final String DRIVER = "driver";
     16     private static final String URL = "url";
     17     private static final String USERNAME = "username";
     18     private static final String PASSWORD = "password";
     19     private static Properties properties = null;
     20     
     21     static {
     22         try {
     23             // 加载资源文件
     24             properties = new Properties();
     25             ClassLoader classLoader = JDBCUtils.class.getClassLoader();
     26             InputStream is = classLoader.getResourceAsStream("db.properties");
     27             properties.load(is);
     28             
     29             // 加载驱动
     30             String driver = properties.getProperty(DRIVER);
     31             Class.forName(driver);
     32         } catch (ClassNotFoundException e) {
     33             e.printStackTrace();
     34         } catch (IOException e) {
     35             e.printStackTrace();
     36         }
     37     }
     38     
     39     /**功能: 连接数据库
     40      * @return
     41      */
     42     public static Connection getConnection() {
     43         String url = properties.getProperty(URL);
     44         String username = properties.getProperty(USERNAME);
     45         String password = properties.getProperty(PASSWORD);
     46         Connection connection = null;
     47         try {
     48             connection = DriverManager.getConnection(url, username, password);
     49             return connection;
     50         } catch (SQLException e) {
     51             e.printStackTrace();
     52         }
     53         return null;
     54     }
     55     
     56     /**功能: 释放 ResultSet, Statement, Connection资源
     57      * @param resultSet
     58      * @param statement
     59      * @param connection
     60      */
     61     public static void release(ResultSet resultSet, Statement statement, Connection connection) {
     62         try {
     63             if (resultSet != null) {
     64                 resultSet.close();
     65             }
     66             if (statement != null) {
     67                 statement.close();
     68             }
     69             if (connection != null) {
     70                 connection.close();
     71             }
     72         } catch (SQLException e) {
     73             e.printStackTrace();
     74         }
     75     }
     76     
     77     /**功能: 释放ResultSet资源
     78      * @param resultSet
     79      */
     80     public static void release(ResultSet resultSet) {
     81         if (resultSet != null) {
     82             try {
     83                 resultSet.close();
     84             } catch (SQLException e) {
     85                 e.printStackTrace();
     86             }
     87         }
     88     }
     89     
     90     /**功能: 释放Statement资源
     91      * @param statement
     92      */
     93     public static void release(Statement statement) {
     94         if (statement != null) {
     95             try {
     96                 statement.close();
     97             } catch (SQLException e) {
     98                 e.printStackTrace();
     99             }
    100         }
    101     }
    102     
    103     /**功能: 释放Connection资源
    104      * @param connection
    105      */
    106     public static void release(Connection connection) {
    107         if (connection != null) {
    108             try {
    109                 connection.close();
    110             } catch (SQLException e) {
    111                 e.printStackTrace();
    112             }
    113         }
    114     }
    115     
    116     /**功能: 释放PreparedStatement资源
    117      * @param PreparedStatement
    118      */
    119     public static void release(PreparedStatement preparedStatement) {
    120         if (preparedStatement != null) {
    121             try {
    122                 preparedStatement.close();
    123             } catch (SQLException e) {
    124                 e.printStackTrace();
    125             }
    126         }
    127     }
    128 
    129 }
    View Code
  • 相关阅读:
    MLPclassifier,MLP 多层感知器的的缩写(Multi-layer Perceptron)
    linux 内存不足时候 应该及时回收page cache
    关闭swap的危害——一旦内存耗尽,由于没有SWAP的缓冲,系统会立即开始OOM
    使用Networkx进行图的相关计算——黑产集团挖掘,我靠,可以做dns ddos慢速攻击检测啊
    ARIMA模型实例讲解——网络流量预测可以使用啊
    http://www.secrepo.com 安全相关的数据获取源
    什么是HTTP Referer?
    列举某域名下所有二级域名的方法
    HMM(隐马尔科夫模型)——本质上就是要预测出股市的隐藏状态(牛市、熊市、震荡、反弹等)和他们之间的转移概率
    成都优步uber司机第五组奖励政策
  • 原文地址:https://www.cnblogs.com/snow1234/p/7281838.html
Copyright © 2011-2022 走看看