Monday, 26 September 2011

Select the data from database

//Select the data from database
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class Update extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String url=" ";
Connection cont;
try
{
url="jdbc:odbc:AddressBook1";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cont=DriverManager.getConnection(url);
Statement statement=cont.createStatement();
String query="SELECT * FROM addresses WHERE ID= '"+ request.getParameter ("number") + "'";
ResultSet rs=statement.executeQuery(query);
while(rs.next())
{
String recnum=rs.getString(1);
if(recnum.equals(request.getParameter("number")))
{
pw.println("

Number:" +recnum+"

");
pw.println("

Name:" +rs.getString(2)+"

");
pw.println("

E-mail:" +rs.getString(4)+"

");
pw.println("

Phone:"+rs.getString(5)+"

");
}
else
{
pw.println("No record found");
}
}
statement.close();
}
catch(Exception e)
{
pw.println(e.toString());
}
}
}

No comments:

Post a Comment