idea自动封装方法

例如

复制代码
package com.utils;

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

/**
 * @author hrui
 * @date 2023/10/13 13:49
 */
public class DBUtils {
    private static ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
    private static String driver=bundle.getString("jdbc.driver");
    private static String url=bundle.getString("jdbc.url");
    private static String username=bundle.getString("jdbc.username");
    private static String password=bundle.getString("jdbc.password");

    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //通用查询多个
    public static <T> List<T> selectList(Class<T> clazz, String sql, Object...args){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            conn=DBUtils.getConnection();
            ps=conn.prepareStatement(sql);

            for(int i=0;i<args.length;i++){
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            List<T> list=new ArrayList<>();
            while(rs.next()){
                T t = clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object object = rs.getObject(i + 1);
                    //String columnName = metaData.getColumnName(i + 1); 这个方法返回实际列名
                    String columnLabel = metaData.getColumnLabel(i + 1);//该方法返回别名,没有别名就返回列名
                    if(columnLabel.contains("_")){
                        int index = columnLabel.indexOf("_");
                        String replace = columnLabel.replace("_", "");
                        char c = Character.toUpperCase(replace.charAt(index));
                        columnLabel=replace.substring(0, index)+c+replace.substring(index+1);
                    }
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,object);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();

        }finally {
            DBUtils.closed(conn,ps,rs);
        }
        return null;
    }

    //通用查询单个
    public static <T> T selectOne(Class<T> clazz,String sql,Object...args){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            conn=DBUtils.getConnection();
            ps=conn.prepareStatement(sql);

            for(int i=0;i<args.length;i++){
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            if(rs.next()){
                T t = clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object object = rs.getObject(i + 1);
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    if(columnLabel.contains("_")){
                        int index = columnLabel.indexOf("_");
                        String replace = columnLabel.replace("_", "");
                        char c = Character.toUpperCase(replace.charAt(index));
                        columnLabel=replace.substring(0, index)+c+replace.substring(index+1);
                    }
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,object);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }finally {
            DBUtils.closed(conn,ps,rs);
        }
        return null;
    }




    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }
    //通用增删改方法
    public static int update(String sql,Object...args){
        Connection conn =null;
        PreparedStatement ps=null;
        int count=0;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1, args[i]);
            }
            count = ps.executeUpdate();
            //ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.closed(conn,ps,null);
        }

        return count;
    }


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

}

这段代码中

可以提取出来作为独立的一个方法

相关推荐
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 工厂能耗分析平台的设计与实现为例,包含答辩的问题和答案
java·vue.js
Spring AI学习5 小时前
Spring AI深度解析(9/50):可观测性与监控体系实战
java·人工智能·spring
java1234_小锋6 小时前
Spring IoC的实现机制是什么?
java·后端·spring
xqqxqxxq6 小时前
背单词软件技术笔记(V2.0扩展版)
java·笔记·python
三天不学习6 小时前
Cursor vs Trae vs VSCode:2025终极IDE横评,谁才是开发者的效率之选?
ide·vscode·编辑器
驴友花雕6 小时前
【花雕动手做】CanMV K230 AI视觉识别模块之使用CanMV IDE调试运行人脸代码
ide·人工智能·单片机·嵌入式硬件·canmv k230 ai视觉·canmv ide 人脸代码
猫头虎6 小时前
又又又双叒叕一款AI IDE发布,国内第五款国产AI IDE Qoder来了
ide·人工智能·langchain·prompt·aigc·intellij-idea·ai编程
消失的旧时光-19436 小时前
深入理解 Java 线程池(二):ThreadPoolExecutor 执行流程 + 运行状态 + ctl 原理全解析
java·开发语言
weixin_387545646 小时前
Antigravity 上手指南:打造 VS Code 风格的 AI IDE
ide·人工智能
程序届的伪精英6 小时前
IDE TRAE介绍与使用
ide·人工智能