RSS Forums RSS
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 5258 | Replies: 8
Reply
Join Date: Jan 2006
Posts: 7
Reputation: yup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yup yup is offline Offline
Newbie Poster

Array String not working?

  #1  
Jan 21st, 2006
Hi there,
I have a problem in my controller.jsp This page aims to retrieve the device's status from my database to display on the form. After that it will update device's new status when my customer wish to change the status.

So I declared a String array status [] to get new status from user and update to my database. but... it seem like my String array does not work, it can't get the device's new status.


[code]
<%
// retrive from database
String query = "SELECT * FROM device where locationID='"+5+"' order by device_name";
dbselect.rs = dbselect.executeQuery(query);

int i=0;
while ( (dbselect.rs).next() ) {

String device = dbselect.rs.getString("device_name");
String status = dbselect.rs.getString("status");
String deviceID = dbselect.rs.getString("deviceID");


String check1 = "";
String check2 = "";

if(status.equalsIgnoreCase("ON")){
check1="checked";
}

if(status.equalsIgnoreCase("OFF")){
check2="checked";
}
%>
<tr><TD>
<%= device %>
<input type="hidden" name="device<%= i%>" value="<%=device%>">
</TD>

<TD align="center">
<input type="hidden" name="ID<%= i%>" value="<%= deviceID%>">
<input type="hidden" name="status<%= i%>" value="<%= status%>">
<input type="radio" name="button<%= i%>" value="ON" <%=check1%> >0n
<input type="radio" name="button<%=i%>" value="OFF" <%=check2%> >Off
</TD>
</TR>
<%
i++ ;
} // end while loop
%>
</table></td>
<td><img src="Image/bathroom1.JPG"></td>

</tr>
</table>


<br>
<center>
<input type="submit" name="ChangeStatus" value="Save Change">
<input type="Reset" name="Reset" value="Don't Save">
</center>
</form>
</div>

<%

//update to database
String change= request.getParameter("ChangeStatus");
String [] status = new String[i];
if (change!=null) {
for (int n=0; n<i; n++)
{
status[n] = (String)request.getParameter("button") + n;
if (status[n]!=null) {
dbselect.executeUpdate = "UPDATE device SET status = '"+status[n]+"' WHERE locationID='"+5+"' and deviceID='"+deviceID+"'";
}
}
%>
</body>
[code]

Please help me. I need it urgently. Many thankx :rolleyes:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,749
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Array String not working?

  #2  
Jan 22nd, 2006
think about what you're doing there.
Each time the page is loaded you first get the data from the database and then write it back. That can't be what you're trying to do.

Then you're intending to write back what the user put into the webform, but you're never retrieving those parameters.
You're retrieving only one and that one doesn't even exist on the form so will always yield an empty string.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Jan 2006
Posts: 7
Reputation: yup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yup yup is offline Offline
Newbie Poster

Re: Array String not working?

  #3  
Jan 22nd, 2006
Hi jwenting,
Although the way you saying is not very friendly but I am thank you that at least you give me some suggestion.

1st, I DID retrieve my database and display on the webform successfully.

2nd, what I need to retrieve is only the status which is display on the radio button (ON or OFF). According to my database, there are quite a number of devices, so I need an array to store all the status. And I did declare it on my controller.jsp page.
String [] status = new String(i); 

3rd my String status tried to retrieve input by user through the form. This command was written inside a while loop.

for (int n=0; n<i; n++)
{
status[n] = (String)request.getParameter("button") + n;
}


I know there's something wrong with my codes but I can't figure out so I here I am asking for help. If you are pleased to help me, please read my post carefully before you raise your voice.

Kind regard,
yup
Reply With Quote  
Join Date: Jan 2006
Posts: 4
Reputation: ekstee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ekstee ekstee is offline Offline
Newbie Poster

Re: Array String not working?

  #4  
Jan 27th, 2006
i read your code and i can't check it further. please post the code before these:
[code]
<%
// retrive from database
i didn't see the program having <form>. so, there's no reloading of the page thus
String change= request.getParameter("ChangeStatus"); will return null...

please reply
Reply With Quote  
Join Date: Jan 2006
Posts: 7
Reputation: yup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yup yup is offline Offline
Newbie Poster

Re: Array String not working?

  #5  
Feb 6th, 2006
thanks ekstee,
Please take a look at these codes. Here you go!

<form action="Bedroom1.jsp" method="post"> 
<TABLE BORDER="1" width ="400" cellspacing="0" cellpadding = "2" align ="center" bgcolor="#006600">
<tr>

<td>
<TABLE BORDER="1" width ="150" cellspacing="0" cellpadding = "2" align ="center" bgcolor="#009900">
	<TR><TH colspan="2" bgcolor="#99FF66" bordercolor="#009900"><span class="style5">Hanny's room</span></TH>
	</tr>
	<tr><TH align="center"><strong>Device</strong></TH> 
	<tH align="center">Status</tH></tr>

<%
String query = "SELECT * FROM device where locationID='"+1+"' order by device_name";
dbselect.rs = dbselect.executeQuery(query);

int i=0;
while ( (dbselect.rs).next() ) {
 
	String device  = dbselect.rs.getString("device_name");
	String status  = dbselect.rs.getString("status");
	
 
	String check1 = "";
	String check2 = "";
	
	if(status.equalsIgnoreCase("ON")){
		check1="checked";
	}
	
	if(status.equalsIgnoreCase("OFF")){
		check2="checked";
	}
%>
		<tr><TD>
			<%= device %>
			<input type="hidden" name="device<%= i%>" value="<%=device%>">
		</TD>
			
			<TD align="center">
				<input type="hidden" name="status<%= i%>" value="<%= status%>">
 				<input type="radio" name="button<%= i%>" value="ON" <%=check1%> >0n
				<input type="radio" name="button<%=i%>" value="OFF" <%=check2%> >Off
			</TD>
		</TR>
<%
i++	;
} // end while loop
%>
</table></td>
<td><img src="Image/hanny.JPG"</td>

</tr>
</table>
 

<br>
<center>
	<input type="submit" name="ChangeStatus" value="Save Change">  
	<input type="Reset" name="Reset" value="Don't Save">
</center>
</form>

Kind regard,
yup
Reply With Quote  
Join Date: Jan 2006
Posts: 7
Reputation: yup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yup yup is offline Offline
Newbie Poster

Re: Array String not working?

  #6  
Feb 6th, 2006
I am using these codes and have this error message:

<%
String change= request.getParameter("ChangeStatus");
String [] status = new String[i];

if (change!=null)
{
for(int n=0; n<=i; n++) {
status[n] =request.getParameter("button") +n;
if (status[n]!= null)
out.println(status[n]);
}
}


%>

Error message:
An error occurred on page Bedroom1.jsp
The exception was:
java.lang.ArrayIndexOutOfBoundsException: 2
Reply With Quote  
Join Date: Jan 2006
Posts: 4
Reputation: ekstee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ekstee ekstee is offline Offline
Newbie Poster

Re: Array String not working?

  #7  
Feb 6th, 2006
wait, i think your contoller page contains the data retrieval from the database while the bedroom1.jsp is the updating of the database. right? i'll read the code again.

<%

//update to database
String change= request.getParameter("ChangeStatus");
String [] status = new String[i];
if (change!=null) {
for (int n=0; n<i; n++)
{
status[n] = (String)request.getParameter("button") + n;
if (status[n]!=null) {
dbselect.executeUpdate = "UPDATE device SET status = '"+status[n]+"' WHERE locationID='"+5+"' and deviceID='"+deviceID+"'";
}
}
%>

is the code above supposed to be at the bedroom1.jsp
Reply With Quote  
Join Date: Jan 2006
Posts: 7
Reputation: yup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yup yup is offline Offline
Newbie Poster

Re: Array String not working?

  #8  
Feb 8th, 2006
well, actually controller page or bedroom page is the same. Bedroom page is retrieved the data from the database, then update again to the database if there is any changes. Sorry to confused you. Cause, I named it wrongly
Reply With Quote  
Join Date: Jan 2006
Posts: 4
Reputation: ekstee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ekstee ekstee is offline Offline
Newbie Poster

Re: Array String not working?

  #9  
Feb 9th, 2006
i'll make a program regarding this and i'll just send it to you. i mean post it here.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:16 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC