0

Hi

I am doing a project where i need to edit values in textboxes from database using jsp, here is my code, Please can anyone help me to solve this:

retrieve.jsp

<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");%>
<HTML>
    <HEAD>
        <TITLE>Fetching Data From a Database</TITLE>
</HEAD>
    <BODY>
        <H1>Fetching Data From a Database</H1>
        <%
                    Connection connection = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver "
                            + "(*.mdb, *.accdb)};DBQ=C:\\Database\\Databaseip.accdb");
                    ResultSet resultset;
                    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    String AggregatedSwitch = request.getParameter("AggregatedSwitch");
                    resultset =
                            statement.executeQuery("SELECT * FROM ip_address WHERE AggregatedSwitch = '" + AggregatedSwitch + "'");
                    if (!resultset.next()) {
                        out.println("Sorry, could not find the data. ");
                    } else {
        %>
        <TABLE cellpadding="15" border="1" style="background-color: #ffffcc" align="center">
            <TR>
                <TH>IP Address</TH>
                <TH>Subnet Mask</TH>
                <TH>Aggregated Switch</TH>
                <TH>Allocated Hostname</TH>
                <TH>VLAN ID</TH>
                <TH>Headend Network</TH>
                <TH>Date Issued</TH>
                <TH>Remarks</TH>
                <TH>Edit</TH>
                <TH>Delete</TH>

            </TR>
            <%


                                        do {
            %>


            <TR>
                <TD> <%=  resultset.getString(1)%> </TD>
                <TD> <%= resultset.getString(2)%> </TD>
                <TD> <%= resultset.getString(3)%> </TD>
                <TD> <%= resultset.getString(4)%> </TD>
                <TD> <%= resultset.getString(5)%> </TD>
                <TD> <%= resultset.getString(6)%> </TD>
                <TD> <%= resultset.getString(7)%> </TD>
                <TD> <%= resultset.getString(8)%> </TD>

                <TD><input type="button" name="edit" value="Edit" onClick="javascript:window.location='editData.jsp';" style="background-color:#49743D;font-weight:bold;color:#ffffff;">

                </TD>
                <TD><input type="button" name="delete" value="Delete" onClick="javascript:window.location='deleteData.jsp';"style="background-color:#ff0000;font-weight:bold;color:#ffffff;"></TD>
            </TR>
            <%resultset.next();
                                        } while (resultset.isAfterLast() != true);

            %>

        </TABLE>
        <BR>
        <%
                    }
        %>
    </BODY>
</HTML>

editData.jsp

<%@ page language="java" import="java.util.*;"%>
<%@ page language="java" import="java.sql.*"%>
<head>
</head>
<body>
    <form name="editform" method="post" action="editData.jsp">
        <%!            String IPAddress = "";
            String SubnetMask = "";
            String AggregatedSwitch = "";
            String AllocatedHostname = "";
            String VLANID = "";
            String HeadendName = "";
            String DateIssued = "";
            String Remarks = "";

        %>

        <br><br><br>
        <table align="center" width="300px" style="background-color:#EDF6EA;border:1px solid #000000;">

            <tr><td colspan=2 style="font-weight:bold;" align="center">Edit User</td></tr>
            <tr><td colspan=2 align="center" height="10px"></td></tr>
            <tr>
                <td>IP Address</td>
                <td><input type="text" name="IPAddress" value="<%=IPAddress%>"></td>
            </tr>
            <tr>
                <td>Subnet Mask</td>
                <td><input type="text" name="SubnetMask" value="<%=SubnetMask%>"></td>
            </tr>
            <tr>
                <td>Aggregated Switch</td>
                <td><input type="text" name="AggregatedSwitch" value="<%=AggregatedSwitch%>"></td>
            </tr>
            <tr>
                <td>Allocated Hostname</td>
                <td><input type="text" name="AllocatedHostname" value="<%=AllocatedHostname%>"></td>
            </tr>

            <tr>
                <td>VLAN ID</td>
                <td><input type="text"  name="VLANID" value="<%=VLANID%>"></td>
            </tr>
            <tr>
                <td>Headend Name</td>
                <td><input type="text" name="HeadendName" value="<%=HeadendName%>"></td>
            </tr>
            <tr>
                <td>Date Issued</td>
                <td><input type="text" name="DateIssued" value="<%=DateIssued%>"></td>
            </tr>
            <tr>
                <td>Remarks</td>
                <td><input type="text" name="Remarks" value="<%=Remarks%>"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
            </tr>
            <tr><td colspan=2 align="center" height="10px"></td></tr>
        </table>
        <%
                    IPAddress = request.getParameter("IPAddress");
                    SubnetMask = request.getParameter("SubnetMask");
                    AggregatedSwitch = request.getParameter("AggregatedSwitch");
                    AllocatedHostname = request.getParameter("AllocatedHostname");
                    VLANID = request.getParameter("VLANID");
                    HeadendName = request.getParameter("HeadendName");
                    DateIssued = request.getParameter("DateIssued");
                    Remarks = request.getParameter("Remarks");
                    Statement stmt;
                    Connection conn = null;
                    String url = "jdbc:odbc:Driver={Microsoft Access Driver "
                            + "(*.mdb, *.accdb)};DBQ=C:\\Database\\Databaseip.accdb";
                    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
                    out.println(request.getRequestURI());
                    if (IPAddress != null && SubnetMask != null && AggregatedSwitch != null && AllocatedHostname != null && VLANID != null && HeadendName != null && DateIssued != null && Remarks != null) {
                        if (IPAddress != "" && SubnetMask != "" && AggregatedSwitch != "" && AllocatedHostname != "" && VLANID != "" && HeadendName != "" && DateIssued != "" && Remarks != "") {
                            try {
                                Class.forName(driver).newInstance();
                                conn = DriverManager.getConnection(url);
                                System.out.println("Connected to the database");

                                //ArrayList al = null;
                                // ArrayList userList = new ArrayList();
                                String query = "UPDATE ip_address SET IPAddress= "+IPAddress+",SubnetMask=" + SubnetMask + ",AggregatedSwitch=" + AggregatedSwitch + ",AllocatedHostname=" + AllocatedHostname + ",VLANID=" + VLANID + ",HeadendName=" + HeadendName + ",DateIssued=" + DateIssued + ",Remarks=" + Remarks + "";
                        stmt = conn.createStatement();

                        int i = stmt.executeUpdate(query);
                        System.out.println("query" + query);
                                if (i > 0) {
                                    response.sendRedirect("adminHome.jsp");
                                }
                                conn.close();
                                System.out.println("Disconnected from database");
        %>
        <br>
        <TABLE style="background-color: #E3E4FA;"
               WIDTH="30%" border="1">
            <tr><th>Data Modified successfully
                    in database.</th></tr>
        </TABLE>
        <%

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

        %>
    </form>
</body>
</html>

with this code my ouput is it displays all the table values in a table fashion as per the form based input, at the end of each row it has edit and delete buttons, when i click edit button it shd edit all the values in that particular row in next jsp page, but am getting simply empty textboxes not the values, Please correct my code and solve this issue.. This is very urgent for me....

Thanks in advance
Lissy.

4
Contributors
4
Replies
20
Views
6 Years
Discussion Span
Last Post by stultuske
0

In editData.jsp the textboxes appears before calling request.getParameter() for each variable. For that reason every textbox is empty. Try to move the block of get parameters above, next to variable declarations.

0

selvampti .. is there any particular reason you chose to revive this thread?
no new answer, no new question ....
I'm puzzled as to your thoughts here ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.