如何使用dataSource操作数据库进行CRUD操作

@Resource
private DataSource dataSource;

try {
        dbConnection = dataSource.getConnection();
        PreparedStatement ps = dbConnection.prepareStatement(entity.getSql());
        String trimmedSql = entity.getSql().trim().toLowerCase();
        if(!(trimmedSql.startsWith("insert") || trimmedSql.startsWith("update"))) {
            return "fail";
        }
        ps.execute();
    }catch (Exception e) {
        return "fail";
    }finally {
        try{
            if(Objects.nonNull(dbConnection)) {
                dbConnection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
posted @ 2024-11-14 17:35  风沙、星辰  阅读(5)  评论(0)    收藏  举报