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

}

这段代码中

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

相关推荐
C7211BA20 分钟前
Visual Studio Code (VS Code) 官方下载渠道
ide·vscode·编辑器
zhangfeng113324 分钟前
win7 VSCode 1.70设置R语言的版本,电脑上有两个版本
ide·vscode·r语言
学习和思考26 分钟前
为什么我的vscode有的时候可以跳转,有的时候不能跳转
arm开发·ide·驱动开发·vscode·学习·1024程序员节
新手村领路人26 分钟前
vscode ssh远程连接 ubuntu虚拟机
ide·vscode·ssh
lkbhua莱克瓦241 小时前
Java基础——常用API2
java·笔记·github·学习方法
繁依Fanyi1 小时前
【参赛心得】我的 HarmonyOS 开发入门与参赛之路
ide·人工智能·华为·word·harmonyos·aiide·codebuddyide
摇滚侠1 小时前
Spring Boot3零基础教程,Lambda 表达式与函数式接口,笔记95
java·spring boot·笔记
好学且牛逼的马1 小时前
【JavaWeb|day19 Web后端进阶 SpringAOP、SpringBoot原理、自定义Starter、Maven高级】
java·spring boot·rpc
码界奇点1 小时前
Java 开发日记MySQL 与 Redis 双写一致性策略挑战与实战解析
java·redis·sql·mysql·java-ee
GHZero1 小时前
Java 之解读String源码(九)
java·开发语言