Update.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="gray">
<form name="f1" method="Post" action="UpdateView.jsp">
<div id="container">
<div class="content"><center>Edit Student</center><br>
<div class="formElements,rightcol" align="center">
<label>Id :
<span><input type="text" id="Id" name="Id" ></span></label>
</div><br>
<div class="buttons" align="center">
<button type="submit" class="primaryAction">Edit</button>
<button type="reset" class="primaryAction">Clear</button>
</div>
</div>
</div>
</form>
</body>
</html>
UpdateView.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<form name="f1" class="anu" method="Post" action="UpdateAction">
<%
// String Name= request.getParameter("Name");
//String Course = request.getParameter("Course");
// int Age=Integer.parseInt(request.getParameter("Age"));
// String Address= request.getParameter("Address");
// String Email = request.getParameter("Email");
String Name="";
String Course="";
int Age=0;
String Address="";
String Email="";
int Id=Integer.parseInt(request.getParameter("Id"));
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/education","root","admin");
String selectStoredProc = "SELECT * FROM education WHERE Id = '"+Id+"'";
PreparedStatement ps = con.prepareStatement(selectStoredProc);
ResultSet rs=ps.executeQuery();
while(rs.next()) {
Id = rs.getInt(1);
Name=rs.getString(2);
Course=rs.getString(3);
Age=rs.getInt(4);
Address=rs.getString(5);
Email=rs.getString(6);
}
rs.close();
rs = null;
}
catch (Exception e) {
System.out.println(e.getLocalizedMessage());
e.printStackTrace();
}
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>Course :
<span><input type="text" name="Course" value="<%=Course%>"></span></label><br>
<label>Age :
<span><input type="text" name="Age" value="<%=Age%>"></span></label><br>
<label>Address :
<span><input type="text" name="Address" value="<%=Address%>"></span></label><br>
<label>Email :
<span><input type="text" name="Email" value="<%=Email%>"></span></label><br>
<div class="buttons">
<button type="submit" class="primaryAction">Edit</button>
<button type="reset" class="primaryAction">Clear</button>
</div>
</form>
</body>
UpdateAction.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
public class UpdateAction extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int Id=Integer.parseInt(request.getParameter("Id"));
String Name= request.getParameter("Name");
String Course = request.getParameter("Course");
int Age=Integer.parseInt(request.getParameter("Age"));
String Address= request.getParameter("Address");
String Email = request.getParameter("Email");
Connection con = null;
System.out.println("--"+Id+"--"+Name+"--"+Course+"--"+Age+"--"+Address+"--"+Email);
try
{
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost/education","root","admin");
// String url = "jdbc:mysql://localhost/education";
// String user = "root";
// String password = "admin";
//connection = DriverManager.getConnection("jdbc:mysql://localhost/education", "root", "admin");
//connection = DriverManager.getConnection(url,user,password);
//String sql="UPDATE education SET Id=?,Name=?,Course=?,Age=?,Address=?,Email=? where Id=?";
//PreparedStatement cstmt= con.prepareStatement(sql);
String sql="UPDATE education SET Id='"+Id+"', Name='"+Name+"', Course='"+Course+"', Age='"+Age+"', Address='"+Address+"', Email='"+Email+"' where Id="+Id;
System.out.println(sql);
PreparedStatement cstmt= con.prepareStatement(sql);
// cstmt.setInt(1,Id);
// cstmt.setString(2,Name);
// cstmt.setString(3,Course);
// cstmt.setInt(4,Age);
// cstmt.setString(5,Address);
// cstmt.setString(6,Email);
cstmt.executeUpdate();
System.out.println("Student updated successfully");
}
catch(Exception e)
{
System.out.println("-------"+e);
e.printStackTrace();
}
}
}
No comments:
Post a Comment