JAVA单排日记-2020/3/15-JDBC-工具类JDBCUtils

  • 目的:简化书写,减少重复代码
  1. 注册驱动也抽取
  2. 抽取一个方法获取连接对象
  3. 抽取一个方法释放资源

创建一个util包,盛放JDBC工具类JDBCutil
在这里插入图片描述

抽取一个方法释放资源:

    /**
     * 释放资源
     * */
    //释放2个资源,Statement,Connection
    public static void close2(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //方法重载,释放3个资源,Statement,Connection,ResultSet
    public static void close(Statement stmt, Connection conn, ResultSet res) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (res!=null){
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

抽取一个方法获取连接对象:使用配置文件

src文件夹下创建配置文件jdbc.properties
在这里插入图片描述
使用静态代码块,利用类加载器,读取配置文件,获取值,并且注册工具类

    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /**
     *使用静态代码块,读取配置文件,获取值
     * */
    static {
        //创建properties对象,用于盛放外部引入的类名和类的成员方法
        Properties pro = new Properties();
        //引入配置文件
        //1.创建类加载器
        ClassLoader cl = JDBCutil.class.getClassLoader();
        //2.根据类加载器,获取配置文件,并转化为IO字节流
        InputStream in = cl.getResourceAsStream("jdbc.properties");
        //将读取的IO字节流数据放入Properties集合中
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据键值对,获取类名和成员方法名
        url=pro.getProperty("url");
        user=pro.getProperty("user");
        password=pro.getProperty("password");
        driver=pro.getProperty("driver");
        //注册工具类
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

连接对象:

    /**
     * 抽取一个方法获取连接对象
     */
    public static Connection connection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

总代码

package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCutil {

    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /**
     *使用静态代码块,读取配置文件,获取值
     * */
    static {
        //创建properties对象,用于盛放外部引入的类名和类的成员方法
        Properties pro = new Properties();
        //引入配置文件
        //1.创建类加载器
        ClassLoader cl = JDBCutil.class.getClassLoader();
        //2.根据类加载器,获取配置文件,并转化为IO字节流
        InputStream in = cl.getResourceAsStream("jdbc.properties");
        //将读取的IO字节流数据放入Properties集合中
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据键值对,获取类名和成员方法名
        url = pro.getProperty("url");
        user = pro.getProperty("user");
        password = pro.getProperty("password");
        driver = pro.getProperty("driver");
        //注册工具类
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 抽取一个方法获取连接对象
     */
    public static Connection connection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    /**
     * 释放资源
     */
    //释放2个资源,Statement,Connection
    public static void close(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //方法重载,释放3个资源,Statement,Connection,ResultSet
    public static void close(Statement stmt, Connection conn, ResultSet res) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

使用

package JDBC;

import util.JDBCutil;

import java.sql.*;

public class DemoSetReuslt02 {
    public static void main(String[] args) {
        Statement stamt = null;
        Connection conn = null;
        ResultSet res = null;
        try {
            //1.导入驱动jar包
            //2.注册驱动
            //3.获取数据库连接对象(连接到数据库)
            conn = JDBCutil.connection();
            //4.定义sql语句
            String sql = "select * from account";
            //5.获取执行sql的对象
            stamt = conn.createStatement();
            //6.执行sql
            res = stamt.executeQuery(sql);
            //7.处理结果
            while (res.next()) {
                String name = res.getString(2);
                int n = res.getInt("balance");
                System.out.println("姓名:" + name + " 工资:" + n);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally { //8.释放资源
            JDBCutil.close(stamt,conn,res);
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值