JBDC工具类的设计

本文介绍了如何在Java项目中创建自定义工具类,用于配置和管理Druid数据库连接池。包括加载配置文件、初始化连接池对象、获取连接、执行SQL操作(如更新、查询)以及资源关闭的方法。此外,还展示了查询方法如何利用反射处理结果集。
摘要由CSDN通过智能技术生成

一、自定义工具类

1.项目配置

  • 建模块、

  • 建资源目录  

  • 添加依赖

  • 项目结构  

2.配置德鲁伊(Druid)连接池

2.1配置连接池对象

 private static DataSource dataSource;

static {
    //加载配置文件资源
    Properties properties = new Properties();
    InputStream in = 	 Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
    try {
        properties.load(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        //程序启动的时候加载配置资源获取连接池对象
        dataSource = DruidDataSourceFactory.createDataSource(properties);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.2打印连接池对象

{
	CreateTime:"2022-07-15 10:15:40",
	ActiveCount:0,
	PoolingCount:0,
	CreateCount:0,
	DestroyCount:0,
	CloseCount:0,
	ConnectCount:0,
	Connections:[
	]
}
  • 注意,德鲁伊连接池对象重写了toString 方法

3.获取连接对象

public static Connection getConn(){    
Connection connection = null;    
try {        
connection = dataSource.getConnection();    
} catch (SQLException throwables) {        
throwables.printStackTrace();    
}    
return connection;
}

4.新增、删除、更新方法

public static int update(Connection conn,String sql,Object...args){
        //受影响行数
    int count = 0;
    PreparedStatement statement = null;
    try {
        //获取预编译对象
        statement = conn.prepareStatement(sql);
        //参数填充
        for (int i = 0; i < args.length; i++) {
            statement.setObject(i+1,args[i]);
        }
        //执行
        count = statement.executeUpdate();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally {
        close(conn,statement,null);
    }
    return count;
}
 public static void close(Connection conn, Statement statement, ResultSet resultSet){
        if(conn != null){
            try {
                //对于使用连接池对象来说,并不是真正的关闭,是放回连接池
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

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

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

5.查询方法

  • 注意,要通过反射来灵活处理工具类

public static <T> List<T>  query(Connection conn,String sql,Class<T> clz,Object...args){
        PreparedStatement statement = null;
        List<T> list= new ArrayList<>();
        ResultSet resultSet = null;
        try {
            statement = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i+1,args[0]);
            }
            //查询操作
            resultSet = statement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            //处理结果集对象
            while (resultSet.next()){
                T t = clz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取列的值
                    Object object = resultSet.getObject(i + 1);
                    //列的名称
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //获取字段名称
                    Field field = clz.getDeclaredField(changeColumn(columnLabel));
                    field.setAccessible(true);
                    field.set(t,object);
                }
                list.add(t);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } finally {
            close(conn,statement,resultSet);
        }
        return list;
    }
 private static String changeColumn(String column){
    String name = column;
    int index = name.indexOf("_");
    if(index > 0 && name.length() != index + 1){
        name = name.replaceFirst("_","");
        String ret = name.substring(index, index + 1);
        ret = ret.toUpperCase();
        column = name.substring(0,index) + ret + name.substring(index+1);
    }else{
        return column;
    }
    //递归调自己不停去寻找
    return changeColumn(column);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值