zoukankan      html  css  js  c++  java
  • 原生态jdbc的应用技术

       为了更好的了解jdbc,最近查阅了前期学习的资料,整理归纳了一下,整理出来了一套jdbc常用的工具类。之所以在这里撰文,一来可以和大家共享技术的魅力,二来可以方便以后的查阅方便。以下是一个jdbc的优化过的工具类。

     1 package com.java.lish.utils;
     2 import java.sql.Connection;
     3 import java.sql.DriverManager;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 import java.sql.Statement;
     7 import java.util.ResourceBundle;
     8 
     9 /**
    10  * 
    11  * 工具类 可以不依赖具体的数据库的jar 进一步优化 工具类 配置文件 txt xml properties key=value src 下添加配置文件
    12  * jdbc.properties Properties p = new Properties(); p.load(inputstream)
    13  * 
    14  * 一行代码读取properties 配置文件信息 ResourceBundle 快速读取 properties 文件信息
    15  */
    16 public class JDBCUtils {
    17     static String url;
    18     static String username;
    19     static String password;
    20     static {
    21         // Class.forName("com.mysql.jdbc.Driver");// 实现类的全路径 包名.类名
    22         try {
    23             Class.forName(ResourceBundle.getBundle("jdbc").getString(
    24                     "driverClass"));
    25             url = ResourceBundle.getBundle("jdbc").getString("url");
    26             username = ResourceBundle.getBundle("jdbc").getString("username");
    27             password = ResourceBundle.getBundle("jdbc").getString("password");
    28         } catch (Exception e) {
    29             e.printStackTrace();
    30         }// 实现类的全路径
    31     }
    32 
    33     // 获取连接 Connection
    34     public static Connection getConnection() {
    35         try {
    36             Connection con = DriverManager.getConnection(url, username,
    37                     password);
    38             return con;
    39         } catch (Exception e) {
    40             e.printStackTrace();
    41             throw new RuntimeException("获取连接失败");
    42         }
    43     }
    44 
    45     // 关闭资源 st con
    46     public static void close(Connection con) {
    47         if (con != null) {
    48             try {
    49                 con.close();
    50             } catch (SQLException e) {
    51                 e.printStackTrace();
    52             }
    53         }
    54     }
    55 
    56     // 关闭2个资源
    57     public static void close(Connection con, Statement st) {
    58         if (st != null) {
    59             try {
    60                 st.close();
    61             } catch (SQLException e) {
    62                 e.printStackTrace();
    63             }
    64         }
    65         close(con);
    66 
    67     }
    68 
    69     // ResultSet 存放数据库查询结果集
    70     public static void close(Connection con, Statement st, ResultSet rs) {
    71         if (rs != null) {
    72             try {
    73                 rs.close();
    74             } catch (SQLException e) {
    75                 e.printStackTrace();
    76             }
    77         }
    78         close(con, st);
    79     }
    80 
    81 }

      首先说明的是以上代码是一个完整的工具包资源,需要配合配置文件(使用myEclipse,放在src文件下)一起使用,当然也要把驱动包放到lib目录下,然后就可以很爽的使用该工具类获取数据连接了。配置文件如下:

      jdbc.properties 这个名字不要写错,是和上面工具类对应的,因为工具类要在配置文件读取数据。这里简单配置了mysql和oracle两个,也可以配置更多。

    username=root
    password=root
    url=jdbc:mysql://localhost:3306/usermanage
    driverClass=com.mysql.jdbc.Driver
    
    #connection  oracle 
    #username=system
    #password=orcl
    #url=jdbc:oracle:thin:@localhost:1521:xe
    #driverClass=oracle.jdbc.driver.OracleDriver
    
    #db2 

    当然这只是简单的一种方式,并没有借助于任何第三方jar包和框架,非常简练,也适合初学习者使用。

  • 相关阅读:
    防止IE缓存jquery ajax 内容
    MyBatis中主要类的生命周期和应用范围
    MyBatis学习练习
    MySql 日期格式化函数date_format()
    getFields()和getDeclaredFields()
    Java final修饰形参
    随笔
    Java SSH远程执行Shell脚本实现(转)
    jQuery 属性操作
    [git]解决:git config --global push.default matching
  • 原文地址:https://www.cnblogs.com/sloveling/p/jdbc.html
Copyright © 2011-2022 走看看