zoukankan      html  css  js  c++  java
  • 抽取JDBC工具类 : JDBCUtils

    • * 目的:简化书写
    • * 分析

    1. 注册驱动也抽取

    2. 抽取一个方法获取连接对象

    * 需求:不想传递参数(麻烦),还得保证工具类的通用性。

    * 解决:配置文件

    jdbc.properties

    url=

    user=

    password=

       

    JdbcUtils工具类代码

    20201027

    20:30

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    124

    125

    126

    127

    128

    129

    130

    131

    132

    package com.demo.day13.jdbc_utils;

       

    import java.io.FileReader;

    import java.io.IOException;

    import java.net.URL;

    import java.net.URLDecoder;

    import java.sql.*;

    import java.util.Properties;

       

    public class JdbcUtils {

       

    // 解决JDBC 代码重复度太高 --- 将几个固定的功能 抽取封装到 几个函数中

    // 1、为了简便用户操作

    // 2、具有通用性

       

    private static String url1 = null;

    private static String password = null;

    private static String user = null;

    private static String driver = null;

       

    public static void main(String[] args) {

    JdbcUtils jdbcUtils = new JdbcUtils();

    jdbcUtils.getConnection();

    }

       

    //文件的读取,只需要读取一次即可拿到这些值。使用静态代码块

    static {

    //1. 创建Properties集合类。

    Properties pro = new Properties();

    try {

       

    // 就可以动态获取src目录下 "jdbc.properties" 的绝对路径了

    //获取src路径下的文件的方式--->ClassLoader 类加载器

    ClassLoader classLoader = JdbcUtils.class.getClassLoader();

    URL resour = classLoader.getResource("jdbc.properties");

    String path = resour.getPath();

    path = URLDecoder.decode(path,"utf-8");

       

    //2. 加载文件

    pro.load(new FileReader(path));

    } catch (IOException e) {

    e.printStackTrace();

    }

       

    //3. 获取数据,赋值

    url1 = pro.getProperty("url");

    password = pro.getProperty("password");

    user = pro.getProperty("username");

    driver = pro.getProperty("driver");

       

    try {

    //4. 注册驱动

    Class.forName(driver);

    } catch (ClassNotFoundException e) {

    e.printStackTrace();

    }

    }

       

       

    // Class.forName()

       

       

    // getConnection

       

    /**

    * 获取连接

    * @return 连接对象

    */

    public static Connection getConnection() {

    // return DriverManager.getConnection("jdbc:mysql://localhost:3308/day4?serverTimezone=UTC","root","root");

       

    try {

    return DriverManager.getConnection(url1, user, password);

    } catch (SQLException throwables) {

    throwables.printStackTrace();

    return null;

    }

    }

       

    // execute.Update == > conn,stat

    // executeQuery == ? conn,stat,ResultSet rs

    /**

    * 释放资源

    * @param stat

    * @param conn

    */

    public static void close(Connection conn, Statement stat){

    if (stat != null){

    try {

    stat.close();

    } catch (SQLException throwables) {

    throwables.printStackTrace();

    }

    }

       

    if (conn != null){

    try {

    conn.close();

    } catch (SQLException throwables) {

    throwables.printStackTrace();

    }

    }

    }

       

    public static void close(Connection conn, Statement stat,ResultSet rs){

       

    if (rs != null){

    try {

    rs.close();

    } catch (SQLException throwables) {

    throwables.printStackTrace();

    }

    }

       

    if (stat != null){

    try {

    stat.close();

    } catch (SQLException throwables) {

    throwables.printStackTrace();

    }

    }

       

    if (conn != null){

    try {

    conn.close();

    } catch (SQLException throwables) {

    throwables.printStackTrace();

    }

    }

    }

       

    }

     

  • 相关阅读:
    三比三:无线监控优点与传输技术剖解
    手把手教你学习FPGA系列视频教程_救护车鸣笛声
    世纪大争论:Linux还是GNU/Linux?
    [分享]我的LABVIEW快速开发串口测试软件实例
    高能效系统的功耗优化技术
    python学习小脚本多用户登录
    python学习三级菜单省市选择
    python学习购物车小程序
    python学习语句总结
    python学习猜年龄小游戏
  • 原文地址:https://www.cnblogs.com/li330273334/p/13887474.html
Copyright © 2011-2022 走看看