当前位置: 首页 > 工具软件 > JdbcUtils > 使用案例 >

自定义JDBCUtils工具类

杭胜
2023-12-01

目的:简化书写

分析:

​ 1.注册驱动抽取

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

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

​		解决:配置文件

​		jdbc.properties

​		url=jdbc:mysql://localhost:3306/test

​		user=root

​		password=root

​ 3.抽取一个方法释放资源
4.测试工具类

第一步:编写pojo实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    private Integer id;
    private String name;
    private String gender;
    private double salary;
    private Date join_date;
    private Integer dept_id;
}

第二步:编写JDBCUtils工具类

package com.haikang.utils;

import com.mysql.jdbc.Connection;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {

    public static String url = null;
    public static String user = null;
    public static String password = null;

    static {//静态代码块随着类的加载而加载,只加载一次,所以可以在静态代码块中注册驱动,因为驱动的注册只需要一次
        //获得jdbc.properties文件的路径
        Class<JDBCUtils> jdbcUtilsClass = JDBCUtils.class;
        ClassLoader classLoader = jdbcUtilsClass.getClassLoader();
        URL resource = classLoader.getResource("jdbc.properties");
        String path = resource.getPath();

        //读取资源文件
        Properties properties = new Properties();
        try {
            properties.load(new FileReader(path));
             url = properties.getProperty("url");
             user = properties.getProperty("user");
             password = properties.getProperty("password");


            //注册驱动
            Class.forName(properties.getProperty("driver"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /*
    * 获取数据库连接对象访求
    * */
    public static Connection getConnection() throws SQLException {
        return (Connection) DriverManager.getConnection(url,user,password);
    }


    /*
    * 释放资源访求
    * */

    public static  void close(Statement statement, Connection connection)  {
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static  void close( ResultSet resultSet,Statement statement, Connection connection)  {
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

第三步:编写测试类

package com.haikang.utils;

import com.haikang.pojo.Employee;
import com.mysql.jdbc.Connection;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class JDBCUtilsTest {
    public static void main(String[] args) {

        List<Employee> allEmployee = getAllEmployee();
        System.out.println(allEmployee);

    }

    public static List<Employee> getAllEmployee(){
        Employee employee = null;

        List<Employee> lists = new ArrayList<>();

        Connection connection = null;

        Statement statement = null;

        ResultSet resultSet = null;
        //1.导入Jar包

        try {
            //2.注册驱动
            //3.获得数据库连接对象
            connection = JDBCUtils.getConnection();
            //4.定义Sql语句
            String sql = "select * from emp";
            //5.获得执行Sql对象
            statement = connection.createStatement();
            //6.执行Sql语句
            resultSet = statement.executeQuery(sql);
            //7.处理结果
            while (resultSet.next()!=false){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String gender = resultSet.getString("gender");
                double salary = resultSet.getDouble("salary");
                Date join_date = resultSet.getDate("join_date");
                int dept_id = resultSet.getInt("dept_id");

                employee = new Employee(id,name,gender,salary,join_date,dept_id);

                lists.add(employee);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBCUtils.close(resultSet,statement,connection);

        return lists;
    }
}
 类似资料: