Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Sunday, 22 April 2012

Develop a program to perform the database driven operation like insert, Delete, Update and select. To perform the above operations create one table named Employee. Field Name Field Type EmpId Integer Empname Varchar Emp_desig Varchar Emp_J_Date Varchar Emp_Salary Numeric


[pro28index.jsp]

<html>
<head>
<script language="javascript">
function valid_num()
{
e=event.keyCode;
//alert(e);
if( !(e>=48 && e<=57))
{
event.keyCode=0;
}
}
</script>
</head>
<body>
<table align=center cellpadding=10>
<tr>
<td><img src="logo.jpg"/>
</table>

<table align=center width=100% cellpadding=5>
<tr>
<td colspan=2 align="center"><h2><i>DATABASE DRIVEN OPERATIONS</i></h2>
<tr>
<td align="right" colspan=2><a href="http://master-gtu.blogspot.com">www.master-gtu.blogspot.com</a>
<tr>
<td valign=top>
<form name="f1" action="pro28">
<table>
<tr>
<td>Employee ID
<td><input type="text" name="empid" name="search" onkeypress="valid_num()">
<td><input type="submit" value="search" onclick="document.f1.flag.value=1;document.f1.fn.value='empid';document.f1.action='pro28index.jsp'">
<tr>
<td>Employee NAME
<td><input type="text" name="empname">
<td><input type="submit" value="search" onclick="document.f1.flag.value=2;document.f1.fn.value='empname';document.f1.action='pro28index.jsp'">
<tr>
<td>Employee DISIGNATION
<td><input type="text" name="emp_desig">
<td><input type="submit" value="search" onclick="document.f1.flag.value=3;document.f1.fn.value='emp_desig';document.f1.action='pro28index.jsp'">
<tr>
<td>Employee JOIN DATE
<td><input type="text" name="emp_j_date">
<tr>
<td>Employee SALARY
<td><input type="text" name="emp_salary" onkeypress="valid_num()">
<tr>
<td colspan=3>
<input type="submit" name="insert" value="INSERT" onclick="document.f1.flag.value=5">
<input type="submit" name="DELETE" value="DELETE" onclick="document.f1.flag.value=6">
<input type="submit" name="update" value="UPDATE" onclick="document.f1.flag.value=7">
<input type="submit" name="view" value="VIEW ALL">
</table>
<input type="hidden" name="flag" value="">
<input type="hidden" name="fn" value="">
</form>
<%
String msg=request.getParameter("msg");
if(msg!=null)
out.println("<font color=red>"+msg+"</font>");
%>
<td>
<jsp:include page="/pro28showtable.jsp"/>
</table>
</body>
</html>

[pro28showtable.jsp]


<%@ page import="java.sql.*" %>
<table border=5><tr><td>
<table cellpadding=10>
<tr bgcolor="limegreen">
<th>Empid<th>Empname<th>emp_desig<th>emp_j_date<th>emp_salary
<%
String q;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","pankaj");
Statement stmt=con.createStatement();
String fn=request.getParameter("fn");
String value=request.getParameter(fn);


if(fn=="" || fn==null)
q="select * from employee";
else
q="select * from employee where "+fn+"='"+value+"'";

ResultSet rs=stmt.executeQuery(q);

while(rs.next())
{
%>
<tr>
<td><%= rs.getInt(1) %>
<td><%= rs.getString(2) %>
<td><%= rs.getString(3) %>
<td><%= rs.getString(4) %>
<td><%= rs.getString(5) %>
<%
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){ out.println("error found:"+e);}
%>
</table></table>

[pro28.java]


import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet("/pro28")
public class pro28 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String q;
String msg="";
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","pankaj");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee");
String empid=req.getParameter("empid");
String empname=req.getParameter("empname");
String emp_desig=req.getParameter("emp_desig");
String emp_j_date=req.getParameter("emp_j_date");
String emp_salary=req.getParameter("emp_salary");

int flag=Integer.parseInt(req.getParameter("flag"));
if(empid.equals(""))
{
msg="Enter EmpID first";
flag=1;
}
switch(flag)
{
case 4:
out.println("4 selected");
break;
case 5:
rs=stmt.executeQuery("select * from employee where empid='"+empid+"'");
if(!rs.next())
{
q="insert into employee values('"+empid+"','"+empname+"','"+emp_desig+"','"+emp_j_date+"','"+emp_salary+"')";
stmt.executeQuery(q);
msg="Insert Record Successfully...";
}
else
{
msg="Employee Id Already Exist...";
}
break;
case 6:
rs=stmt.executeQuery("select * from employee where empid='"+empid+"'");
if(rs.next())
{
q="delete from employee where empid='"+empid+"'";
stmt.executeQuery(q);
msg="Delete Record Successfully...";
}
else
{
msg="Employee Not Exist...";
}
break;
case 7:

rs=stmt.executeQuery("select * from employee where empid='"+empid+"'");
if(rs.next())
{
q="update employee set empname='"+empname+"', emp_desig='"+emp_desig+"', emp_j_date='"+emp_j_date+"', emp_salary='"+emp_salary+"' where empid='"+empid+"'";
stmt.executeQuery(q);
msg="Updated Record Successfully...";
}
else
{
msg="Employee Not Exist...";
}
break;
}
rs.close();
stmt.close();
con.close();

}catch(Exception e){}
res.sendRedirect("pro28index.jsp?msg="+msg);
}
}


1 comment:

  1. Thank you to tell us so much useful information. I’m glad to read it. for more details you can go to below link…
    herbal incense

    ReplyDelete