JDBC工具类

JDBC工具类

最近学了JDBC,使用的时候只要写好sql语句和要使用的数据就能返回结果 感觉非常好用

public class JDBCUtil {
    static Properties properties  = new Properties();
    static {
        InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(inputStream);
            Class.forName(properties.getProperty("driverClass"));
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("username"),properties.getProperty("password"));
    }
    public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn){
        try {
            if(rs != null) rs.close();
            if(pstmt != null) pstmt.close();
            if(conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
     * @description:增删改
     * @param sql
     * @param params
     * @return: int
     * @author: dodo
     * @time: 2022/9/8 0:04
     */
    public static int executeUpdate(String sql,Object... params){
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        try {
            conn = JDBCUtil.getConnection();
            preparedStatement = conn.prepareStatement(sql);
            if(params != null  && params.length > 0){
                for (int i = 0 ; i <params.length;i++) {
                    preparedStatement.setObject(i+1,params[i]);
                }
            }
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(null,preparedStatement,conn);
        }
        JDBCUtil.close(null,preparedStatement,conn);
        return 0;
    }
/*
 * @description:多个结果
 * @param rs
 * @param clazz
 * @return: java.util.List<T>
 * @author: dodo
 * @time: 2022/9/8 0:38
 */
    public static <T> List<T> parseResultSet(ResultSet rs, Class<T> clazz) throws SQLException, IllegalAccessException, InstantiationException {
        List<T>  list = new ArrayList<>();
        if(rs == null){
            return null;
        }
        while(rs.next()){
            //反射
            T t = clazz.newInstance();
            //获取属性
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                //约定: 属性名与列名一样: 使用别名
                field.set(t,rs.getObject(field.getName()));
                //找set方法赋值
            }
            list.add(t);
        }
        return list;
    }
    /*
     * @description: 单个结果
     * @param rs
     * @param clazz
     * @return: java.lang.Object
     * @author: dodo
     * @time: 2022/9/8 9:14
     */
    public static <T> Object parseResultSetByOne(ResultSet rs, Class<T> clazz) throws SQLException, IllegalAccessException, InstantiationException {

        if(rs == null){
            return null;
        }
        T t = null;
        while(rs.next()){
            //反射
             t = clazz.newInstance();
            //获取属性
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                //约定: 属性名与列名一样: 使用别名
                field.set(t,rs.getObject(field.getName()));
                //找set方法赋值
            }

        }
        JDBCUtil.close(rs,null,null);
        return t;
    }
/*
 * @description: 查询
 * @param sql
 * @param params
 * @return: java.sql.ResultSet
 * @author: dodo
 * @time: 2022/9/8 9:14
 */
    public static ResultSet executeQuery(String sql,Object... params) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            preparedStatement = conn.prepareStatement(sql);
            if (params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    preparedStatement.setObject(i + 1, params[i]);
                }
            }
            rs = preparedStatement.executeQuery();
            return rs;

        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
           // JDBCUtil.close(null,preparedStatement,conn);
        }
            return null;


    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值