Click here to Skip to main content
11,501,513 members (71,888 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MySQL JSP
I have a webpage called updategroup.jsp.
In this, I have one combobox called groupname.
I am getting all the available groupnames from database to this drop down box. Based on the value selected in dropdown box, I have to fetch data from database and I should display in text boxes and I need to update mysql table.

But my problem is if I select the first combobox value, I am getting value whenever I am selecting second value in combobox, in textboxes previous values are displaying and the total mysql table is updating.

Below is the code for updategroup.jsp:
<%@ Page import="java.sql.*" %>
 
<%@ Page import="javax.servlet.*" %>
 
<%
            Connection con=null;
            String sqlQuery="";
            Statement st=null;
            ResultSet rs=null;
            String groupname_var="";
            String data_firstname="";
            String data_lastname="";
            String data_middlename="";
            String data_add1="";
            String data_add2="";
            String data_city="";
            String data_state="";
            String data_country="";
            int data_zip=0;
            String data_phone="";
            String data_mn="";
            String data_emailid="";
%>
<html>
<head>
    <title>edit user</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <style>
        td
        {
            font: 13px Arial,sans-serif;
        }
        th
        {
            font: bold 13px Arial, sans-serif;
            color: white;
            background-color: #153e7e;
        }
        input
        {
            font: 13px Arial,sans-serif;
            border: solid 1;
        }
        select
        {
            font: 13px Arial,sans-serif;
        }
        #button
        {
            cursor: hand;
        }
        A:hover
        {
            color: red;
        }
        
        textarea
        {
            resize: none;
        }
        td
        {
            border: none;
        }
    </style>
    <script type="text/javascript">
        function reset() {
            alert("etst");
            document.getElementByID("subzone").value = "";
        }
    </script>
    <script type="text/javascript">
        window.history.forward();
        function noback() { window.history.forward(); }
    </script>
    <script type="text/javascript">
        function myoriginaldata() {
            document.getElementById("fn").value = document.getElementById("hfn").value;
            document.getElementById("ln").value = document.getElementById("hln").value;
            document.getElementById("mn").value = document.getElementById("hmn").value;
            document.getElementById("add1_id").value = document.getElementById("hadd1").value;
            document.getElementById("add2_id").value = document.getElementById("hadd2").value;
            document.getElementById("city_id").value = document.getElementById("hcity").value;
            document.getElementById("state_id").value = document.getElementById("hstate").value;
            document.getElementById("country_id").value = document.getElementById("hcountry").value;
            document.getElementById("zip_id").value = document.getElementById("hpostalcode").value;
            document.getElementById("phone_id").value = document.getElementById("hphoneno").value;
            document.getElementById("mobile_id").value = document.getElementById("hmobileno").value;
            document.getElementById("email_id").value = document.getElementById("hemailid").value;
        }
    </script>
</head>
<body>
    <form method="post" name="frm_addUser" action="./updategroup.jsp">
    <table width="40%;" border="0" cellpadding="5" cellspacing="1" bgcolor="" bordercolor="#333366"
        align="center">
        <tbody>
            <tr>
                <th colspan="2" align="middle">
                    <font size="4">Edit Group</font>
                </th>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Group Name</font>
                </td>
                <%
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
                    sqlQuery="select groupname from grouplist";
                    st=con.createStatement();
                    rs=st.executeQuery(sqlQuery);
                %>
                <td>
                    <select name="group_name" id="gid" style="width: 200px"  önchange="this.form.submit()">
                        <option value="">Select groupname</option>
                        <% while(rs.next()) {%>
                        <option value="<%=rs.getString("groupname")%>">
                            <%=rs.getString("groupname")%></option>
                        <% }
 
                        rs.close();
                        st.close();
                        con.close();
                        %>
                </td>
            </tr>
            <%
                groupname_var=request.getParameter("group_name");
                Class.forName("com.mysql.jdbc.Driver");
                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
                sqlQuery="select firstname,middlename,lastname,address1,address2,city,state,country,zipcode,phoneno,mobileno,emailid from groupdata where group_name='"+groupname_var+"'";
                st=con.createStatement();
                rs=st.executeQuery(sqlQuery);
                while(rs.next())
                {
                data_firstname=rs.getString("firstname");
                data_lastname=rs.getString("lastname");
                data_middlename=rs.getString("middlename");
                data_add1=rs.getString("address1");
                data_add2=rs.getString("address2");
                data_city=rs.getString("city");
                data_state=rs.getString("state");
                data_country=rs.getString("country");
                data_zip=rs.getInt("zipcode");
                data_phone=rs.getString("phoneno");
                data_mn=rs.getString("mobileno");
                data_emailid=rs.getString("emailid");
                }
                rs.close();
                st.close();
                con.close();
            %>
            <input type="hidden" id="hgroupname_id" value="<%=groupname_var%>">
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">First Name </font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="firstname" style="width: 200px" value="<%=data_firstname%>" id="fn">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Middle Name </font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="middlename" style="width: 200px" value="<%=data_middlename%>" id="mn">
                </td>
            </tr>
            <tr>
                <td colspan="1" bordercolor="Gainsboro">
                    <font size="4">Last Name </font>
                    <td bordercolor="Gainsboro">
                        <input name="lastname" style="width: 200px" type="text" value="<%=data_lastname%>"
                            id="ln">
                    </td>
            </tr>
            <tr>
                <td colspan="1" bordercolor="Gainsboro">
                    <font size="4" color="green">Address</font>
                </td>
                <td bordercolor="Gainsboro">
                     
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Line 1</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="lineone" style="width: 200px" value="<%=data_add1%>" id="add1_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Line 2</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="linetwo" style="width: 200px" value="<%=data_add2%>" id="add2_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">City</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="city" style="width: 200px" value="<%=data_city%>" id="city_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">State</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="state" style="width: 200px" value="<%=data_state%>" id="state_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Country</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="country" style="width: 200px" value="<%=data_country%>" id="country_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Pin/Zip Code </font>
                </td>
                <%
                if(data_zip!=0)
            { %>
                <td bordercolor="Gainsboro">
                    <input name="postalcode" style="width: 200px" value="<%=data_zip%>" id="zip_id">
                </td>
            </tr>
            <%
                }
            %>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Phone No</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="phoneno" value="<%=data_phone%>" style="width: 200px" id="phone_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">Mobile No</font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="mobileno" style="width: 200px" value="<%=data_mn%>" id="mobile_id">
                </td>
            </tr>
            <tr>
                <td bordercolor="Gainsboro">
                    <font size="4">E-Mail </font>
                </td>
                <td bordercolor="Gainsboro">
                    <input name="email" style="width: 200px" value="<%=data_emailid%>" id="email_id">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2" bordercolor="#f8f8ff">
                    <input type="submit" value="Update" style="border-right: green 1px solid; border-top: GREEN 1px solid;
                        font: GREEN 8pt Tahoma; border-left: GREEN 1px solid; width: 90px; border-bottom: GREEN 1px solid;
                        height: 34px" name="bOk"  önclick="return checkInput();">
                    <input type="reset" value="Cancel" style="border-right: GREEN 1px solid; border-top: GREEN 1px solid;
                        font: GREEN 8pt Tahoma; border-left: GREEN 1px solid; width: 90px; border-bottom: GREEN 1px solid;
                        height: 34px" name="cancel">
                </td>
            </tr>
            <%
                try
                {
                String fn_var=request.getParameter("firstname");
                String ln_var=request.getParameter("lastname");
                String mn_var=request.getParameter("middlename");
                String add1_var=request.getParameter("lineone");
                String add2_var=request.getParameter("linetwo");
                String city_var=request.getParameter("city");
                String state_var=request.getParameter("state");
                String country_var=request.getParameter("country");
                String zipcode_var=request.getParameter("postalcode");
                String phone_var=request.getParameter("phoneno");
                String mobile_var=request.getParameter("mobileno");
                String email_var=request.getParameter("email");
                Class.forName("com.mysql.jdbc.Driver");
                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
                st=con.createStatement();
            %>
            <input type="hidden" id="hfn" value="<%=fn_var%>">
            <input type="hidden" id="hmn" value="<%=mn_var%>">
            <input type="hidden" id="hln" value="<%=ln_var%>">
            <input type="hidden" id="hadd1" value="<%=add1_var%>">
            <input type="hidden" id="hadd2" value="<%=add2_var%>">
            <input type="hidden" id="hcity" value="<%=city_var%>">
            <input type="hidden" id="hstate" value="<%=state_var%>">
            <input type="hidden" id="hcountry" value="<%=country_var%>">
            <input type="hidden" id="hpostalcode" value="<%=zipcode_var%>">
            <input type="hidden" id="hphoneno" value="<%=phone_var%>">
            <input type="hidden" id="hmobileno" value="<%=mobile_var%>">
            <input type="hidden" id="hemailid" value="<%=email_var%>">
            <%
                    if(groupname_var!=null &&  fn_var!=null && mn_var!=null && ln_var!=null && add1_var!=null && add2_var!=null && city_var!=null && state_var!=null && country_var!=null && zipcode_var!=null && phone_var!=null && mobile_var!=null && email_var!=null)
                    {
                        if(!groupname_var.equals("") && !fn_var.equals("") && !ln_var.equals("") && !mn_var.equals("") && !add1_var.equals("") && !add2_var.equals("") && !city_var.equals("") && !state_var.equals("") && !country_var.equals("") && !zipcode_var.equals("") && !phone_var.equals("") && !mobile_var.equals("") && !email_var.equals(""))
                        {
            %>
            <script type="text/javascript">
                myoriginaldata();
            </script>
            <%
                            st.executeUpdate("update groupdata set firstname='"+fn_var+"',lastname='"+ln_var+"',middlename='"+mn_var+"',address1='"+add1_var+"',address2='"+add2_var+"',city='"+city_var+"',state='"+state_var+"',country='"+country_var+"',zipcode='"+zipcode_var+"',phoneno='"+phone_var+"',mobileno='"+mobile_var+"',emailid='"+email_var+"' where group_name='"+groupname_var+"'");
 
                        }
                    }
 
                    st.close();
                    con.close();
                }
                catch(Exception e)
                {
                }
            %>
            <script type="text/javascript">
                document.getElementById("gid").value = document.getElementById("hgroupname_id").value;
            </script>
        </tbody>
    </table>
    </form>
</body>
</html>
Please help me to solve this.
Posted 18-Dec-12 19:33pm
Edited 18-Dec-12 20:01pm
(no name)134K
v2
Comments
Gian rebollido at 26-Mar-13 0:35am
   
try to debug your code.. to see the problems..:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100