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.

<%
// 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>

Please help me. I need it urgently. Many thankx :rolleyes:

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.

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

i read your code and i can't check it further. please post the code before these:

<%
// 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

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

I am using these codes and have this error message:

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


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

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;
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

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

i'll make a program regarding this and i'll just send it to you. i mean post it here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.