I have an admin page for one of my apps where i can type in a sql statement and have it executed
this page supports select, update, delete, insert, store procs etc
If the sql statement returns a ResultSet then I display the results in the browser to the user.
Is there any way using Spring's jdbc classes to determine if a sql statement I have executed has any results?
Right now I have a poor implementation as follows in my DAO impl class.
It would be nice if there was a way for me to execute a statement and somehow interrogate the result to see if the statement returned a resultset or how many updated/deleted/inserted rows and process accordingly.Code:public SqlResult executeSql(String sql) { GenericRowMapper mapper = new GenericRowMapper(); SqlResult result = new SqlResult(); // i wish i knew a 'spring' way to work out whether a sql statement produced some results. // for now we just check for the insert, update and delete keywords if (sql.toLowerCase().startsWith("insert") || sql.toLowerCase().startsWith("update") || sql.toLowerCase().startsWith("delete")) { getJdbcTemplate().update(sql); } else { List data = getJdbcTemplate().query(sql, new RowMapperResultReader(mapper)); result.setData(data); result.setColumnNames(mapper.getColumnNames()); } return result; }
In plain jdbc code I would do something like
But as I have found out - directly calling jdbc is evilCode:String sql = "a sql statement"; CallableStatement clstmt = null; ResultSet rset = null; CallableStatement cs = con.prepareCall(sql); cs.execute(); result = cs.getResultSet(); if (result != null) { // generate a html representation of the results html = getHtml(); } else { html = "<html>SQL executed successfully</html>"; }
Is there a Spring way to do this?



Reply With Quote