Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
JDBC Tutorial
 
Book Must Read
 
 

JDBC Update Records

 
 
Print   Email
 
     
 

To update in table, we have to use UPDATE sql command.

Update records in table

In this form, we will update records in staff_register table. First make database connection and fetch records which we have to update and display on the form.

Example of Update record in database through JSP

updateRecord.jsp form get records from database and populates on this form for further updation according to requirement.  

updateRecord.jsp

Simple Code Example

<%@ page  language="java" import="java.sql.*" errorPage="" %>
<%
        Connection conn = null;
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String jdbcURL="jdbc:mysql://localhost:3306/jsp";
        
        conn = DriverManager.getConnection(jdbcURL,"root", "");
        
        PreparedStatement psSelectRecord=null;
        ResultSet rsSelectRecord=null;
        
        String sqlSelectRecord=null;
        
        int empID=0;
        String staffName=null;
        String staffDept=null;
        int staffPhone=0; 
        
        sqlSelectRecord = "select iEmpID, sStaffName, sStaffDept, iStaffPhone from staff_register";
        psSelectRecord=conn.prepareStatement(sqlSelectRecord);
        rsSelectRecord=psSelectRecord.executeQuery();
        if(rsSelectRecord.next())
        {
            staffName=rsSelectRecord.getString("sStaffName");
            staffDept=rsSelectRecord.getString("sStaffDept");
            staffPhone=rsSelectRecord.getInt("iStaffPhone");
            empID=rsSelectRecord.getInt("iEmpID");
        }
        
%>

<html>
<head>
<title>Update record into database in JSP</title>
</head>

<body>
<form name="frm" action="saveUpdateRecord.jsp">
<input type="hidden" name="EmpID" value="<%=empID%>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="42%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3" align="center">Update  Record into Database</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Staff Name</td>
    <td><input type="text" name="sStaffName" value="<%=staffName%>"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td><tr>
    <td>Staff Phone</td>
    <td><input type="text" name="sPhone" value="<%=staffPhone%>"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value=" Submit "></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>
<%
      try{
          if(psSelectRecord!=null)
          {
           psSelectRecord.close();
          }
          
          if(rsSelectRecord!=null)
          {
           rsSelectRecord.close();
          }
          
          if(conn!=null)
          {
           conn.close();
          }
        }
        catch(Exception e)
        {
          e.printStackTrace(); 
        }
%>

If user does modification in this form and submit. This form jumps to saveUpdateRecord file to run JSP code for update in table.

saveUpdateRecord.jsp

<%@ page language="java" import="java.sql.*" errorPage="" %>
<%
        Connection conn = null;
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String jdbcURL="jdbc:mysql://localhost:3306/jsp";
        conn = DriverManager.getConnection(jdbcURL,"root", "");
        
        PreparedStatement psUpdateRecord=null;
        String sqlUpdateRecord=null;
        
        int empID=Integer.parseInt(request.getParameter("EmpID"));
        String staffName=request.getParameter("sStaffName");
        int staffPhone=Integer.parseInt(request.getParameter("sPhone")); 
        //// if this come blank and aphla number it will throw
        /// parse int NumberFormatException exception, only number
        
        try
        {
            sqlUpdateRecord="update staff_register set sStaffName=?, iStaffPhone=? where iEmpID=?";
            psUpdateRecord=conn.prepareStatement(sqlUpdateRecord);
            psUpdateRecord.setString(1,staffName);
            psUpdateRecord.setInt(2,staffPhone);
            psUpdateRecord.setInt(3,empID);
            
            psUpdateRecord.executeUpdate();
        }
        catch(Exception e)
        {
          response.sendRedirect("updateRecord.jsp");
           //// On error it will send back to addRecord.jsp page
        }
        
        try{
          if(psUpdateRecord!=null)
          {
           psUpdateRecord.close();
          }
          
          if(conn!=null)
          {
           conn.close();
          }
        }
        catch(Exception e)
        {
          e.printStackTrace(); 
        }
%>
<html>
<body>
  Data is updated successfully.
</body>
</html>

 

 

 

     
 
If You Like this Article, share this

 
 
     
   
 
     
     
     
 
  Privacy Policy