zoukankan      html  css  js  c++  java
  • 一个依赖配置属性文件管理的JDBC工具类。

    自己异想天开做的一个工具类,可以用database.properties管理连接的数据库属性。

    有两种加载方式,一种是默认加载方式,即默认加载src(即编译后的classes)目录下的database.properties文件,

    或者自定义文件,实例化的时候传入URL对象加载。

    代码如下

      1 /**
      2  * 此类可以进行不同数据库的连接,操作。<br>
      3  * 此类有一个带有一个String类型参数的构造方法,用来接收一个字符串作为所连接数据库的标识。并加载驱动<br>
      4  * 此类不提供任何初默认数据库连接配置,使用需要手动在类加载路径下配置database.propertiesa属性文件。<br>
      5  * 如果不想在类加载路径下配置database.propertiesa属性文件,可以在实例化的时候传入URL参数进行指定文件初始化。<br>
      6  * 配置格式如下:<br>
      7  * driver=oracle.jdbc.driver.OracleDriver<br>
      8  * url=jdbc:oracle:thin:@127.0.0.1:1521:orcl<br>
      9  * username=username<br>
     10  * password=password<br>
     11  * 每次使用完本类后需要手动调用close()方法关闭所使用的资源.<br>
     12  * 
     13  * @version 1.0.0.0
     14  * @author leaves叶知泉,<a href="http://c5ms.iteye.com/">博客地址c5ms.iteye.com</a>
     15  * QQ:1330771552
     16  */
     17 public class JDBConnection {
     18 
     19     /**
     20      * 构造方法,只调用init方法,此方法不需要提供属性文件路径,会自动在类加载路径下搜寻database.properties文件,默认即src目录下
     21      * 。
     22      */
     23     public JDBConnection() {
     24         init(null);
     25     }
     26 
     27     /**
     28      * 构造方法,只调用init方法,需要制定加载的属性文件,需要一个URL类型参数 。
     29      */
     30     public JDBConnection(URL databaseURL) {
     31         init(databaseURL);
     32     }
     33 
     34     /**
     35      * 实例化方法,指定数据库名字,该类会得到属性配置文件里面配置的参数。
     36      * 
     37      * @param databaseName
     38      *            数据库名
     39      * @throws Exception
     40      */
     41     private void init(URL databaseURL) {
     42         URL path;
     43         Properties prop = new Properties();
     44         InputStream is;
     45         if (databaseURL != null) {
     46             path = databaseURL;
     47         } else {
     48             path = ClassLoader.getSystemResource("database.properties");
     49         }
     50         if (path == null) {
     51             try {
     52                 throw new Exception("没有配置属性文件不能构建");
     53             } catch (Exception e) {
     54                 e.printStackTrace();
     55             }
     56         }
     57         try {
     58             is = new FileInputStream(new File(path.toURI()));
     59             prop.load(is);
     60             is.close();
     61             driver = prop.getProperty("driver");
     62             url = prop.getProperty("url");
     63             username = prop.getProperty("username");
     64             password = prop.getProperty("password");
     65             Class.forName(driver).newInstance();
     66         } catch (Exception e) {
     67             e.printStackTrace();
     68             System.out.println("属性配置读取失败,初始化失败");
     69             return;
     70         }
     71     }
     72 
     73     /**
     74      * getConnection()方法会返回一个连接上构造此类时制定的数据库的Connection.
     75      * 
     76      * @return 目标连接数据库的连接
     77      */
     78     public Connection getConnection() {
     79         Connection conn = null;
     80         try {
     81             conn = DriverManager.getConnection(url, username, password);
     82         } catch (Exception ee) {
     83             ee.printStackTrace();
     84         }
     85         if (conn == null) {
     86             System.err.println("警告: DriverManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" + driver + "\r\n链接位置:" + url
     87                     + "\r\n用户/密码" + username + "/" + password);
     88         }
     89         return conn;
     90     }
     91 
     92     /**
     93      * 功能:执行查询语句 executeQuery()方法执行查询语句并且返回一个ResultSet,封装了执行结果。
     94      * 
     95      * @param sql
     96      *            想要执行的sql语句
     97      * @param update
     98      *            是否返回允许更新的ResultSet
     99      * @param objects
    100      *            此sql语句需要处理的预编译参量
    101      * @return rs 查询结果过对应的ResultSet
    102      * 
    103      */
    104     public ResultSet exeQuery(String sql, boolean update, Object... objects) throws SQLException {
    105         conn = getConnection();
    106         if (update)
    107             prestmt = conn.prepareStatement(sql, ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_INSENSITIVE);
    108         else
    109             prestmt = conn.prepareStatement(sql);
    110         for (int i = 0; i < objects.length; i++) {
    111             prestmt.setObject(i + 1, objects[i]);
    112         }
    113         rs = prestmt.executeQuery();
    114         return rs;
    115     }
    116 
    117     /**
    118      * 功能:执行查询语句 executeQuery()方法执行查询语句并且返回一个ResultSet,封装了执行结果。
    119      * 
    120      * @param sql
    121      *            要执行的查询语句,此语句是预编译语句。
    122      * @return 执行给定查询语句后返回封装了执行结果的ResultSet<br>
    123      *         注意:此ResultSet是可滚动但通常不受 ResultSet 底层数据更改影响,并且不可更新的。
    124      */
    125     public ResultSet exeQuery(String sql) {
    126         try {
    127             conn = getConnection();
    128             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    129             rs = stmt.executeQuery(sql);
    130         } catch (SQLException ex) {
    131             System.err.println(ex.getMessage());
    132         }
    133         return rs;
    134     }
    135 
    136     /**
    137      * 功能:执行更新操作 executeUpdate()方法执行更新语句,并且返回一个int值,包含给定查询所生成数据的 ResultSet 对象
    138      * 
    139      * @param sql
    140      *            要执行的查询语句,此语句是预编译语句
    141      * @param objects
    142      *            此sql语句需要处理的预编译参量
    143      * @return result 包含给定sql语句操作的数据条数,如果执行不成功,则返回0
    144      */
    145     public int exeUpdate(String sql, Object... objects) {
    146         int result;
    147         try {
    148             conn = getConnection();
    149             prestmt = conn.prepareStatement(sql, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE);
    150             for (int i = 0; i < objects.length; i++)
    151                 prestmt.setObject(i + 1, objects[i]);
    152             result = prestmt.executeUpdate();
    153         } catch (SQLException e) {
    154             e.printStackTrace();
    155             result = 0;
    156         }
    157         return result;
    158 
    159     }
    160 
    161     /**
    162      * 功能:执行更新操作 executeUpdate()方法执行更新语句,并且返回一个int值,包含给定查询所生成数据的 ResultSet 对象
    163      * 
    164      * @return result 包含给定sql语句操作的数据条数,如果执行不成功,则返回0
    165      */
    166     public int exeUpdate(String sql) {
    167         int result = 0;
    168         try {
    169             conn = getConnection();
    170             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    171             result = stmt.executeUpdate(sql);
    172         } catch (SQLException ex) {
    173             result = 0;
    174         }
    175         return result;
    176     }
    177 
    178     /**
    179      * 功能:关闭数据库的连接 close()方法关闭所使用的资源.
    180      */
    181     public void close() {
    182         try {
    183             if (rs != null)
    184                 rs.close();
    185             if (prestmt != null)
    186                 prestmt.close();
    187             if (stmt != null)
    188                 stmt.close();
    189             if (conn != null)
    190                 conn.close();
    191         } catch (Exception e) {
    192         }
    193     }
    194 
    195     private String driver;
    196     private String url;
    197     private String username;
    198     private String password;
    199 
    200     private Connection conn = null;
    201     private Statement stmt = null;
    202     private ResultSet rs = null;
    203     private PreparedStatement prestmt = null;
    204 
    205 }
    206  属性文件如下:
    207 
    208 #######################
    209 ###        ORACLE        ###
    210 #######################
    211 
    212 #driver=oracle.jdbc.driver.OracleDriver
    213 #url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    214 #username=scott
    215 #password=tiger
    216 
    217 
    218 
    219 #######################
    220 ###        MYSQL        ###
    221 #######################
    222 driver=com.mysql.jdbc.Driver
    223 url=jdbc:mysql://127.0.0.1:3306/file
    224 username=root
    225 password=admin
    天行健君子以自强不息。
  • 相关阅读:
    log4net(c#) 配置及使用
    【转】JMeter试用手记
    【转】性能测试工具JMeter的使用技巧
    【转】JMeter基础之——录制脚本
    【转】Jmeter基础之——jmeter基础概念
    【转】JMeter基础之——一个简单的性能测试
    【转】JMeter入门
    【转】Jmeter压力测试模拟并发
    【转】JMeter Tutorial的安装和具体操作
    【转】JMeter代理录制脚本
  • 原文地址:https://www.cnblogs.com/mrye/p/2442810.html
Copyright © 2011-2022 走看看