how to make enable/disable textbox in while(rs.next)

how to make enable/disable textbox in while(rs.next)

Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want to update my data, the data are not updated correctly. When i remove the enable/disable function,the data are updated correctly. Is my javascript wrong? How do i correct it? Please help. Thank you. (only the update part is wrong)

JSP: editDepartment.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
</style>
<script language="javascript" type="text/javascript">
function enable_text(status)
{
    status=!status;
    deptName=document.updateDepartment.elements['updateDeptName'];
    HOD=document.updateDepartment.elements['updateHOD'];
    for (i=0;i<deptName.length;i++)
    {
        if ( document.updateDepartment.others[i].checked != status )
            {
        deptName[i].disabled = status;
        HOD[i].disabled = status;
            }
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body onload='disable_text(false)'>
<form name="addDepartment" action="AddDepartment">Add new
department : <br>
Department name : <input type="text" name="deptTxt" /> <br>
HOD name : <input type="text" name="HODTxt" /> <br>
<input type="submit" value="Add" /></form>
<form name="deleteDepartment" action="DeleteDepartment"><br>
<br>
Delete department : <br>
    <%
        Connection con = null;
        try {
            int row=1;
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE", "tay", "tay");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt
                    .executeQuery("SELECT * FROM DEPARTMENT ORDER BY DEPARTMENT.DEPTNAME");
            out.println("<table id=delDept>");
            out.println("<tr>");
            out.println("<td>Delete<td>deptID<td>deptName<td>HOD");
            out.println("</tr>");
            while (rs.next()) {
                out.println("<td><input type='checkbox' name='chk' value='"+rs.getString("DEPTID")+"'>");
                out.println("<td>"+rs.getString("DEPTID"));
                out.println("<td>"+rs.getString("DEPTNAME"));
                out.println("<td>"+rs.getString("HOD"));
                out.println("</tr>");
                row = row + 1;
            }
            out.println("</table>");
            out.println("<input type=hidden name=counterloop value='"
                    + (row-1) + "'>");
            stmt.close();
            rs.close();
            //con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    %>
<br>
<input type="submit" value="Delete" />
</form>
<form name="updateDepartment" action="UpdateDepartment">
<br><br>
Update department : <br>
    <%
        try {
            int row2=1;
            Statement stmt1 = con.createStatement();
            ResultSet rs1 = stmt1
                    .executeQuery("SELECT * FROM DEPARTMENT ORDER BY DEPARTMENT.DEPTNAME");
            out.println("<table id=updDept>");
            out.println("<tr>");
            out.println("<td>Select<td>deptID<td>deptName<td>HOD");
            out.println("</tr>");
            while (rs1.next()) {
                out.println("<tr>");
                out.println("<td><input type='checkbox' name='others' onclick='enable_text(this.checked)'>");
                //out.println("<td><input type='checkbox' name='others'>");
                out.println("<td><input type=hidden name=updateDeptID value='"+rs1.getString("DEPTID")+"'><input type=text name=updateDeptID disabled='true' value='"+rs1.getString("DEPTID")+"'>");
                out.println("<td><input type=text name=updateDeptName disabled='true' value='"+ rs1.getString("DEPTNAME")+"'>");
                out.println("<td><input type=text name=updateHOD disabled='true' value='"+rs1.getString("HOD")+"'>");
                out.println("</tr>");
                row2=row2+1;
            }
            out.println("</table>");
            out.println("<input type=hidden name=counterloop2 value='"+ (row2-1) + "'>");
            stmt1.close();
            rs1.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    %>
<br>
<input type="submit" value="Update" />
</form>
</body>
</html>

servlet: UpdateDepartment.java

package testpackage;

import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement;

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

/** * Servlet implementation class UpdateDepartment */ public class UpdateDepartment extends HttpServlet { private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String deptID[] = request.getParameterValues("updateDeptID");
    String deptName[] = request.getParameterValues("updateDeptName");
    String HOD[] = request.getParameterValues("updateHOD");
    Integer row = Integer.parseInt(request.getParameter("counterloop2"));
    System.out.println(row);
    Connection con = null;

    //if (deptID != null && deptID.length != 0)
    //{
    for(int i=0;i<row;i++)
    {   
    try {
        System.out.println(deptID[i]);  
        System.out.println(deptName[i]);    
        System.out.println(HOD[i]); 
        Class.forName("oracle.jdbc.driver.OracleDriver")
            .newInstance();
            con = DriverManager
            .getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE",
                    "tay", "tay");

            Statement stmt = con.createStatement();
            stmt.executeUpdate("UPDATE DEPARTMENT SET DEPTNAME='"+deptName[i] +"', HOD='"+HOD[i]+"' WHERE DEPTID='"+ deptID[i]+"'");
            con.close();
            }
    catch (Exception e) {
            System.out.println(e);
        }

}
//}
    response.sendRedirect("editDepartment.jsp");
}

}

View Answers

March 10, 2011 at 4:15 PM

JSP edit application:

1)editapplication.jsp:

<%@ page import="java.sql.*" %>
<html>
<script>
function enable_text(){
    var i=0;
    deptName=document.form.elements['name'];
    HOD=document.form.elements['address'];
    for (i=0;i<deptName.length;i++)  {
        if(document.form.check[i].checked){
        deptName[i].disabled = true;
        HOD[i].disabled = true;
            }
}
}
</script>
<form name="form"  method="post" action="retcheck.jsp">
 <table border="1">
<tr><th></th><th>Name</th><th>Address</th></tr>
<%
int i=0;
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";

int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from data";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
<tr><td><input type="checkbox" value="<%= rs.getString("id")%>"  onclick='enable_text()' name="check">
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
<input type="submit" value="Edit">
</form>
</html>

2)retcheck.jsp:

<%@ page import="java.sql.*" %>

<%
String id[]=request.getParameterValues("check");
%>
<form name="form" method=post action="up.jsp">
<table border=1>
<tr><th></th><th>Name</th><th>Address</th></tr>
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
ResultSet rs = null;
Statement st=null;
st=conn.createStatement();
for(int a=0;a<id.length;a++){
rs = st.executeQuery("select * from data where id='"+id[a]+"'");
while(rs.next()){ 
    %>
<tr>
<td><input type="hidden" name="id" value="<%=rs.getString("id")%>" > </td>
    <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
    <td><input type="text" name="address" value="<%=rs.getString("address")%>"></td></tr>
      <%
}
}
%>
</table>
<input type="submit" value="update">
</form>

3)up.jsp:

<%@ page import="java.sql.*" %>
<%
String id[]=request.getParameterValues("id");
String name[]=request.getParameterValues("name");
String add[]=request.getParameterValues("address");
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");

Statement st=null;
st=conn.createStatement();
for(int i=0;i<id.length;i++){
st.executeUpdate("update data set  name='"+name[i]+"',address='"+add[i]+"' where id='"+id[i]+"'");
out.println("Data is updated successfully");
}
response.sendRedirect("editapplication.jsp");
%>









Related Tutorials/Questions & Answers:
how to make enable/disable textbox in while(rs.next)
how to make enable/disable textbox in while(rs.next)  Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want.../disable function,the data are updated correctly. Is my javascript wrong? How do i
how to make enable/disable textbox in while(rs.next)
how to make enable/disable textbox in while(rs.next)  Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want.../disable function,the data are updated correctly. Is my javascript wrong? How do i
Advertisements
how to enable and disable a input type in jsp - JSP-Servlet
how to enable and disable a input type in jsp  how to disable the input type and enable based on some input in the input type   Hi Friend, Try the following code: function function1(){ var n
enable disable checkbox in javascript
enable disable checkbox in javascript  How to enable disable checkbox in javascript
enable disable table
enable disable table  hi I have table with 3 columns if i click one column the other 2 column should disable pls can anyboby help me
Enable and Disable Tab in Java
Enable and Disable Tab in Java       In this section you will learn how to enable... name and index number. To enable or disable any tab you can use the setEnabledAt
Task manager enable and disable thru java
Task manager enable and disable thru java  I would like to know, how to enable and disable task manager using java. Kindly, please Let me know
how to make seperate view count for each row while clicking view button
how to make seperate view count for each row while clicking view button  I am getting problem with view count when i click view button the seperate count should be happen for each row here is my code ** Tabledata.jsp <
Jsp code to enable and disable certain links using jsp code - Java Beginners
Jsp code to enable and disable certain links using jsp code  Hi... have to enable/disable a link.I need a code to do that. Thanks in advance... but with only a certain links only visible and enabled. now my doubt is how can i
How to show data from database in textbox in jsp
How to show data from database in textbox in jsp   How to show data from database in textbox in jsp   Here is an example that retrieve... countryname LIKE '"+name+"%'"); while(rs.next()){ buffer=buffer
how make ID - Ajax
how make ID  how make a ID in eyeball chat
How to make elements invisible ?
How to make elements invisible ?   How to make elements invisible
how to make this pattern???
how to make this pattern???  how to make following pattern in javascript
how to make exampage in jsp ?
how to make exampage in jsp ?  how to make a online exam page in jsp and servelet
showing the information of database in textbox
showing the information of database in textbox  how to make a information of a database make appear to the user in the textbox
How to check text in textbox using JavaScript
How to check text in textbox using JavaScript  How to check text in textbox using JavaScript I have a form in HTML that contains text feilds and a submit button. The main purpose of this form is to take input from user. My
JTree Multiple Selection
Enable and Disable Multiple Selections in a JTree Component       In this section, you will learn how to enable and disable the multiple selections in a JTree
how to make employee i card
how to make employee i card  hello friends can anyone help me.. I have to make a project to make Identity card for employees...but i am facing..."; ResultSet rs = stmt.executeQuery(strQuery); while(rs.next()){ %>
How to display current date in textbox - Date Calendar
How to display current date in textbox  Hi guys, I'm really need your...;% } This is my problem: I want to display current date in textbox... show in textbox. Please help me to solve that problem, I will wait your answer
how to make a program on array
how to make a program on array  When you make a program on array that the element will move downward and upward and when you input twice 0 then thats the time that it will not move. pls. give me a formula...tnx
How to make selectOneMenu scrollable
How to make selectOneMenu scrollable  how to make the selectOneMenu scrollable? I have around 20 values in drop down and i want the scrollable functionality after 5 values. Kindly reply as soon as possible. Thanks Shikha
how to display textbox value based on selected option value?
how to display textbox value based on selected option value?  Hi,I have some problem. I use jsp and ajax. first select: 1 second select:2 based...("<th>Time</th></tr>"); while(rs.next()){ data
how to make paging with function ?
how to make paging with function ?  how to make paging with function ?   //this page is display.php <?php</p> $con=mysql_connect... name user password gender e-mailid mobile no image edit delete "; while($row
How to write a loop and a while loop
How to write a loop and a while loop  How do I write a 1 loop and a 1 while loop for the example code: public boolean isTheFirstOneBigger (int num1, int num2) { if (num1 > num2) { return true
How to create textbox on combo value selection using javacsript in jsp?
How to create textbox on combo value selection using javacsript in jsp?  dynamically create textbox on combo value selection. when select multiple values then create multiple textboxes
how make excel
how make excel  how make excel spreadsheet IN JAVA. please send this code argently   Hello Friend, Try the following code: import java.io.*; import org.apache.poi.hssf.usermodel.HSSFSheet; import
how to make multiple rectangles
how to make multiple rectangles  I,m a beginner , m sorry if the question is really simple i have an array list.I want to print the contents...(JFrame.EXIT_ON_CLOSE); //Set JFrame size setSize(400,400); //Make JFrame
How to display on textbox using ObjectUtil class
How to display on textbox using ObjectUtil class       In this tutorial we will see how to display a text in textbox using ObjectUtil class. Here
How to insert dynamic textbox values into database using Java?
How to insert dynamic textbox values into database using Java?  Hi I am trying to insert dynamic textbox values to database, my jsp form have 2... these dynamic textbox values to database(Oracle 11g)...Please help me out. I have
How to make a chain, make a chain, chain
How to make a chain       To learn how to design a chain in the photoshop has become so easy by this example, it has some important instruction to make it so follow
how to make a field left justification
how to make a field left justification  how to make a field left justification.need code.thanks in advance
how to make a label left alignment?
how to make a label left alignment?  how to make a label left alignment
How to make first JSP page?
How to make first JSP page?  Hello, How I can make first JSP page? Thanks
How to make bubbles, make bubbles, bubbles
How to make bubbles      ... to make it easily to draw. New File: First take colored background file.ADS.... ADS_TO_REPLACE_3 Duplicate: Now make duplicate layer and press Ctr
how to make paging with class and ajax
how to make paging with class and ajax  paging with class and ajax
how to get the values from dynamically generated textbox in java?
how to get the values from dynamically generated textbox in java?  I... textbox corresponding to the data. I want to get data from textboxes(generated as per the retrieved data) and I want to store textbox values into the two table
How to make directory in java
Description: This example demonstrate how to create a directory at specified path. Code: import java.io.File; public ... is that it will make a dir at root directory of C drive
how to get data using dropdownlist, textbox and search button in jsp
how to get data using dropdownlist, textbox and search button in jsp  Hi, I want to display data using jsp,javascript and mysql. My Q. is If i select...' in textbox and click on search button it should show me all the title names
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
How to make a transparent text
How to make a transparent text Learn how to make a transparent... mask tool (T key) and make formatting as looking here. Write Text: Now write... text make copy (Ctr +C) and past (Ctr + V).ADS_TO_REPLACE_2 Drop Shadow: Go
How To get DgroupId while using maven.
How To get DgroupId while using maven.  Hello everyone, I read... it and it is working, but when i want to make other project for speech to text converter ,i can't find the right "DgroupId", also i can't get how to make
How to make a bone, make a bone, a bone
How to make a bone       This example bone design example, It is very easy. If you are looking for, please follow now. New File: Create a new file.ADS_TO_REPLACE_1
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
HOW TO DISPLAY ID IN TEXTBOX BASED ON COMBOBOX SELECTION IN A SAME PAGE
HOW TO DISPLAY ID IN TEXTBOX BASED ON COMBOBOX SELECTION IN A SAME PAGE  Dear sir, Am having one table called rolemaster. In that there are 2 fields... is how to get Roleid in textbox when i select Role_name from combobox in a same
PHP While Loop
While Control Structure: While is the simplest form of loop. While loop checks..., and become infinite loop. While loop is also known as pre-test loop. Basic format of while loop is as follows: ADS_TO_REPLACE_1 while(condition
How to make a new List in Java
How to make a new List in Java  Hi, I have to create list object in Java. How to make a new List in Java? Thanks (adsbygoogle = window.adsbygoogle || []).push({});   Hi, The easiest way is to use
how to make multiple choice questions
how to make multiple choice questions  I have to make a multiple choice choice using JSP/Servlets. I created a a session bean for the questions and answers. how to associate group of questions to a user? so each user will have

Ads