Link to home
Start Free TrialLog in
Avatar of aman0711
aman0711

asked on

How to delete particular values from a HashMap

Hi Experts,

               I have the following method, which runs an update query on the DB. the method has two parameters: HashMap hm, String Section.

               both of these parameters are fetched from a jsp page using:
         
Now the problem I am facing is, the is a save button at the bottom of this page(Snapshot attached), On submitting the save button, the update query in the code shown runs and it updates the Backend table and refreshs the page with new data.
             It works fine for the first time(When i open the page for the first time), but during its second iteration, it fails and SQL error i get is :
error occurred during batching: ORA-00904: "SECTIONS": invalid identifier

           because somehow, the parameters in URL gets passed to this query(String) and it fails. How do I restrict my hashmap to contain only integers or how do i pass only the integer values of the hashmap o the method that runs the SQL.


           
      @RequestMapping(method=RequestMethod.POST)
       public String saveAdminDetails(HttpServletRequest request ,ModelMap model, @ModelAttribute("editAdminForm") AdminChart AdminChart) {
            
            String sect = request.getParameter("section");
                        
//            log.info("EditControl>>POST>>>selected_sect>>>=" + sect);
            
            HashMap<String, String> map = new HashMap<String, String>();
            Enumeration e = request.getParameterNames();
            while (e.hasMoreElements()) {
                  String name = (String) e.nextElement();
                  String value = request.getParameter(name);
//                  log.info(">>>>editChart>>>>>" + name + "==>" + value);
                  map.put(name, value);
            }
            
            AdminChartSummaryData acsd = new AdminChartSummaryData();
            acsd.updateChart(map, sect);
            
            String chartUpdates = AdminChart.getNotes();
//            log.info(">>>>>edit_notes>>>>>>" + chartUpdates);
//            log.info(">>>>>edit_notes_chartId=>>>>>>" + AdminChart.getChartId());
            //adminChartSummaryData.updateChartNotes(AdminChart, chartUpdates);
            
            model.put("currentpage", "admin");
      
            return "redirect:/admin.html";
      }
      


public void updateArrayOfChartsPosition(HashMap hm ,String section) {
//		System.out.println("chartPostion mtd called>>>>>>>section=" + section);
 
		String updateQry = null;
		String table = " MYWORLD_CHART_DETAILS ";
 
		try {
			dbc = new DbConnection();
			con = dbc.getConnection(url, user, password);
 
			stmt = dbc.getStatement(con);
			
			if (section.equals("Summary")) {
				table = " MYWORLD_CHART_DETAILS ";
			} else if (section.equals("Transaction")) {
				table = " MYWORLD_CHART_DETAILS_TRX ";
			}
			
			Set myKeys = hm.keySet();
			Iterator itr = myKeys.iterator();
 
			 
			
			while (itr.hasNext()) {
				String ky = (String) (itr.next());
				 System.out.println(">>>>>>>" + ky + ">>>" + hm.get(ky));
					
				 if (!ky.equalsIgnoreCase("section")) {
					 										 
					 updateQry = "update " + table + " set CHART_POSITION='"
						+ hm.get(ky) + "' where ID=" + ky;				 
					 stmt.addBatch(updateQry);
				 }
			}
			int[] res = stmt.executeBatch();
			
 
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbc.closeResources(stmt, con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

Open in new window

AdminConsole.PNG
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I'm not sure what you're doing with the Map, but the parameters are already in one:

http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap()
Avatar of aman0711
aman0711

ASKER

Hi Charles,

                     I am fetchign everything from the page, and storing it in a Map... because the query that runs on the Table contains an id filed (id is in the backend table)
SOLUTION
Avatar of SordSord
SordSord
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi SordSord,

                   Going to try your solution
>>because the query that runs on the Table contains an id filed (id is in the backend table)

Why don't you just do req.getParameter("id") ?
Hi sordsord,
 
                   I actually cant see any error on the console maybe coz too much of stuff prints there.

But here is what I got. the first time when I hit save, This is what my HashMap looks like:
I have also posted the HashMap values, when i hit save for the second time:
FIRST TIME:
>>>>>>>19>>>2
>>>>>>>17>>>3
>>>>>>>18>>>89
>>>>>>>36>>>4
>>>>>>>15>>>92
>>>>>>>33>>>91
>>>>>>>16>>>1
>>>>>>>34>>>43
>>>>>>>13>>>93
>>>>>>>14>>>10
>>>>>>>118>>>115
>>>>>>>119>>>117
>>>>>>>12>>>94
>>>>>>>20>>>7
>>>>>>>103>>>102
>>>>>>>23>>>6
>>>>>>>24>>>9
>>>>>>>28>>>11
>>>>>>>29>>>38
>>>>>>>2>>>88
>>>>>>>1>>>551
>>>>>>>10>>>87
>>>>>>>7>>>991
>>>>>>>6>>>5
>>>>>>>32>>>41
>>>>>>>5>>>77
>>>>>>>31>>>40
>>>>>>>4>>>99
>>>>>>>9>>>8
>>>>>>>8>>>550

SECOND TIME:
>>>>>>>19>>>2
>>>>>>>17>>>3
>>>>>>>18>>>89
>>>>>>>36>>>4
>>>>>>>15>>>92
>>>>>>>33>>>91
>>>>>>>16>>>1
>>>>>>>34>>>43
>>>>>>>13>>>93
>>>>>>>14>>>10
>>>>>>>118>>>115
>>>>>>>119>>>117
>>>>>>>12>>>94
>>>>>>>20>>>7
>>>>>>>sections>>>Summary
>>>>>>>103>>>102
>>>>>>>23>>>6
>>>>>>>24>>>9
>>>>>>>currentpage>>>admin
>>>>>>>section>>>Summary
>>>>>>>28>>>11
>>>>>>>29>>>12
>>>>>>>2>>>88
>>>>>>>1>>>551
>>>>>>>10>>>87
>>>>>>>7>>>991
>>>>>>>6>>>5
>>>>>>>5>>>77
>>>>>>>32>>>41
>>>>>>>4>>>99
>>>>>>>31>>>40
>>>>>>>9>>>8
>>>>>>>8>>>550
java.sql.BatchUpdateException: error occurred during batching: ORA-00904: "SECTIONS": invalid identifier
@ Charles:

                  This is the table you are seeing in the snap shot.. This contains everything.

<form:form commandName="adminForm" onsubmit="return validateTable();">
<input type="hidden" value="${adminLeftForm.section}"/>
 
<display:table name="adminDetails" id="row" class="table-style">
    <display:column property="chartGroup" title="Report Group"/>
    <display:column property="chartSubgroup" title="Report Subgroup"/>
    <display:column property="chartName" title="Chart Name"/>
    <display:column property="chartType" title="Chart Type"/>
    <display:column property="liabilityCode" title="Customer Type"/>
    <display:column property="timeline" title="Timeline"/>    
    <display:column><a href="adminEdit.html?id=${row.chartId}&section=${adminLeftForm.section}">Edit</a></display:column>
    <display:column><a href="adminDelete.html?id=${row.chartId}&section=${adminLeftForm.section}" onclick="javascript:return confirm('Are you sure you want to delete the Chart? Please click Yes to continue or else click Cancel')">Delete</a></display:column>
    <display:column title="Chart Position"><input name="${row.chartId}" type="text" size="2" value="${row.chartPosition}""></display:column>
 </display:table>
 
 <table>
 <tr>
 			<td>  <input type="submit" value="Cancel" onclick="window.close()" align="middle" /></td>
              <td> <input type="reset" value="Refresh to Original" align="middle" /> </td> 
             <td><input type="submit" value="Save"  onsubmit="return validateTable();"></td>
</tr>  
 </table>
 
   </form:form>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry

if (val.matches("\\d+") && key.matches("\\d+")) {
hi Charles.. these two things are not in one Java class.. the request.getParameter is in the Controller and the update method is in the backend class
OK, then just copy the parameter map:

Map m = new HashMap(request.getParameterMap());

>> Map m = new HashMap(request.getParameterMap());

Sorry didnt get you :( i controller or backend class :(

Below is the POST method of controller

@RequestMapping(method=RequestMethod.POST)
	 public String saveAdminDetails(HttpServletRequest request ,ModelMap model, @ModelAttribute("editAdminForm") AdminChart AdminChart) {
		
		String sect = request.getParameter("section");
				
//		log.info("EditControl>>POST>>>selected_sect>>>=" + sect);
		
		HashMap<String, String> map = new HashMap<String, String>();
		Enumeration e = request.getParameterNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = request.getParameter(name);
//			log.info(">>>>editChart>>>>>" + name + "==>" + value);
			
			try {
				  Integer.parseInt(value); 
			map.put(name, value);
			} catch (NumberFormatException e1) {
				
			}
		}
		
		AdminChartSummaryData acsd = new AdminChartSummaryData();
		acsd.updateChart(map, sect);
		acsd.updateArrayOfChartsPosition(map, sect);
		
		String chartUpdates = AdminChart.getNotes();
//		log.info(">>>>>edit_notes>>>>>>" + chartUpdates);
//		log.info(">>>>>edit_notes_chartId=>>>>>>" + AdminChart.getChartId());
		//adminChartSummaryData.updateChartNotes(AdminChart, chartUpdates);
		
		model.put("currentpage", "admin");
	
		return "redirect:/admin.html";
	}

Open in new window

All this code:

>>

   Enumeration e = request.getParameterNames();
                while (e.hasMoreElements()) {
                        String name = (String) e.nextElement();
                        String value = request.getParameter(name);
//                      log.info(">>>>editChart>>>>>" + name + "==>" + value);
                       
                        try {
                                  Integer.parseInt(value);
                        map.put(name, value);
                        } catch (NumberFormatException e1) {
                               
                        }
                }
>>
can be replaced by the code i posted
Cool.. Gonna try right now :)
>>// Do the insert

(would mean insert into the Map)
Charles, getting an error on one of the line.


map.PNG
Yes I did the insert :(
Looks like you have

map.Entry

instead of

Map.Entry
No I had Map.Entry but that was throwing an error, so i moved it to map.Entry

map.PNG
>>map.put(name, value)

should be

map.put(key, val)
Still throwing an error cannot convert an object to map entry
Yes, sorry - you're going to have to use the Enumeration you had
U mean the whole code? :(
No - see below
        for(Enumeration e = req.getParameterNames();e.hasMoreElements();) {
            String key = e.nextElement().toString();
            String val = req.getParameter(key);
            if (val.matches("\\d+") && key.matches("\\d")) {
                // Do the insert
            }   
        }   

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Objects you are great. just 2 lines fixed everything :)