|
|
| JSP,JDBC and HTML(Very Urgent) |
Expert:Ragavendran.R
Respected Sir/Madam, Thanks for your response. You asked me to give my requirements clearly. My requirement is as follows: HOME PAGE: Actually my program deals with accessing employee details from database using JDBC and JSP. The home page i.e HTML Page must contain three text boxes. 1)Employee ID,2)Employee Name and 3) Enter New Name. Near Employee ID text box,there must be a combo box which contains all the ID's present in the database. The same page must also consists of four radio buttons 1)Insert,2)Delete,3)Update and 4)Query with a submit button. PROCESS: Now when I click Combo box and select the ID,that particular ID must automatically fit into the Employee ID text box.And When I click Insert, an alert box must display"Insertion successful". The above process must be followed for delete and update operation also. Now for Query,When I click combo box and select an ID, it must come into the Employee ID text Box and when I click the Query option,The Paricular name for the paricular ID selected must automatically be displayed in the Employee Name text box. NOTE: If any reason for failure for Insertion,Deletion,Updation or query exists, then the reason must be in the alert box display..
This is my exact requirement in which if I get an immediate help,I will be very much grateful to ROSEINDIA team.. This is Very Urgent. Plz Plz send me the coding ASAP..
Thank you/Regards, R.Ragavendran... |
| Answers |
Hi raghavendran, I completed the code:
I am not using any html. I used only jsp & jdbc.
HOme Page: FindEmp.jsp
<%@ page language="java" %> <%@ page import="java.lang.*" %> <%@ page import="java.sql.*" %>
<HTML>
<BODY BGCOLOR="LIGHTYELLOW"> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:Employee","",""); Statement stmt=null; %> <FORM METHOD="GET" ACTION="process.jsp">
<H3> <P ALIGN="CENTER"> <FONT SIZE=6> EMPLOYEE DETAILS </FONT> </P> </H3> </BR> </BR> <BR> <BR> <TABLE CELLSPACING=5 CELLPADDING=5 BGCOLOR="LIGHTBLUE" COLSPAN=2 ROWSPAN=2 ALIGN="CENTER"> <TR> <TD> <FONT SIZE=5> Enter Employee ID </TD> <TD> <INPUT TYPE="TEXT" ID="id" NAME="id"> </FONT> </TD> </TR> <TR> <TD> <FONT SIZE=5> Enter Employee Name </TD> <TD><INPUT TYPE="TEXT" ID="name" NAME="name"> </FONT> </TD> </TR> <TR> <TD> <FONT SIZE=5> Enter New Name (For UPDATE only) </TD> <TD><INPUT TYPE="TEXT" NAME="nname"> </FONT> </TD> </TR> <TR> <TD>Employee Ids: <SELECT NAME="empIds" ONCHANGE="document.getElementById('id').value=this.options[this.selectedIndex].text"><option>Select One</option> <% String rec="SELECT Emp_code,Emp_name FROM Employee"; try { stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(rec); while(rs.next()) { %> <option><%= rs.getInt(1)%></option> <%} } catch(Exception e){System.out.println(e);} %> </SELECT> </TD> </TR> <TR> <FONT SIZE=6> <B> <TD><INPUT TYPE="RADIO" NAME="r1" VALUE="add" >Insert </TD> </TR> <TR> <TD><INPUT TYPE="RADIO" NAME="r1" VALUE="del" >Delete </TD> </TR> <TR> <TD><INPUT TYPE="RADIO" NAME="r1" VALUE="mod" >Update </TD> </TR> <TR> <TD><INPUT TYPE="RADIO" NAME="r1" VALUE="query">Query </TD>
</TR> </FONT> </B> <TR> <TD><INPUT TYPE="SUBMIT" VALUE="Submit"> <INPUT TYPE="RESET" VALUE="Reset"> </TD> </TR> <% if(session.getAttribute("empcode")!=null && session.getAttribute("empname")!=null) { %> <script language="javascript"> document.getElementById('id').value=<%=session.getAttribute("empcode").toString()%> document.getElementById('name').value='<%=session.getAttribute("empname").toString()%>' </script> <% session.removeAttribute("empcode"); session.removeAttribute("empname"); }%> </FORM>
</BODY> </HTML>
Note: Please align textboxes & combo box according to ur requirement.
process.jsp:
<%@ page language="java" %> <%@ page import="java.lang.*" %> <%@ page import="java.sql.*" %>
<html> <body> <% Connection con=null; Statement stmt=null; //ResultSet rs=null; String radioSelected=request.getParameter("r1"); String name=request.getParameter("name"); String code=request.getParameter("id");
String query=null; int EmpID=Integer.parseInt(code); int status; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:Employee","",""); stmt=con.createStatement(); if(radioSelected.equals("add")){ query="INSERT INTO Employee (Emp_code,Emp_name) VALUES("+EmpID+",'"+name+"')"; status=stmt.executeUpdate(query); if(status==1){//This is for successful insertion %> <script language="javascript"> alert("Insertion successful"); document.location="FindEmp.jsp"; </script> <% }else{ %> <script language="javascript"> alert("Insertion Failed"); document.location="FindEmp.jsp"; </script> <% } } else if(radioSelected.equals("del")){ query="DELETE * FROM Employee where Emp_code="+EmpID+""; status=stmt.executeUpdate(query); if(status==1){//This is for successful Deletion %> <script language="javascript"> alert("Deletion successful"); document.location="FindEmp.jsp"; </script> <% }else{ %> <script language="javascript"> alert("Deletion Failed Bcoz,Employee Id Not Found"); document.location="FindEmp.jsp"; </script> <% } } else if(radioSelected.equals("mod")){ String new_name=request.getParameter("nname"); query="UPDATE Employee SET Emp_name='"+new_name+"' where Emp_code="+EmpID+""; status=stmt.executeUpdate(query); if(status==1){//This is for successful updation %> <script language="javascript"> alert("Record Updated successfully"); document.location="FindEmp.jsp"; </script> <% }else{ %> <script language="javascript"> alert("Updation Failed"); document.location="FindEmp.jsp"; </script> <% } } else if(radioSelected.equals("query")){ query="Select Emp_code, Emp_name from Employee where Emp_code="+EmpID+""; ResultSet rs=stmt.executeQuery(query); if(rs.next()) //If record found.... { session.setAttribute("empcode",rs.getInt(1)); session.setAttribute("empname",rs.getString(2)); %> <script language="javascript"> document.location="FindEmp.jsp"; </script> <% } else{ %> <script language="javascript"> alert("Employee Id Not Found"); document.location="FindEmp.jsp"; </script> <%} } } catch(Exception e){ e.printStackTrace(); } finally{ con.close(); stmt.close(); //rs.close(); }
%> </body> </html>
For query radio button, i changed slightly. Not immediately after clicking the radio button, after selecting the radio button then click submit button. Then it displays selected employee id details.
I didn't do any validations for ur text boxes like number only allowed for employee id text box etc.
Thanks Sreenivas
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|