The website has been updated, checkout below websites.

https://codingvariable.com/

Friday, 13 January 2017

Servlet Program to Accept the values from the user and to display all the records added in Database.

2.Write a Servlet to Accept the values from the user and to display all the records added in Database.


In these programs, we have created two Servlets

  1. DeptForm.java
  2. DeptEntry.java


DeptForm.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DeptForm extends HttpServlet {
@Override
public void service(HttpServletRequest req,
HttpServletResponse res)
throws IOException,ServletException { 
try{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<Html><Head><Title>Department Info</Title></Head>");
out.println("<Form name=frm method=get action=DeptEntry>");
out.println("<table><tr>");
out.println("<td>Department No: <input type=text name=txtNo></td></tr><br>");
out.println("<tr><td>Department Name: <input type=text name=txtName></td></tr><br>");
out.println("<tr><td>Location: <input type=text name=txtLoc></tr></td><br>");
out.println("</table>");
out.println("<input type=submit name=Submit>");
out.println("<input type=reset name=Reset>");

out.println("</Form></Html>");
       }
catch (Exception e){
System.out.println(e.getMessage());
      } 
   } 
}

DeptEntry.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class DeptEntry extends HttpServlet
{
@Override
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException,ServletException
{
String a,b,c,d,e,f;
int i;
Connection con;
try {
res.setContentType("text/html");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dept","root","1234");
String Query="insert into depttable Values(?,?,?)";
Statement st=con.createStatement();
PreparedStatement ps;
ps=con.prepareStatement(Query);
a=(String)req.getParameter("txtNo");
b=(String)req.getParameter("txtName");
c=(String)req.getParameter("txtLoc");
ps.setString(1,a);
ps.setString(2,b);
ps.setString(3,c);
ps.executeUpdate(); PrintWriter out=res.getWriter();
ResultSet rs=st.executeQuery("select * from depttable");
ResultSetMetaData md=rs.getMetaData();
int num=md.getColumnCount();
out.println("<html><body><table border=1><tr>");
for(i=1;i<=num;i++)
 { 
out.print("<th>"+md.getColumnName(i)+"</th>");
 }
out.println("</tr>");
while(rs.next())
 {
d=rs.getString(1);
e=rs.getString(2);
f=rs.getString(3);
out.println("<tr><td>"+d);
out.println("</td><td>"+e);
out.println("</td><td>"+f);
out.println("</td></tr>");
 }
out.println("</table>");
con.commit();
out.println("<a href=DeptForm.class>BACK</a>");
out.println("</body></html>");
 }
catch (Exception ae)
{ 
    System.out.println(ae.getMessage());
}
}
}

No comments:

Post a Comment