In: Computer Science
As an enterprise system developer, you need to develop
the registration page of the employee management system. The
administrator of the system registers a new employee using the GUI
developed in JSP. The required table ‘Employee’ is provided in
mysql database with columns as employee id, name, address, city,
state and zip code as shown in figure.
Write a GUI snippet/JSP code with GUI components to
take the user’s input. Separately, write the servlet code to insert
the input records from the JSP page into the ‘employee’ table using
JDBC (Java Database Connectivity).
Note: You may assume the username and password for the database
connectivity.
1]
Firstly for insert data in MySQL using JSP first we have to create a table in data base.
CREATE TABLE Employee(e_id int,e_name text,e_address text,e_city
text,e_state text,e_zipcode int);
insert into Employee values(1,"Poonam","Manik nagar","pune","MG
Road",412307);
select * from Employee;
2]
Code to create Employee registeration form and jsp code to insert values into Employee tble using JDBC
</html>
<html>
<head>
<title>Registeration Form</title>
</head>
<body>
<form action="index.jsp">
Employee Id:<input type="text" name="eid" value="Id..."
onclick="this.value=''"/><br/>
Employee Name:<input type="text" name="ename" value="Name..."
onclick="this.value=''"/><br/>
Employee Address:<input type="text" name="eaddress"
value="Address..." onclick="this.value=''"/><br/>
Employee City<input type="text" name="ecity" value="City..."
onclick="this.value=''"/><br/>
Employee State<input type="text" name="estate" value="State..."
onclick="this.value=''"/><br/>
Employee Pin Code:<input type="text" name="epincode" value="Pin
Code..." onclick="this.value="''"/><br/>
<input type="submit" value="Register"/>
// Jsp code to insert values in Employee table using JDBC
connectivity
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*"%> //import java.sql
and java.util package
<%
String e_id=request.getParameter("eid"); //access and stored user
input values of form in e_id parameter
String e_name=request.getParameter("ename"); //access and stored
user input values of form in e_name parameter
String e_address=request.getParameter("eaddress"); //access and
stored user input values of form in e_address parameter
String e_city=request.getParameter("ecity"); //access and stored
user input values of form in e_cityt parameter
String e_state=request.getParameter("estate"); //access and stored
user input values of form in e_state parameter
String e_pincode=request.getParameter("epincode"); //access and
stored user input values of form in e_pincode parameter
int employee_id = Integer.parseInt(e_id); //convert e_id parameter
from string to integer and stored that value into employee_id
int employee_pincode = Integer.parseInt(e_pincode); //convert
e_pincode parameter from string to integer and stored that value
into employee_pincode
try
{
Class.forName("com.mysql.jdbc.Driver"); // code for database
connection in mysql
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "");
Statement st=conn.createStatement(); // Creating object of
Statement class
// call to method ececuteUpdate to insert values in Employee
table
int i=st.executeUpdate("insert into
Employee(employee_id,e_name,e_address,e_city,e_state,employee_pincode)values('"+employee_id+"','"+e_name+"','"+e_address+"','"+e_city+"','"+e_state+"','"+employee_pincode+"')");
out.println("Data is successfully inserted!");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>
</form>
</body>
</html>
If my answer is helpful for you,then upvote for my answer.