title: How do i edit my table in jsp page without opening new page to edit and it should reflects in database?
body: <p>when i edit table and hitting update , after that if i press view button the contents of table set to null.. please help me out of this..</p>
  
  <p>This is my <code>index.jsp</code> page used to retrieve data from database.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@ page import="java.sql.*" %&gt; 
  &lt;%@ page import="java.io.*" %&gt; 
  &lt;%@page import="java.sql.DriverManager"%&gt;
  &lt;%@page import="java.sql.ResultSet"%&gt;
  &lt;%@page import="java.sql.Statement"%&gt;
  &lt;%@page import="java.sql.Connection"%&gt;
  &lt;%
  String id = request.getParameter("id");
  String driver = "com.mysql.jdbc.Driver";
  String connectionUrl = "jdbc:mysql://localhost:3306/";
  String database = "test";
  String userid = "root";
  String password = "root";
  try {
  Class.forName(driver);
  } catch (ClassNotFoundException e) {
  e.printStackTrace();
  }
  Connection connection = null;
  Statement statement = null;
  ResultSet resultSet = null;
  %&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;!-- &lt;h1&gt;Retrieve data from database in jsp&lt;/h1&gt; --&gt;
  &lt;table border="1"&gt;
  &lt;tr&gt;
  &lt;td&gt;id&lt;/td&gt;
  &lt;td&gt;first name&lt;/td&gt;
  &lt;td&gt;last name&lt;/td&gt;
  &lt;td&gt;City name&lt;/td&gt;
  &lt;td&gt;Email&lt;/td&gt;
  &lt;td&gt;update&lt;/td&gt;
  &lt;/tr&gt;
  &lt;%
  try{
  connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  statement=connection.createStatement();
  String sql ="select * from users";
  resultSet = statement.executeQuery(sql);
  while(resultSet.next()){
  %&gt;
  &lt;tr&gt;
  &lt;td  contenteditable&gt;&lt;%=resultSet.getString("id") %&gt;&lt;/td&gt;
  &lt;td  contenteditable&gt;&lt;%=resultSet.getString("first_name") %&gt;&lt;/td&gt;
  &lt;td  contenteditable&gt;&lt;%=resultSet.getString("last_name") %&gt;&lt;/td&gt;
  &lt;td  contenteditable&gt;&lt;%=resultSet.getString("city_name") %&gt;&lt;/td&gt;
  &lt;td  contenteditable&gt;&lt;%=resultSet.getString("email") %&gt;&lt;/td&gt;
  &lt;td&gt;&lt;a href="update-process.jsp?id=&lt;%=resultSet.getString("id")%&gt;"&gt;update&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;%
  }
  connection.close();
  } catch (Exception e) {
  e.printStackTrace();
  }
  %&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This my <code>update-process.jsp</code>, this is used to store data back to database.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ page import="java.sql.*" %&gt;
  &lt;%! String driverName = "com.mysql.jdbc.Driver";%&gt;
  &lt;%!String url = "jdbc:mysql://localhost:3306/test";%&gt;
  &lt;%!String user = "root";%&gt;
  &lt;%!String psw = "root";%&gt;
  &lt;%
  String id = request.getParameter("id");
  String first_name=request.getParameter("first_name");
  String last_name=request.getParameter("last_name");
  String city_name=request.getParameter("city_name");
  String email=request.getParameter("email");
  if(id != null)
  {
  Connection con = null;
  PreparedStatement ps = null;
  int personID = Integer.parseInt(id);
  try
  {
  Class.forName(driverName);
  con = DriverManager.getConnection(url,user,psw);
  String sql="Update users set id=?,first_name=?,last_name=?,city_name=?,email=? where id="+id;
  ps = con.prepareStatement(sql);
  ps.setString(1,id);
  ps.setString(2, first_name);
  ps.setString(3, last_name);
  ps.setString(4, city_name);
  ps.setString(5, email);
  int i = ps.executeUpdate();
  if(i &gt; 0)
  {
  out.print("Updated");
  }
  else
  {
  out.print("There is a problem in updating Record.");
  } 
  }
  catch(SQLException sql)
  {
  request.setAttribute("error", sql);
  out.println(sql);
  }
  }
  %&gt;
  
  &lt;html&gt;
  &lt;body&gt;
  &lt;form action="index.jsp"&gt;
  &lt;input type="submit" value="view"&gt;
  &lt;/form&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>