• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

correct my code..

HI EXPERTS,

Somewhere going wrong on my approach:

I have a JSP form:
<input type="checkbox" name="MenuGroup1-main" value"">
   <input  type="checkbox" name="MenuGroup1" value="...">
   <input  type="checkbox" name="MenuGroup2" value="...">
<input type="checkbox" name="MenuGroup2-main" value"">
   <input  type="checkbox" name="MenuGroupA" value="...">
   <input  type="checkbox" name="MenuGroupB" value="...">

---------output----------
[]MenuGroup1
   []MenuName1
   []MenuName2
[]MenuGroup2
   []MenuNameA
   []MenuNameB
..
..


Now what I'm trying is in my action JSP checking which chechbox are checked and add to some arraylist and after few other line I need to do update query and utilize this arralylist... But I'm unable to check correctly.. Here is my code.

String menu = "SELECT MENU_GROUP FROM MENUS-TABLE";
                    ResultSet rs = stmt.executeQuery(menu);
                   
                    while (rs.next())
                    {
                        String value = rs.getString("MENU_GROUP");
                         String[] selected1 = request.getParameterValues(value);  
                              if (selected1 != null && selected1.length != 0)
                          {
                            for (int i = 0; i < selected1.length; i++)
                               {
                            out.println("<br>Lock: " + selected1[i]);      // add to array list.          
                               }          
                          }                
                    }
...
..
Finaly Update Query using arraylist (menu names) and other parameters....


thx,
PH    
0
princehyderabad
Asked:
princehyderabad
  • 10
  • 9
  • 4
2 Solutions
 
fargoCommented:
first check

String value = rs.getString("MENU_GROUP");

is value available?

fargo
0
 
princehyderabadAuthor Commented:
while (rs.next())
                  {
                    String value = rs.getString("MENU_GROUP");
if (value != null) // << First check
{
                      String[] selected1 = request.getParameterValues(value);  
                         if (selected1 != null && selected1.length != 0)
                       {
                          for (int i = 0; i < selected1.length; i++)
                            {
                          out.println("<br>Lock: " + selected1[i]);      // add to array list.          
                            }          
                       }              
}
                   }

Like this... didnt worked
0
 
fargoCommented:
Hi,

1) i mean first check whether the "value" from resultset is null or not

System.out.println("value"+value);

like the above.

2) Be sure that the parameters you are getting from request has the same name as coming from the resultset.

fargo
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mujtaba_Alam_KhanCommented:
hmm That doesnt make sense to me..
ill show you why..
-----------------------------
while (rs.next())
{
             String value = rs.getString("MENU_GROUP");
             if(value != null) { }                                                 //? This doesnt make sense.. UNLESS you value in the Db is "" or null
}        

---------------------------

 String value = rs.getString("MENU_GROUP");     vs  String[] selected1 = request.getParameterValues(value);  

------------------------------------------------------

String[] selected1 = request.getParameterValues(value);  
                                                                          ^

request.getParameterValues() // this is getting an ArrayList from the Other JSP!!            
---------------------------------------------------------

<input type="checkbox" name="MenuGroup1" value"X">
<input  type="checkbox" name="MenuGroup1" value="Y">
<input  type="checkbox" name="MenuGroup1" value="Z">

request.getParameterValues("MenuGroup1");
-----------------------------------------------------------

Best idea is to print out the Selected1 first..
then compare in to the Database...

<input type="checkbox" name="MenuGroup1-main" value"">
   <input  type="checkbox" name="MenuGroup1" value="...">
   <input  type="checkbox" name="MenuGroup2" value="...">
<input type="checkbox" name="MenuGroup2-main" value"">
   <input  type="checkbox" name="MenuGroupA" value="...">
   <input  type="checkbox" name="MenuGroupB" value="...">

^ Creating complexity there..

if it only one value your are checking use:      String Select = request.getParameter(value) insisted of request.getParameterValues()..

-Muj ;-)

                                                                 
0
 
princehyderabadAuthor Commented:
>> 1) i mean first check whether the "value" from resultset is null or not

It is never null. I can assure that.

>>2) Be sure that the parameters you are getting from request has the same name as coming from the resultset.

yes it will same, becuase the name displayed on form is from DB. So same table
0
 
princehyderabadAuthor Commented:
Muj bhai, here is the seen:

Table:
MenuGroup     MenuName
Asia                  india
Asia                  china
Asia                  pak
Europe            Germany
Europe            France
...
...


I read the table and displayed like this:
[]Asia
   []India
   []china
   [] pak
[]Europe
  []Germany
  []France


===Action======
Anyone check []Asia, my JS checks all 3 countries.Same with Europe.
When user submit I wanna stored values assigned to that user. Eg: If only China is checked then I need to get under which MenuGroup is China and then sort. Thats why i was using my logic which is not giving me right result.

So in my case how can I handle JSP response and store in DB assigning menuname to that user.
0
 
fargoCommented:
this is for Muj,

  if(value != null) { }                                                 //? This doesnt make sense.. UNLESS you value in the Db is "" or null

it does makes sense in situation where db data is inconsistent or badly managed or value somehow returns null according to the complexity of the retrieval query.

>> UNLESS you value in the Db is "" or null
This explains why to use the check.

fargo
0
 
fargoCommented:
Hi,

1) String[] selected1 = request.getParameterValues(value);  
can you just see if the returned value gives any result. I mean, the length of the string array?

2) As Muj suggested, print the selected values first just for debug
request.getParameterValues("MenuGroup1");

may be u can use the hardcoded values to check like above.

regards,
fargo



0
 
princehyderabadAuthor Commented:
yes I'm getting the display properly when I hardcoded as you guys mentioned.

Let me share my code and let you know that my field names are not hardcoded, anywaz have a look:

String hoh = "DB Query";
                  ResultSet rs= stmt4.executeQuery(hoh);    
                  String tempMenuGroup="";
                      while (rs.next())                
                  {
                           String mg = rs.getString("MENU_GROUP");
                           if (mg!=null && !mg.equals(tempMenuGroup))
                           {%>                          
                           <input type=checkbox  name="<%=mg%>" onclick="javascript:chk('<%=mg%>')" value="<%=mg%>"><b><%=mg%></b><br>
                           <%    
                           }                                                      
                           String mn = rs.getString("MENU_NAME");                                                                                  
                           String checked="";
                           String assigned=rs6.getString("assigned");
                           if(assigned!=null && assigned.equals("1")){
                                    checked="checked='checked'";
                            }
                           if(mn!=null){
                          %>
         <input type=checkbox name="<%=mg%>" <%=checked%> value="<%=mn%>">&nbsp;&nbsp;&nbsp;&nbsp;<%=mn%><br>                      
                           <%
                           }  
                           tempMenuGroup = mg;
                    }%>

FYI: mg stands for menu_group, mn=menu_name
eg:
[]Asia
   []India
   []china
   [] pak
Then <input> name for all above checkbox is "Asia"
0
 
Mujtaba_Alam_KhanCommented:
see to me like you have created alot of complexity..

as fargo say: what do you get when you print out "value"..

while(rs.next)
{
       String value = rs.getString("MENU_GROUP");
       out.println("Value: " + value);
}

I think insisted of using request.getParameterValues()..

Use request.getParameter() because I think it each parameter has a different name..

try this and see if it makes a difference..:

while(rs.next)
{
       String value = rs.getString("MENU_GROUP");
       out.println("Value: " + value);
       String selected1 = request.getParameter(value);  
       if (selected1 != null && selected1.length != 0)
       {
                          out.println("<br>Lock: " + selected1);    
        }              
}

-Muj ;-)


0
 
princehyderabadAuthor Commented:
This is getting printed
out.println("Value: " + value);

But its printing
out.println("<br>Lock: " + selected1);    
Even though I'm checking few checkbox on form and submiting....

I hv to go will check 2morror fresh mind.
0
 
Mujtaba_Alam_KhanCommented:
ok cya..

so ignore one of the line printed.. possible this one .. out.println("Value: " + value);

0
 
princehyderabadAuthor Commented:
let me give  you example, hope then U'll get my idea:

I think I know the problem but not the solution: Here it is:

while(rs.next())
{
        String a = rs1.getString("MENU_NAME");

        String b = request.getParameter(a); // Problem is here........??????

            if(b.equals("checked"))
                 {
                out.print("<br>Done--:"+b); //DB Insert Query
                 }
       }
                 
}

Let me give U Example DB Table look like:

menu_group       menu_name    MenuID
Activity Track      sell                    001
Activity Track      buy                    002
Activity Block       stop                  003
Activity Block       cancel               004
...so on ....

my JSP FORM:
<input type=checkbox name="Activity Track"  value="Activity Track">
<input type=checkboxname="Activity Track1"  value ="sell">
<input type=checkboxname="Activity Track2"  value='buy">
<input type=checkboxname="Activity Block"  value="Activity Block">
<input type=checkboxname="Activity Block1"  value="stop">
<input type=checkboxname="Activity Block2"   value="cancel">
.. so on ...

0
 
Mujtaba_Alam_KhanCommented:
try and print out a

String a = rs1.getString("MENU_NAME");

out.println("a: " + a);

in the loop of course..

-Muj ;-)
0
 
princehyderabadAuthor Commented:
I'm gettting correct out for 'a". But if U see name of in input and getParameter its different. I dont know hwo to scope that..
0
 
Mujtaba_Alam_KhanCommented:
>> rs1.getString("MENU_NAME");

Shouldnt that be MENU_GROUP

<input type=checkbox name="Activity Track"  value="Activity Track">

                                                     ^ this is what the getParameter looks for.. and then get the Value for it..

-Muj ;-)
0
 
princehyderabadAuthor Commented:
Understand my explanation:

JSP Form:

[]Activity Track
  []sell
  []buy
[]Activity Block
  []stop
  []start
...so on...


Someone click on [] Activity Track, all its child is checked. That is only the purpose of having [] place with menu_gropu 'Activity Track'
soely for check all and uncheck all its child. Because of this javascript logic my code for above is like this:
<input type=checkbox name="Activity Track"  value="Activity Track">
<input type=checkboxname="Activity Track1"  value ="sell">
<input type=checkboxname="Activity Track2"  value='buy">

name is GroupName+number (and number gets incremented for each input) thats why U see when 'Activity Track has 2 child. they child input names are 'Activity Track1, Track2
and Activity Block has 2 childs and they child input names are 'Activity Block3', 'Activity4'... so on it .

Now u tell me what sh be done

0
 
Mujtaba_Alam_KhanCommented:
It doesnt make sense to me why you would have activity 1 .. 2 etc..

it would have been easier if they had the same name..

<input type=checkbox name="Activity Track"  value="Activity Track">                
<input type=checkboxname="Activity Track"  value ="sell">
<input type=checkboxname="Activity Track"  value='buy">                                

while(rs.next())
{
        String a = rs1.getString("MENU_GROUP");

        String b = request.getParameter(a);                        

            if(b.equals("checked"))
                 {
                out.print("<br>Done--:"+b); //DB Insert Query
                 }
       }
                 
}
--------------------------------------------------------------------------------
but here is my suggestion..
dont know if it will works..
--------------------------------------------------------------------------------

<input type=checkbox name="Activity Track"  value="Activity Track">                
<input type=checkbox name="Activity Track1"  value ="sell">
<input type=checkbox name="Activity Track2"  value='buy">                                

int i = 1;

while(rs.next())
{
        String a = rs1.getString("MENU_GROUP");

        String b = request.getParameter(a);                        
        b = b + i;                                                    // Activity Track1
            if(b.equals("checked"))
                 {
                out.print("<br>Done--:"+b); //DB Insert Query
                 }
       }        
}

---------------------------------------------

try this and see if it get the first Value at least..
ill see if there is a better solution..

-Muj ;-|

0
 
princehyderabadAuthor Commented:
>>It doesnt make sense to me why you would have activity 1 .. 2 etc..

I told it was done to make my javascript

Your 2nd option worked, but only if parent is checked. If I select just a single child its showing null.
0
 
Mujtaba_Alam_KhanCommented:
try this:

int i = 1;

while(rs.next())
{
        String a = rs1.getString("MENU_GROUP");

        String b = request.getParameter(a);                        
        b = b + i;                                                    // Activity Track1 .. Activity Track2
            if(b.equals("checked"))                                
                 {
                out.print("<br>Done--:"+b); //DB Insert Query
                 }
       
         b = "";
         i++;
     
}
0
 
princehyderabadAuthor Commented:
Just a quick question: Instead of calling DB Insert query in a while so many times can I call it once and all. I hope I can, but thing is how should I get all those not null values gather and pass it DB query at once

while ..{
if (.) {
 // how to gather or build all query values here ??????
}
}
INSERT INTO tACCESS (USERID,MENU_ID) VALUES ('user',?????);
0
 
Mujtaba_Alam_KhanCommented:
try this for DB update.. (insert)..

http://java.sun.com/docs/books/tutorial/jdbc/jdbc2dot0/batchupdates.html
but do the update after the loop..
------------------------------------------------------------------------------------------

while(rs.next())
{
        String a = rs1.getString("MENU_GROUP");

        String b = request.getParameter(a);    
       if(b != "" || b != null)
      {
             b = b + i;                                                    // Activity Track1 .. Activity Track2
            if(b.equals("checked"))                                
                 {
                out.print("<br>Done--:"+b); //DB Insert Query
                 }
       }
         b = "";
         i++;
     
}


-Muj ;-)
0
 
Mujtaba_Alam_KhanCommented:
Thanks ;-D
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now