JAVA CODE FOR THREE-TIER APPLICATION: ADDRESS SEARCH | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JAVA CODE FOR THREE-TIER APPLICATION: ADDRESS SEARCH

CREATE A  TABLE IN DB AS FOLLOWS:
================================
PID => NUMBER
NAME =>  TEXT
ADDRESS =>  TEXT
CITY  => TEXT
PHONE NO =>  NUMBER

Then Please insert some sample values in this table.Then Use the following code


FRONT PAGE PROGRAM USING HTML:

<html >
<head>
<title>WELCOME TO ADDRESS SEARCH</title>
</head>
<body bgcolor="cyan">
<h1> Welcome to address search</h1>
<form  name="form1" method="get" action="http://localhost:8080/servlet/address">
<input type="text" name="teln" />
<input type="submit"  value="Submit" />
</form>  
</body>
</html>                      



PROGRAM:

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Example extends HttpServlet
  {
  public void doGet(HttpServletRequest req,HttpServletResponse res)throws     ServletException,IOException
{
             res.setContentType("text/html");
            PrintWriter out=res.getWriter();
String n=req.getParameter("teln");
int nn=Integer.parseInt(n);
try
            {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c=DriverManager.getConnection("jdbc:odbc:xxx");
    Statement s=c.createStatement();
    String  str="select * from address where telno="+nn;
    ResultSet r=s.executeQuery(str);
   while(r.next())
{
int id1=r.getInt(1);
String name1=r.getString(2);
String add1=r.getString(3);
String city1=r.getString(4);
int no1=r.getInt(5);

//output page
out.println(“<html ><head><title>WELCOME TO ADDRESS SEARCH</title></head>”);
out.println(“<body><h1> WELCOME TO ADDRESS SEARCH<BR><BR><BR><BR>”);
out.println(“<table WIDTH="100%" CELLPADDING="2"                       CELLSPACING="2" bgcolor=CYAN align="center" BORDER="3">”);
out.println(“<tr><FONT SIZE=32><TH> ID </TH><TH>   NAME</TH> <TH> ADDRESS</TH><TH> CITY </TH><TH> PHONE NO</TH> </TR>”);
      out.println(“<tr><td>”+id1+”<td>”+name1+”<td>”+add1+”<td>”+city1+”<td>”+no1+”</tr>”);
out.println(“</table></body></html>”);
}
       }

catch(Exception e)
{
out.println(e);
}
      }
}


No comments:

Post a Comment

Thanks for your valuable comments

Followers