JAVA中直接执行sql语句示例

代码:

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    //执行select
    private List<Map<String, Object>> executeGet(String schema, String sql) throws SQLException {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection connection = getConnection();
            connection.setCatalog(schema);
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            List<Map<String, Object>> resultList = new ArrayList<>();
            while (resultSet.next()) {
                Map<String, Object> dataMap = new HashMap<>();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    dataMap.put(metaData.getColumnLabel(i), resultSet.getObject(i));
                }                resultList.add(dataMap);
            }
            return resultList;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {

            }
        }
    }

    //执行update,insert,delete
    private int executeUpdate(String schema, String sql) throws SQLException {
        Statement statement = null;
        try {
            Connection connection = getConnection();
            connection.setCatalog(schema);
            statement = connection.createStatement();
            return statement.executeUpdate(sql);
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {

            }
        }
    }

	//将结果集转换为对象
    public static <T> List<T> parserDbo(List<Map<String, Object>> dboMapList, Class<T> dboClass, boolean canEmpty) throws Exception {
        if (CollectionUtil.isEmpty(dboMapList)) {
            if (!canEmpty) {
                throw new Exception("");
            }
            dboMapList = new ArrayList<>();
        }
        List<T> resultList = new ArrayList<>();
        for (Map<String, Object> dboMap : dboMapList) {
            resultList.add(BeanUtil.mapToBean(dboMap, dboClass, false));
        }
        return resultList;
    }


}

注意:BeanUtil.mapToBean这个方法,需要导入以下依赖

    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.7.4</version>
    </dependency>
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值