Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet 17284-how-to-make-enable how to make enable/disable textbox in while(rs.next)
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Tay Hock Lye
how to make enable/disable textbox in while(rs.next)
1 Answer(s)      3 years and 6 months ago
Posted in : JSP-Servlet


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");
}

}


Advertisement
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 and Disable Tab in Java
Enable and Disable Tab in Java       In this section you will learn how to enable... number. To enable or disable any tab you can use the setEnabledAt(int index
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
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
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 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 <
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 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 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
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 this pattern???
how to make this pattern???  how to make following pattern in javascript
How to make elements invisible ?
How to make elements invisible ?   How to make elements invisible
how make ID - Ajax
how make ID  how make a ID in eyeball chat
how to make exampage in jsp ?
how to make exampage in jsp ?  how to make a online exam page in jsp and servelet
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 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 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 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 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 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 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 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 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
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
 

 

 

DMCA.com