Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
68 views

Java Exam Answer Sheet

The document contains code for an employee information web application. It includes: 1) An HTML form to collect employee data like name, address, age, and department. 2) A Java servlet (EmployeeData.java) that inserts the submitted form data into a MySQL database. 3) Another Java servlet (Process.java) that retrieves and displays the stored employee records from the database in an HTML table. The application allows users to enter employee data via a web form, stores it in a database, and displays the stored records in a table.

Uploaded by

Raghav Adhikari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Java Exam Answer Sheet

The document contains code for an employee information web application. It includes: 1) An HTML form to collect employee data like name, address, age, and department. 2) A Java servlet (EmployeeData.java) that inserts the submitted form data into a MySQL database. 3) Another Java servlet (Process.java) that retrieves and displays the stored employee records from the database in an HTML table. The application allows users to enter employee data via a web form, stores it in a database, and displays the stored records in a table.

Uploaded by

Raghav Adhikari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Q. No.

1
<html> <!-- index.html file -->
<head>
<title>Employee Information</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Employee Information</h1>
<form action="EmployeeData" method="post">
<p>Employee Name:</p>
<input type="text" name="nameField">
<p>Address:</p>
<input type="text" name="addressField">
<p>Age:</p>
<input type="text" name="ageField">
<p>Department:</p>
<select name="departmentFiels">
<option value="Information Technology">Information Technology</option>
<option value="Information Technology">Medical</option>
<option value="Information Technology">Sales</option>
</select><br><br>
<input type="submit" name="submitbtn" value="Save">
<input type="button" name="clearbtn" value="Clear">
</form>
<a href="Process"><p>View Employee Data</p></a>
</body>
</html>
import java.sql.*; //EmployeeData.java file
import java.util.*;
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;

public class EmployeeData extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException{

String name= request.getParameter("nameField");


String address= request.getParameter("addressField");
String age= request.getParameter("ageField");
String department= request.getParameter("departmentField");

try{
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_db","root","");
String sql= "insert into emp_details(Name,Address,Age,Department) values(?,?,?,?)";
PreparedStatement ps= con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, address);
ps.setString(3, age);
ps.setString(4, department);
ps.execute();
}catch(Exception e){

}
}
}
import java.sql.*; //Process.java file
import java.util.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class Process extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_db","root","");
String sql= "select * from emp_details";
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Employee Data</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Employee Information</h1>");
out.println("<table>");
while(rs.next()){
out.println("<tr");
out.println("<td>"+rs.getString(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td>"+rs.getString(4)+"</td>");
out.println("</tr");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e){ }
}
}
Q. No. 2(b)
package endexam2b;
import javax.swing.*;

public class EndExam2b{

public static void main(String[] args) {


JFrame f= new JFrame();
f.setTitle("Login");

JLabel l1= new JLabel("Please Login to Continue");


l1.setBounds(150,50,200,20);

JLabel l2= new JLabel("Username: ");


l2.setBounds(100,100,100,20);
JTextField tf1= new JTextField();
tf1.setBounds(200,100,200,20);

JLabel l3= new JLabel("Password: ");


l3.setBounds(100,150,100,20);
JTextField tf2= new JTextField();
tf2.setBounds(200,150,200,20);

JButton b1= new JButton("Login");


b1.setBounds(200,200,80,20);
JButton b2= new JButton("Clear");
b2.setBounds(300,200,80,20);

f.add(l1);
f.add(l2);
f.add(l3);
f.add(tf1);
f.add(tf2);
f.add(b1);
f.add(b2);

f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Q. No. 3
package javaexam3; // Insert Record
import java.sql.*;
import java.util.*;

public class JavaExam3 {

public static void main(String[] args) throws Exception{


System.out.println("Enter the id");
Scanner sc= new Scanner(System.in);
int id= sc.nextInt();
System.out.println("Enter the Name");
Scanner sc1= new Scanner(System.in);
String name= sc.next();
System.out.println("Enter the Address");
Scanner sc2= new Scanner(System.in);
String address= sc.next();
System.out.println("Enter the Gender");
Scanner sc3= new Scanner(System.in);
String gender= sc.next();
System.out.println("Enter the Course");
Scanner sc4= new Scanner(System.in);
String course= sc.next();

Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/boston","root","");
String sql= "insert into student values(?,?,?,?,?)";

PreparedStatement ps= con.prepareStatement(sql);


ps.setInt(1,id);
ps.setString(2,name);
ps.setString(3,address);
ps.setString(4,gender);
ps.setString(5,course);
ps.execute();
con.close();
}
}
package javaexam3; // Update Address

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class JavaExam3UpdateAddress {

public static void main(String[] args) throws Exception{

Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/boston","root","");
String sql="update student set s_address='Chitwan' where s_address='London'";

PreparedStatement ps= con.prepareStatement(sql);


ps.executeUpdate();
con.close();
}
}

package javaexam3; //Update Course

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class JavaExam3UpdateCourse {

public static void main(String[] args) throws Exception{

Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/boston","root","");
String sql="update student set s_course='BCIS' where s_course='BBA'";

PreparedStatement ps= con.prepareStatement(sql);


ps.executeUpdate();

con.close();
}
}
Q. No. 4
<!DOCTYPE html> <!-- index.html file -->

<html>

<head>

<title>Login</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<h1>Login to Continue</h1>

<form action="Login" method="post">

<span>Username: </span>

<input type="text" name="usernameField">

<span>Password: </span>

<input type="password" name="passwordField">

<br><br>

<input type="submit" name="submitbtn" value="Login">

<input type="button" name="clearbtn" value="Clear">

</form>

</body>

</html>
import java.io.*; //Login.java file
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;

public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

PrintWriter out = response.getWriter();


String username= request.getParameter("usernameField");
String password= request.getParameter("passwordField");

try {
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/WebApplication","root","");
String sql= "select * from login_data where username=? && password=?";
PreparedStatement ps= con.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs= ps.executeQuery();

if(rs.next()){
HttpSession session=request.getSession();
session.setAttribute("user",username);

RequestDispatcher rd = request.getRequestDispatcher("Status");
rd.forward(request, response);
} else{
out.println("Username or Password incorrect");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.include(request, response);
}
}
catch(Exception e){ }
}
}
import java.sql.*; //Status.java file
import java.util.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class Status extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
PrintWriter out = response.getWriter();

HttpSession session=request.getSession(false);
if(session!=null){

try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Status</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Post Status</h1>");
out.println("<form action='Status' method='post'>");
out.println("<input type='text' name='statusField'>");
out.println("<input type='submit' name='postbtn' value='Post'>");

Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/WebApplication","root","");
String sql= "select * from status_data";
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs= ps.executeQuery();

out.println("<h1>Ststus</h1>");
while(rs.next()){
out.println("<p>"+rs.getString(1)+"</p>");
}
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e){ }
}
else{
out.print("Please login first");
request.getRequestDispatcher("index.html").include(request, response);
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

String status= request.getParameter("statusField");

try {
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/WebApplication","root","");
String sql= "insert into status_data(Status) values(?)";
PreparedStatement ps= con.prepareStatement(sql);
ps.setString(1, status);
ps.execute();
}
catch(Exception e){ }
}
}

You might also like