[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need Logic to correct my code

Posted on 2006-05-09
21
Medium Priority
?
172 Views
Last Modified: 2010-03-31
hi experts,

Just need a simple logic here. Code is working but I added one more feature and its not working as it should be. Here is it:

JSP:
<input type="checkbox" name="lock" value="<%=rs.getString("UserID")%>"></td>


Java:
String[] Lock = request.getParameterValues("lock");
                  if (Lock != null && Lock.length != 0)
                  {
                        for (int i = 0; i < Lock.length; i++)
                        {
                              //Lock Update Query based on UserID                                                
                              stmt.executeQuery(lo);
                              System.out.println("Lock User: "+ Lock[i]);
                        }          
                  }                    


Now the feature I added in JSP is the check box's will be checked if DBvalue="Y" and unchecked if DBvalue ="N"

JSP
<input type="checkbox" <% if (rs.getString("USER_LOCKED").equals("Y")) {out.print("checked");  }%> name="lock" value="<%=rs.getString("UserID")%>"></td>


Java:
Need code modification here. Help !!
(user can submit all checked, so need to Update DB with "Y", user can uncheck few and check few so need to Update DB according... also NULL pointer exception issue)


Thx,
PH
0
Comment
Question by:princehyderabad
  • 10
  • 10
21 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16645240
>> rs.getString("USER_LOCKED").equals("Y")

You might want to add a null-check there for the value that rs.getString ( "USER_LOCKED" ) returns.

>> also NULL pointer exception issue

If stmt is not null in: >> stmt.executeQuery(lo);

- then it should not happen. BTW, what is lo? Lock[i] is not being used in the query.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 16645349
Hi,

>>>> <input type="checkbox" <% if (rs.getString("USER_LOCKED").equals("Y"))

<input type="checkbox" <% if (null != rs.getString("USER_LOCKED") && rs.getString("USER_LOCKED").equals("Y"))...

R.K
0
 

Author Comment

by:princehyderabad
ID: 16645619
TO: mayankeagle

>>You might want to add a null-check there for the value that rs.getString ( "USER_LOCKED" ) returns.
This value is comign from DB can't be null either Y or N.  Do I still need to check for null ?

>>what is lo? Lock[i] is not being used in the query.

String lo = "Update table set  Lock="Y"...... where userid="+Lock[i];

TO:rama_krishna580
what abt Java part, bascially I need help in Java code. When user select few more checkbox to lock user those need to update user_lock="Y" and if user deselect checkbox those user need to update user_lock="N".
0
Independent Software Vendors: 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!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16645653
>> This value is comign from DB can't be null either Y or N.  Do I still need to check for null ?

In that case, no.

>> String lo = "Update table set  Lock="Y"...... where userid="+Lock[i];

That should be written inside the for-loop. I don't see it there.
0
 

Author Comment

by:princehyderabad
ID: 16645768
>>That should be written inside the for-loop. I don't see it there.

Yeah that is in for-loop. I just quickly posted Q so didnt mentioned. But again my Java code is doing only setting Lock=Y, its not unlocking that is what I wanted.
By the way here is the actual java code:

String[] Lock = request.getParameterValues("lock");
               if (Lock != null && Lock.length != 0)
               {
                    for (int i = 0; i < Lock.length; i++)
                    {
                         //Lock Update Query based on UserID        
                         String lo = "Update table set  Lock="Y"...... where userid="+Lock[i];                              
                         stmt.executeQuery(lo);
                         System.out.println("Lock User: "+ Lock[i]);
                    }          
               }                  
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16645787
You also need to do set lock = "N" for those users which are not in the Lock[] array I guess.
0
 

Author Comment

by:princehyderabad
ID: 16646040
That is what I'm trying to Question. How to deal with it ? What if condition and how should I go for it.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16646269
Do you want to do it for all users not in the current list?
0
 

Author Comment

by:princehyderabad
ID: 16646396
No that is not what case. Let me explain you in details with pictures, I hope it will be clear then.

DB Table

UserID    Name     User_Lock
001         aaa           N
002         bbb           N
003         ccc            Y
004         ddd            N


GUI Display:

--------------------------------------------
[] un\lock All             name       UserID
---------------------------------------------
[ ]                             aaa           001
[ ]                             bbb           002
[x]                             ccc           003
[ ]                            ddd            004


Now Admin logins and want to unlock userid 003, and may be want to lock userid 003. So he check 1st record, and uncheck 3rd record here in this case.
Then it call my Java code does operations...

New Updated DB Table

UserID    Name     User_Lock
001         aaa           Y
002         bbb           N
003         ccc            N
004         ddd            N


GUI Display:

--------------------------------------------
[] un\lock All             name       UserID
---------------------------------------------
[x]                             aaa           001
[ ]                             bbb           002
[ ]                             ccc           003
[ ]                            ddd            004

Similarly Admin, can lock all or unlock all etc., and perform operation as he like.
I need Java code. Half of coding is done if you can see my posted Question up.
0
 

Author Comment

by:princehyderabad
ID: 16646403
Sorry correction in this line:

Now Admin logins and want to unlock userid 003, and may be want to lock userid 001. So he check 1st record, and uncheck 3rd record here in this case.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16646493
I think that Lock[] will only contain user-names of those check-boxes which were selected. So you could perhaps shoot another query to the DB, like: "UPDATE table set Lock = 'N' WHERE User_ID NOT IN ('a', 'b', 'c') " ) ; Here, 'a', 'b', etc are values in the Lock[] array - you will need to loop through the array to create this query.

0
 

Author Comment

by:princehyderabad
ID: 16646546
>> I think that Lock[] will only contain user-names of those check-boxes which were selected.
Yeah its contains UserID bascially.

>>UPDATE table set Lock = 'N' WHERE User_ID NOT IN ('a', 'b', 'c') " )
Do I need to ?. Because by default all records are set to 'N' when created. Only when if user has changed from status 'Y' to 'N' (checked to unchecked) then may be your above update can be called.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16646561
Yes, but after that, if he wants to assign 'N' to something which had 'Y', you will need that.
0
 

Author Comment

by:princehyderabad
ID: 16646619
Okay agreed what you says. Can you help me in code please:-

String[] Lock = request.getParameterValues("lock");
               if (Lock != null && Lock.length != 0)
               {
                    for (int i = 0; i < Lock.length; i++)
                    {
                         //Lock Update Query based on UserID        
                         String lo = "Update table set  Lock="Y"...... where userid="+Lock[i];                              
                         stmt.executeQuery(lo);
                         System.out.println("Lock User: "+ Lock[i]);
                    }          
               }  

I need to modify above and add Lock="N'.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 1000 total points
ID: 16646672
StringBuffer sb = new StringBuffer ( "UPDATE Table SET Lock = ''N' WHERE UserID IN ('" ) ;

for ( int i = 0 ; i < Lock.length ; i ++ )
{
  sb.append ( Lock[i] ) ;
  if ( i < Lock.length - 1 )
    sb.append ( "', " ) ;
}

sb.append ( "'" ) ;
String query = sb.toString () ;

Now execute this query using stmt.executeUpdate () ;
0
 

Author Comment

by:princehyderabad
ID: 16661392
Okay I think there were minor correction in code I did it, here is the correct code. BUT STILL IT DIDNT SOLVE MY SOLUTIONS.
==============
StringBuffer sb = new StringBuffer ("UPDATE USERS SET USER_LOCKED='N' WHERE USERID NOT IN ('");
if (Lock != null && Lock.length != 0)
{
for ( int i=0; i<Lock.length; i++ )
{
  sb.append (Lock[i]) ;
  if (i<Lock.length - 1)
    sb.append ("','") ;
}
}
sb.append ( "')" ) ;
String query = sb.toString () ;            
out.print(query);
=========

It didnt solve my soultions because I'm using Pagination to display records as records are in thousands. Let says 1st page has 10 records with 3 usered Locked="Y" and when I try to unlock one user, the query is unlocking this one person (plus) + its also unlocking Locked users which are in page 2nd to page 10th. I hope you got my point. NOW WHAT !!!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16664395
Oh well, didn't know about the pagination part. I guess you will have to maintain all the user-IDs in a hidden variable and before calling the servlet, the JSP should populate this hidden variable with the values of the user-IDs that were unchecked. Then you should shoot the above query for those.
0
 

Author Comment

by:princehyderabad
ID: 16701526
I can pass hidden variable but how to check or compare which are checked and which are unchecked.

I mean let say 10 records on page. 10 hidden variables. And say 3 checked box.
If I'm doing do while length of 10 hidden, I dont get proper values from those 3 checked box.
eg:
hidden[1] = checked[1]
hidden[2]=checedk[2]
hidden[3]=checked[3]
hidden[4] = nothing here
hidden[5] = nothing here...
....

But in real sense checked[2] would be 8th record of 10. and checked[3] would be 10th record.  I'm not getting it.  Let me know if my approache is wrong.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16706133
Don't make it so complicated. You can maintain one hidden variable which has all user-IDs (say comma-separated).
0
 

Author Comment

by:princehyderabad
ID: 16711679
How about this, let me know if its not correct approach:

Get all ID's in Hidden variables.
for (HiddenVariable.Length)
{
//make query string
}
Query1 = Assign all 10 ID with Lock=N
Get Check Box ID which are checked:
for(checkVariable.length)
{
//make query string
}
Query1=Assign Checked Variable ID with Lock=Y

Bascially you are runing 2 querys. First all records are set to 'N" and then I take checkbox which are not null and set to 'Y'.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16716966
You can do it either ways. Set those which are selected in the checkbox (which come from the request.getParameterValues ()) as 'Y' and the others (in the hidden varaible) can be set to 'N'. Yes - you require 2 queries for this or write a stored procedure which can do it in one shot by taking all values as parameters.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question