[an error occurred while processing this directive]

Update Database Table using JDBC in JSP


This example shows how to update the existing  record of mysql table using jdbc connectivity in the jsp page. In this example we have created two jsp pages update.jsp and updatingDatabase.jsp.  In the update.jsp page, we are using a Text box where user can give his/her name and submit the page. After submitting the page,  updatingDatabase.jsp will be called and the sql query ("update servlet set name = ? where id = ?") is executed which will modify the table record.




<%@page language="java" session="true" 
"text/html;charset=ISO-8859-1"  %> 
<font color="blue">Please Enter Your Name </font><br><br>
<form name="frm" method="post" action="updatingDatabase.jsp">
<table border = "0">
  <tr align="left" valign="top">
    <td><input type="text" name ="name" /></td>
  <tr align="left" valign="top">
    <td><input type="submit" name="submit" value="submit"/></td>

 Type the url: http://localhost:8080/ServletExample/jsp/update.jsp on your browser. Following output will be displayed:


<%page language = "java" contentType = 
          "text/html; charset = ISO-8859-1"

  import = "java.io.*"
  import = "java.sql.*"
  import = "java.util.*"
  import = "javax.sql.*"
  import "java.sql.ResultSet"
  import "java.sql.Statement"
  import "java.sql.Connection"
  import "java.sql.DriverManager"
  import "java.sql.SQLException"
  Connection con = null;  
  PreparedStatement ps = null;
  ResultSet rs = null;
  Statement stmt = null;
  String name = request.getParameter("name");
  Integer id = 5;
  <title>Updating Database</title>
  try {
    con =DriverManager.getConnection 
    ps = con.prepareStatement("update servlet set 
    name = ? where id = ?"
    ps.setInt(2, id);
    ps.setString(1, name);
      Database successfully Updated!<br>
      rs = stmt.executeQuery("SELECT * FROM servlet");

  catch (IOException e) {
    throw new IOException("Can not display records.", e);
  catch (ClassNotFoundException e) {
    throw new SQLException("JDBC Driver not found.", e);
  finally {
    try {
      if(stmt != null){
        stmt = null;
      if(ps != null) {
        ps = null;
      if(con != null) {
        con = null;
    catch (SQLException e) {}

After submitting the name, it will be updated in the database and the records will be displayed on your browser.

Download Source Code


[an error occurred while processing this directive]