This is a form for Updating Record using Jsp technology with mysql DataBase.Here,mainly first I want to search the record and later I want to update a record into DatBase.

Procedure:

Existing Table:


Step 1:  develop a jsp file for searching the record fisrst search.jsp
              action attribute is forwarded to edit_studentdetails.jsp when you click on submit button

    <form name="f1" method="post"  action="edit_studentdetails.jsp">  
  
    <div id="container">
    <div  style="float:right;width:10%;border:0;font-color:#163362">
<a href=logout.jsp">Log Out</a></div><br>

        <div class="content"><center>Edit Student</center><br>

            <div class="formElements,rightcol" align="center">
                <label>Rollno:
                 <span><input type="text" id="id" name="id" ></span></label>
                       </div><br>
            <div class="buttons">
                <button type="submit" class="primaryAction">Edit</button>
                 <button type="reset" class="primaryAction">Clear</button>
            </div>
             </div>
         </div>
       </form>

Step2: Develop a form for edit_studentdetails.jsp and for getting a record like..


<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.PreparedStatement"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Connection"%>
<form name="f1" class="anu" method="post" action="update_studentdetails.jsp">  
    
    <%  
     String name="";
        String sal="";
         String id=request.getParameter("id");
try 
                {

                Class.forName("com.mysql.jdbc.Driver");
     Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
String selectStoredProc = "SELECT * FROM empTable WHERE empId = '"+id+"'";

                        PreparedStatement ps = con.prepareStatement(selectStoredProc);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()) {
                      id = rs.getString(1);

                      name=rs.getString(2);
                      sal=rs.getString(3);
                    }
                    rs.close();
                    rs = null;
                } 
                catch (Exception e) {
                    System.out.println(e.getLocalizedMessage());
                }
                finally {     
                }

    %>

                                 ID:
          <input type="text"  name="id" value="<%=id%>"><br><br>
<label>Name
              <span><input type="text"  name="name" value="<%=name%>"></span></label><br>
                <label>Sal:
                 <span><input type="text"  name="sal" value="<%=sal%>"></span></label><br>
            <div class="buttons">
                <button type="submit" class="primaryAction">Edit</button>
                <button type="reset" class="primaryAction">Clear</button>
            </div> 
</form>
  </body>

step 3: Develop a update_studentdetails.jsp for upadting record into data base

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.CallableStatement"%>

<%
        String id1=request.getParameter("id");
        int id=Integer.parseInt(id1);
        String name=request.getParameter("name");
        String sal1=request.getParameter("sal");
        int sal=Integer.parseInt(sal1);
      Connection con=null;

 try {
                Class.forName("com.mysql.jdbc.Driver");

                con= DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
     
    PreparedStatement cstmt= con.prepareStatement("update empTable set empName=?,empSalary=? where empId=?");
             
 cstmt.setString(1,name);
 cstmt.setInt(2,sal);

 cstmt.setInt(3,id);
       
                cstmt.executeUpdate(); 

               out.println("Student updated successfully");
       }
       catch(Exception e){
        out.print("try not allowed"+e);
                }


%>

OutPut:












Download for war file with jar Files Click Here




  


6 comments: