private void removeGroupIdFromTablePaths ( int groupIdToRemove ) { PreparedStatement queryStatement = null ; PreparedStatement statement = null ; ResultSet results = null ; try ( Connection sqlConnection = sqlService . getConnection ( ) ) { queryStatement = sqlConnection . prepareStatement ( "SELECT * FROM " + Constants . DB_TABLE_PATH ) ; results = queryStatement . executeQuery ( ) ; // this is a hashamp from a pathId to the string of groups HashMap < Integer , String > idToGroups = new HashMap < Integer , String > ( ) ; while ( results . next ( ) ) { int pathId = results . getInt ( Constants . GENERIC_ID ) ; String stringGroupIds = results . getString ( Constants . PATH_PROFILE_GROUP_IDS ) ; int [ ] groupIds = Utils . arrayFromStringOfIntegers ( stringGroupIds ) ; String newGroupIds = "" ; for ( int i = 0 ; i < groupIds . length ; i ++ ) { if ( groupIds [ i ] != groupIdToRemove ) { newGroupIds += ( groupIds [ i ] + "," ) ; } } idToGroups . put ( pathId , newGroupIds ) ; } // now i want to go though the hashmap and for each pathId, add // update the newGroupIds for ( Map . Entry < Integer , String > entry : idToGroups . entrySet ( ) ) { Integer pathId = entry . getKey ( ) ; String newGroupIds = entry . getValue ( ) ; statement = sqlConnection . prepareStatement ( "UPDATE " + Constants . DB_TABLE_PATH + " SET " + Constants . PATH_PROFILE_GROUP_IDS + " = ? " + " WHERE " + Constants . GENERIC_ID + " = ?" ) ; statement . setString ( 1 , newGroupIds ) ; statement . setInt ( 2 , pathId ) ; statement . executeUpdate ( ) ; } } catch ( SQLException e ) { e . printStackTrace ( ) ; } finally { try { if ( results != null ) { results . close ( ) ; } } catch ( Exception e ) { } try { if ( queryStatement != null ) { queryStatement . close ( ) ; } } catch ( Exception e ) { } try { if ( statement != null ) { statement . close ( ) ; } } catch ( Exception e ) { } } }
Remove all references to a groupId