5a. Html page with fields, eno, name, age, desg, salary. Now on submit this data to a JSP page which will update the employee table of database with matching eno.
MySQL Query
create
table emp(empid varchar(10) PRIMARY KEY, ename varchar(50), salary
varchar(50),age varchar(50) )
insert into emp
values('1','aaa','221234','11')
insert into emp
values('2','bbb','334567','22')
insert into emp
values('3','ccc','44454','33')
insert into emp
values('4','ddd','55123','44')
---------------------
index.html -------------------------------
<html>
<body>
<form action="UpdateEmp.jsp"
>
Enter
Employee Number<input type="text" name="txtEno"
><br> Enter Name<input type="text"
name="txtName" ><br>
Enter
age<input type="text" name="txtAge" ><br> Enter
Salary<input type="text" name="txtSal" ><br>
<input
type="reset" ><input type="submit">
</form>
</body>
</html>
-------------- UpdateEmp.java
--------------
<%@page contentType="text/html" import="java.sql.*"%>
<%@page contentType="text/html" import="java.sql.*"%>
<html>
<body>
<h1>Employee
Record Update</h1>
<%
String
eno=request.getParameter("txtEno");
String
name=request.getParameter("txtName");
String
age = request.getParameter("txtAge");
String
sal = request.getParameter("txtSal");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/empdb");
PreparedStatement stmt = con.prepareStatement("select * from emp where
empid=?");
stmt.setString(1, eno);
ResultSet
rs = stmt.executeQuery();
if(rs.next()){
out.println("<h1>~~~
Employee "+name+" Exist ~~~ </h1>");
PreparedStatement
pst1= con.prepareStatement("update emp set salary=? where empid=?");
PreparedStatement
pst2= con.prepareStatement("update emp set age=? where empid=?");
pst1.setString(1,
sal);
pst1.setString(2,
eno);
pst2.setString(1,
age);
pst2.setString(2,
eno);
pst1.executeUpdate();
pst2.executeUpdate();
}
else{
out.println("<h1>Employee
Record not exist !!!!!</h1>");
}
}
catch(Exception
e)
{
out.println(e);
}
%>
</body>
</html>
Comments
Post a Comment