public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } List<String> columns = getColumns(); Map<String, Object> = getMap(); KeyHolder createdKey = new GeneratedKeyHolder(); int result = getJdbcTemplate().update((PreparedStatementCreator) con -> { PreparedStatement ps = con.prepareStatement(sql); for(int i=0;i<columns.size();i++){ Object value = columnNameValueMap.get(columns.get(i)); if(value instanceof String || value instanceof Date){ ps.setString(i+1, String.valueOf(value)); }else if(value instanceof Boolean){ if(value.equals(true)) ps.setInt(i+1, 1); else ps.setInt(i+1, 0); }else if(value instanceof Integer){ ps.setInt(i+1, (Integer) value); }else if(value instanceof BigDecimal){ ps.setBigDecimal(i+1, (BigDecimal) value); } } return ps; },createdKey);
I am getting above error when trying to perform insert operation from java application using JDBCTemplate.
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; The statement must be executed before any results can be obtained.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained
Advertisement
Answer
Change prepare call to con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);