?
Solved

jsp checkbox display for loop value according to array will have duplication

Posted on 2009-02-08
16
Medium Priority
?
3,181 Views
Last Modified: 2013-11-24
The following code displaying multiple checkbox with same value is the checkbox depends on the for loop. When the arr.length increases, the checkbox number for the same value will increase.

However, I do not know how to fix it.

I want to databind the data with checkbox, when member comes back, he can view the checked items. For each time of update, an array length increases, the checkbox number for the same item also increases.

<%  
String sql = "SELECT s.id as id, s.cook as cook, m.cooking as e FROM cooking s join member m where m.id=" + myID;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
String arr[]= new String [rs.getString("e").split("\\,").length];
int i;
for(i=0;i<arr.length;i++)
{
arr = rs.getString("e").split("\\,");
%>
<input TYPE=checkbox NAME="cooking" VALUE="<%=rs.getInt("id")%>" <%if(rs.getInt("id")==Integer.parseInt(arr[i])){out.print("checked");}%>><%=rs.getString("cook")%><BR>
<%
}
}%>

Open in new window

0
Comment
Question by:techques
  • 9
  • 4
  • 3
16 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 23583233
Why are u splitting the string? Can you tell me how many rows are returned from your database for your query and also paste one of the row values here so that I can have a look at it.
0
 

Author Comment

by:techques
ID: 23583834
In cooking table, there are 2 fields: id, cook
1 milk
2 juice
3 sauce
.....totally 10 rows of data

In member table, there is a field 'cooking'
It records the checkbox (checked values) in a string, e.g. 2,5,9,10

When the member comes back to the checkbox page, I need to do databind with those input values in cooking field of member table and the cooking table. So that member knows what did he chose last time.

I need to split the string 2,5,9,10, when it matches with the id in cooking table, it is checked.

But, the for loop will display the checkbox same value for the array length of the split string.

I tried 2 days but still cannot think how can fix it.

Could you help?
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23584214
First of all I think your query is wrong. If you want to join a table you need to specify the columns to join from both tables. From your table structure what you can actually do is first query the member table based on the member id the values for cooking (e.g. 2,5,9,10 ) from member table and store this in a variable (let us call it varCooking). Then query all the cooking table values loop through the results and check the id exists in the varCooking variable. You will not have do any splits you can compare if the string exists using java string functions. I dont remember the function names and all because I have used around 4 years back. Hope this helps..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:techques
ID: 23587618
Can you show me the code? Is it need 2 queries?
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23588307
I am not an expert in JSP but you would want to create 2 queries as I said earlier in post 23584214. That post contains all you need to do this thing working
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 23592179
Yes you can manage it with 2 queries.. your code will look something like below..
no need to manage a string array here
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%  
String sql = "SELECT s.id as id, s.cook as cook FROM cooking s";
String sql1 ="Select m.cocking as e from member m where m.id="=myID;
ResultSet rs = s.executeQuery(sql);
ResultSet rs1=s.executeQuery(sql1);
rs1.next();
String selected= rs1.getString("e");
while (rs.next()) {
%>
<input TYPE=checkbox NAME="cooking" VALUE="<%=rs.getInt("id")%>" <%if(select.indexOf(rs.getString("id"))>-1){out.print("checked");}%>><%=rs.getString("cook")%><BR>
<%
}
}%>

Open in new window

0
 

Author Comment

by:techques
ID: 23594604
the code throws exception:
throws at while (rs.next()) {

javax.servlet.ServletException: java.sql.SQLException: Operation not allowed after ResultSet closed

com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:644)
com.mysql.jdbc.ResultSet.next(ResultSet.java:6663)

and why it does not need array to split the rs1.getString("e"); which has the value "1,3,5," for example?

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%  
String sql = "SELECT s.id as id, s.cook as cook FROM cooking s";
String sql1 ="Select m.cooking as e from member m where m.id=" + myID;
ResultSet rs = s.executeQuery(sql);
ResultSet rs1=s.executeQuery(sql1);
rs1.next();
String selected= rs1.getString("e");
while (rs.next()) { //throws exception
%>
<input TYPE=checkbox NAME="cooking" VALUE="<%=rs.getInt("id")%>" <%if(selected.indexOf(rs.getString("id"))>-1){out.print("checked");}%>><%=rs.getString("cook")%><BR>
<%
}
}%>
<%
rs.close();
rs1.close();
s.close();
con.close();
%>	
<input type="submit" name="Submit" value="cook" >

Open in new window

0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 23594672
oops sorry I missed the fact that you will need anotherStatement as well.

create another statement say s1 ( at the same place where you are initiating s).
and change
ResultSet rs1=s.executeQuery(sql1);

to
ResultSet rs1=s1.executeQuery(sql1);

you dont need the string because I am using (indexOf) to find your string around.
0
 

Author Comment

by:techques
ID: 23594878
The databind works now, thanks for help.

when I select checkbox numbers: 4
it insert into the field value '4,'

when i select checkbox numbers: 4,8
it inserts into the field value '3,5,2,24,4,2,6,10,4,8,' instead of '4,8,'

What happen to my java code below?





private int myID=1;
private String in[];
in = request.getParameterValues("cooking");
for(int i=0; i<in.length;i++){
inappend += in[i]+",";
}
save();
 
public void save() throws MemberException
{
MySQLConnector db = new MySQLConnector();
Connection con = db.Getcon();
String upd;
int isWrite;
Statement stmt=null;
upd = "update member set cooking";
upd = upd + "='"+inappend+"'";
upd = upd + " where id = '" + myID +"';";
try
{
stmt = con.createStatement();
}

Open in new window

0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 400 total points
ID: 23594921
thats interesting..!
whats the value of inappend when you start your code here?

also in above code..

add this line..

for(int i=0; i<in.length;i++){
inappend += in[i]+",";
}
System.out.println("The values are"+inappend);
save();
..
..



This is to check what values are going in..

Also it looks like you are using global variables ( or class level variables).. its not a good idea to do in a servlet.
0
 

Author Comment

by:techques
ID: 23595193
you are using global variables ( or class level variables)..

I think it was caused by that problem. I set those variables insides doGet() and pass the inappend to method save(String inappend) it works now.

Thank you very much for your help.
0
 

Author Comment

by:techques
ID: 23595196
sorry, i got to add points
0
 

Author Closing Comment

by:techques
ID: 31544186
Excellent advice and very detail guideline. I learnt a lot from Kuldeepchaturvedi and he is very professional.
0
 

Author Comment

by:techques
ID: 23631189
Sorry, it does not work if I select an item with id = 123, (e)
The checkbox will check 1,2,3,12,123.

String selected= rs1.getString("e");
selected = 123

But, selected.indexOf(rs.getString("id"))>-1

indexOf will get 1,2,3,12,123 even the e value in database is 123.


String sql = "SELECT s.id as id, s.cook as cook FROM cooking s";
String sql1 ="Select m.cooking as e from member m where m.id=" + myID;
ResultSet rs = s.executeQuery(sql);
ResultSet rs1=s.executeQuery(sql1);
rs1.next();
String selected= rs1.getString("e");
while (rs.next()) { 
%>
<input TYPE=checkbox NAME="cooking" VALUE="<%=rs.getInt("id")%>" <%if(selected.indexOf(rs.getString("id"))>-1){out.print("checked");}%>><%=rs.getString("cook")%><BR>

Open in new window

0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 23631695
yup you are right.. lets do it differently



String selected= rs1.getString("e");
String [] arrSel = selected.split("\\,");
java.util.ArrayList alSel= new ArrayList(Arrays.asList(arrSel));
while (rs.next()) {
%>
<input TYPE=checkbox NAME="cooking" VALUE="<%=rs.getInt("id")%>" <%if(alSel.contains(rs.getString("id"))>-1){out.print("checked");}%>><%=rs.getString("cook")%><BR>


Also for your other question, I will have to look at it later, have a busy day today.
0
 

Author Comment

by:techques
ID: 23638538
<%if(alSel.contains(rs.getString("id"))>-1)

needs to changed to be

<%if(alSel.contains(rs.getString("id")))

and it works now

Thank you very much

By the way, do you know how to set and get Vector object's parameters?

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.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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