closeStatement(statement); } if (queryIdSql != null) { Statement queryStmt       = null; ResultSet lastInsertedIds = null;  try {    connection.commit();  queryStmt       = connection.createStatement(); lastInsertedIds = queryStmt.executeQuery(queryIdSql);  lastInsertedIds.next();  Object value = lastInsertedIds.getObject(autoIdColumn.getName());  PropertyUtils.setProperty(dynaBean, autoIdColumn.getName(), value); } catch (NoSuchMethodException ex) {  } catch (IllegalAccessException ex) {  } catch (InvocationTargetException ex) {  } finally { if (lastInsertedIds != null) { lastInsertedIds.close(); } closeStatement(queryStmt); }
returnConnection(connection);
    public void insert(DynaBean dynaBean, Connection connection) throws SQLException    {        SqlDynaClass      dynaClass  = getSqlDynaClass(dynaBean);        SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();        if (properties.length == 0)        {            _log.info("Cannot insert type " + dynaClass + " as there are no properties");            return;        }        Column autoIdColumn = _database.findTable(dynaClass.getTableName()).getAutoIncrementColumn();        if (autoIdColumn != null)        {            // We have to remove the auto-increment column as some databases won't like            // it being present in the insert command            SqlDynaProperty[] newProperties = new SqlDynaProperty[properties.length - 1];            int               newIdx        = 0;            for (int idx = 0; idx < properties.length; idx++)            {                if (properties[idx].getColumn() != autoIdColumn)                {                    newProperties[newIdx++] = properties[idx];                }            }            properties = newProperties;        }                String            insertSql    = createInsertSql(dynaClass, properties, null);        String            queryIdSql   = autoIdColumn != null ? createSelectLastInsertIdSql(dynaClass) : null;        PreparedStatement statement    = null;        if (_log.isDebugEnabled())        {            _log.debug("About to execute SQL: " + insertSql);        }        if ((autoIdColumn != null) && (queryIdSql == null))        {            _log.warn("The database does not support querying for auto-generated pk values");        }        try        {            statement = connection.prepareStatement(insertSql);            for (int idx = 0; idx < properties.length; idx++ )            {                setObject(statement, idx + 1, dynaBean, properties[idx]);            }            int count = statement.executeUpdate();            if (count != 1)            {                _log.warn("Attempted to insert a single row " + dynaBean +                         " in table " + dynaClass.getTableName() +                         " but changed " + count + " row(s)");            }        }        finally        {            closeStatement(statement);        }        if (queryIdSql != null)        {            Statement queryStmt       = null;            ResultSet lastInsertedIds = null;            try            {                // we'll have to commit the statement(s) because otherwise most likely                // the auto increment hasn't happened yet (the db didn't actually                // perform the insert yet so no triggering of sequences did occur)                connection.commit();                queryStmt       = connection.createStatement();                lastInsertedIds = queryStmt.executeQuery(queryIdSql);                lastInsertedIds.next();                Object value = lastInsertedIds.getObject(autoIdColumn.getName());                PropertyUtils.setProperty(dynaBean, autoIdColumn.getName(), value);            }            catch (NoSuchMethodException ex)            {                // Can't happen because we're using dyna beans            }            catch (IllegalAccessException ex)            {                // Can't happen because we're using dyna beans            }            catch (InvocationTargetException ex)            {                // Can't happen because we're using dyna beans            }            finally            {                if (lastInsertedIds != null)                {                    lastInsertedIds.close();                }                closeStatement(queryStmt);            }        }    }