JavaEE(JavaWeb)——JDBC工具类封装

package com.test.utils;

import java.io.File;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCUtils {
    static final String URL = "jdbc:mysql://localhost:3306/aaa";
    static final String USERNAME = "root";
    static final String PASSWORD = "hanmenghao";
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接的接口
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     * 通用查询方法
     * @param sql
     * @param cls
     * @param params
     * @param <T>
     * @return
     */
    public static <T> List<T> executeQuery(String sql, Class<T> cls, Object... params) {
        List<T> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement pstmt =  null;
        ResultSet rs = null;

        try {
            pstmt = conn.prepareStatement(sql);
            if(params != null) {
                for(int i = 0; i < params.length; i++) {
                    pstmt.setObject(i+1, params[i]);
                }
            }
            rs = pstmt.executeQuery();
            while(rs.next()) {
                T t = cls.newInstance();
                Field[] fields = cls.getDeclaredFields();

                for(Field field : fields) {
                    field.setAccessible(true);
                    field.set(t, rs.getObject(field.getName()));
                }
                list.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            close(rs, pstmt, conn);
        }


        return list;
    }

    /**
     * 通用增删改 接口
     * @param sql
     * @param params
     * @return
     */
    public static int executeUpdate(String sql, Object... params) {
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement pstmt = null;
        int result = 0;
        try {
            pstmt = conn.prepareStatement(sql);
            if(params != null) {
                for(int i = 0; i < params.length; i++) {
                    pstmt.setObject(i+1, params[i]);
                }
            }
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 通用单项数据查询
     * @param sql
     * @param cls
     * @param params
     * @param <T>
     * @return
     */

    public static <T> T getOne(String sql,Class<T> cls, Object... params) {
        T t = null;
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement pstmt =  null;
        ResultSet rs = null;

        try {
            pstmt = conn.prepareStatement(sql);
            if(params != null) {
                for(int i = 0; i < params.length; i++) {
                    pstmt.setObject(i+1, params[i]);
                }
            }
            rs = pstmt.executeQuery();
            if (rs.next()) {
                t = cls.newInstance();
                Field[] fields = cls.getDeclaredFields();

                for(Field field : fields) {
                    field.setAccessible(true);
                    field.set(t, rs.getObject(field.getName()));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            close(rs, pstmt, conn);
        }

        return t;
    }

    /**
     * 通用资源关闭接口
     * @param stmt
     * @param conn
     */

    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();
            }
        }
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if(rs != null) {
            try {
                rs.close();
                close(stmt, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42067873/article/details/114145043