Sunday, December 26, 2021

Jsp and Servlet Update the data in database

                                                                 


step 1

Eclipse---File ----New----Dynamic Web Project----ProjectName----Finish

step 2

To Create a Jsp Page for update.jsp

Click the Project Folder-----go to src folder----main folder----web app ---right click the web app---new
----jsp---update.jsp



Update.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="update" method="post">
<input type="password" name="n1" placeholder="old password"><br><br>
<input type="password" name="n2" placeholder="new password"><br><br>
<input type="submit"><br><br>
</form>
</body>
</html>


Create a Servlet Page


goto src/main/java then right click new---sevlet---class name---update----finish


update.java



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
 * Servlet implementation class register
 */
@WebServlet("/update")
public class update extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw=response.getWriter();
try
{
String a=request.getParameter("n1");
String b=request.getParameter("n2");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con =         DriverManager.getConnection("jdbc:mysql://localhost:3306/example","root","");
            Statement st=con.createStatement();
            st.executeUpdate("update ex set password='"+b+"' where password='"+a+"' ");
            pw.println("update successfully");
}
catch(Exception e)
{
pw.println(e);
}
}

}

connect the jar files

right click the project build path----configure build path---add external jars---add mysqlconnector.jar





Note : download the mysqlconnector.jar in google

also copy the jar file paste into ----src---main---web app---WEB-INF---lib-----mysql-connector.jar 









Run

Right click the jsp page----run as ---run an server--finish










No comments:

Post a Comment

Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...