Wednesday, July 11, 2018

Evaluation Number Two

Note: Before using this code.
Please do following =>>
1. create a database, name of database should be 'company'
2. create a table inside this database, name of table should be employee
3. inside this table create 6 columns [id, name, desig, salary, tech]
note: id must be primary key
4. now insert few records inside the employee table

note: please create a dynamic-web-project in eclipse ide and create three (ie. 3)  jsp files inside WebContent folder, the names of jsp must be following
1. index.jsp
2. edit.jsp
3. update-record.jsp  

note: also copy and paste mysqlconnector.jar file inside the lib folder (lib folder is present inside WEB-INF folder)



code for index.jsp =>>

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>home-page</title>
</head>
<body>
<center>
<form action="edit.jsp" method="get">
<b><i>Input employee id</i></b><br>
<input type="text" name="emp_id" required><br>
<input type="submit" value="get employee data">
</form>
</center>
</body>
</html>

code for edit.jsp =>>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>edit</title>
</head>
<body>
<center>
<%
// fetch the data of html form 
// here we are fetching the value of emp_id given in index.jsp
String id = request.getParameter("emp_id");

// load driver class
Class.forName("com.mysql.jdbc.Driver");

// get object of connection 
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/company","root","root");

// get object of statement
Statement st = co.createStatement();

// create query to be executed
String sql = "select * from employee where id='"+id+"'";

// execute the query and get object of result-set
ResultSet rs = st.executeQuery(sql);

// check if there is a record inside the result-set
if(rs.next()) // fetch record from result-set 
{
// fetch the data of all the columns of result-set
String name = rs.getString("name");
String desg = rs.getString("desig");
float salary = rs.getFloat("salary");
String tech = rs.getString("tech");

out.println("<form action='update-record.jsp' method='post'>");

out.println("Id<br>");
out.println("<input type='text' name='id' value="+id+" readonly><br><br>");

out.println("Name<br>");
out.println("<input type='text' name='name' value="+name+" required><br><br>");

out.println("Designation<br>");
out.println("<input type='text' name='desig' value="+desg+" required><br><br>");

out.println("Salary<br>");
out.println("<input type='text' name='salary' value="+salary+" required><br><br>");

out.println("Technology<br>");
out.println("<input type='text' name='tech' value="+tech+" required><br><br>");

out.println("<input type='submit' value='update-this-record'>");
out.println("</form>");
}
else // no record found inside result-set, since id is not valid
{
out.println("<h3 style='color : red'>employee id not found in our records</h3>");
out.println("<a href='index.jsp'>please try again</a>");
}
%>
</center>
</body>
</html>



code for update-record.jsp =>>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>update-record</title>
</head>
<body>
<center>
<%
// fetch html form data which was submitted from the edit.jsp
String id = request.getParameter("id");
String name = request.getParameter("name");
String desig = request.getParameter("desig");
String salary = request.getParameter("salary");
String tech = request.getParameter("tech");

// load driver class
Class.forName("com.mysql.jdbc.Driver");

// get object of connection 
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/company","root","root");

// get object of statement
Statement st = co.createStatement();
String sql = "update employee set name='"+name+"',desig='"
+desig+"',salary='"+salary+"',tech='"+tech+"' where id='"+id+"'";

// execute sql statement and get how many records were updated
int rec = st.executeUpdate(sql);

if(rec > 0) // if updated records are more than zero
{
// show the data at client side
out.println("<p style='color : blue'><i>your data has been updated</i><p>");
out.println("<a href='index.jsp'>goto home page</a>");
}
%>
</center>
</body>

</html>






5 comments:

  1. Explained in a very easy way.
    And i would prefer to lean java feom this blog

    ReplyDelete
  2. I have done it bhaiya

    ReplyDelete
  3. Awesome superb....love you sir...

    ReplyDelete
  4. Please explain where to use Statement and PreparedStatement.

    ReplyDelete