0

How to properly get reference to the Statment object inside the NamedParameterJdbcTemplate query/exeucte/update.

I need the reference because I have a button on the GUI to stop the running query (connection.close() & statement.close() & release connection etc.)

I do not want to use plain jdbc because I need the extras that namedjdbctemplate provide. I also don't want to use httprpc with plain jdbc because httprpc named paramters setter bugged when the sql statement contains :. (in regex, column names, or any char inside in an inner function)

So I want to use namedjdbctemplate for my queries and I also need the Connection and Statement object reference.

At nJdbcTemplate.query I could override the extract data and from the ResultSet I got it:

    var wrapper = new Object() {
        Statement value = null;
    };

    var columnWrapper = new Object() {
        List<Object> columnNames = null;
    };

    Statement statement = wrapper.value;
    task.setPreparedStatement(statement);

    String sql = reportTab.getSqlStatement();

    try {
        List<List<Object>> query = nJdbcTemplate.query(sql, paramMap, new ResultSetExtractor<List<List<Object>>>() {

            @Override
            public List<List<Object>> extractData(ResultSet rs) throws SQLException, DataAccessException {

                wrapper.value = rs.getStatement();

                List<Object> columnNames = getColumnNames(rs);
                columnWrapper.columnNames = columnNames;

My main problem is that I cannot get it when I have to use execute/update

I tried with this, but when I use DLL the resultset is null.

        Object result = nJdbcTemplate.execute(sql, paramMap, (PreparedStatementCallback<Object>) ps -> {
            ResultSet resultSet = ps.getResultSet();
            Statement statement2 = resultSet.getStatement();
            wrapper.value = statement2;
            ps.executeUpdate();
            return "result";
        });
SüniÚr
  • 826
  • 1
  • 16
  • 33

0 Answers0