Here is a source code for searching of some terms in database,First of all please tell me the differece between using
class.forname and DriverManager.registerDriver.When I use this code with proper initialization it executes twice but after that it terminates.pLease tell me whats wrong with this,I am new to
servlets so please excuse me if this sounds odd.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import sun.jdbc.odbc.*;
public class SearchPage extends HttpServlet{
Connection dbCon;
public void init() throws ServletException{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbCon=DriverManager.getConnection("jdbc

dbc:test");
DriverManager.registerDriver(new JdbcOdbcDriver());
dbCon=DriverManager.getConnection("jdbc

dbc:test");
}
catch(ClassNotFoundException e)
{
System.out.println("Test:database driver ccould not be found");
System.out.println(e.toString());
throw new UnavailableException(this,"Database driver class not found");
}
catch(SQLException e)
{
System.out.println("Error connecting to the database");
System.out.println(e.toString());
throw new UnavailableException(this,"Cannot connect to the database");
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
/*try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbCon=DriverManager.getConnection("jdbc

dbc:test");
}
catch(ClassNotFoundException e)
{
System.out.println("Test:database driver ccould not be found");
System.out.println(e.toString());
throw new UnavailableException(this,"Database driver class not found");
}
catch(SQLException e)
{
System.out.println("Error connecting to the database");
System.out.println(e.toString());
throw new UnavailableException(this,"Cannot connect to the database");
}*/
/*try{
DriverManager.registerDriver(new JdbcOdbcDriver());
dbCon=DriverManager.getConnection("jdbc

dbc:test");
}
catch(SQLException e)
{
System.out.println("Error connecting to the database");
System.out.println(e.toString());
throw new UnavailableException(this,"Cannot connect to the database");
}*/
ResultSet rs;
Statement stmt;
StringBuffer qry=new StringBuffer(1024);
int numCriteria=0;
String name="pawan";
String zip="831001";
String speciality="java";
String money="4000";
res.setContentType("text/html");
PrintWriter out=res.getWriter();
name=req.getParameter("txtName");
speciality=req.getParameter("txtSpeciality");
zip=req.getParameter("txtZip");
money=req.getParameter("txtMoney");
qry.append("SELECT fname,speciality,money_range FROM Table1 WHERE");
if(name.length()>0){
qry.append(" fname LIKE '%");
qry.append(name);
qry.append("%' AND");
numCriteria++;
}
if(zip.length()>0){
qry.append(" zip LIKE '%");
qry.append(zip);
qry.append("%' AND");
numCriteria++;
}
if(speciality.length()>0){
qry.append(" speciality LIKE '%");
qry.append(speciality);
qry.append("%' AND");
numCriteria++;
}
if(money.length()>0){
qry.append(" money_range LIKE '");
qry.append(money);
qry.append("' AND");
numCriteria++;
}
if(numCriteria>0)
{
qry.delete(qry.length()-3,qry.length());
}else{
qry.delete(qry.length()-5,qry.length());
}
try{
stmt=dbCon.createStatement();
rs=stmt.executeQuery(qry.toString());
}catch(SQLException e)
{
res.sendError(res.SC_ACCEPTED,"The request hasbeen accepted ,but it failed to complete due to a problem in quering the database.");
return;
}
HTML h=new HTML("Search Results");
h.add(HTML.HEADING,"Selected results",true);
h.add(HTML.LINE," ",false);
String labels[]={"Name" ,"Speciality","Money Range"};
if(!h.addTable(labels,rs)){
res.sendError(res.SC_ACCEPTED,"The request hasbeen accepted but it failed to complete due to problem accessing the data.");
return;
}
out.println(h.getPage());
out.close();
}
public void destroy(){
try{
dbCon.close();
}catch(Exception e)
{
System.out.println("Test

atabase close failed");
System.out.println(e.toString());
}
}
public class HTML
{
public static final int NORMAL=0;
public static final int HEADING=1;
public static final int LINE=2;
public StringBuffer buffer;
public HTML(String title)
{
buffer=new StringBuffer(4096);
this.buffer.append("<HTML><HEAD><TITLE>");
this.buffer.append(title);
this.buffer.append("</TITLE></HEAD><BODY>");
}
public void add(int style,String text,boolean linebreak)
{
switch(style)
{
case NORMAL:
this.buffer.append(text);
break;
case HEADING:
this.buffer.append("<H1>");
this.buffer.append(text);
this.buffer.append("</H1>");
break;
case LINE:
this.buffer.append("<HR>");
break;
default:
break;
}
if(linebreak)
{
buffer.append("<BR>");
}
}
public String getPage()
{
this.buffer.append("</BODY></HTML>");
return this.buffer.toString();
}
public boolean addTable(String labels[],ResultSet rs)
{
int cols=labels.length;
this.buffer.append("<TABLE WIDTH='100%'>");
this.buffer.append("<TR>");
for(int i=0;i<cols;i++){
this.buffer.append("<TH ALIGN='left'>");
this.buffer.append(labels[i]);
this.buffer.append("</TH>");
}
this.buffer.append("</TR>");
try{
while(rs.next()){
this.buffer.append("<TR>");
for(int i=1;i<=cols;i++){
this.buffer.append("<TD>");
this.buffer.append(rs.getString(i));
this.buffer.append("</TD>");
}
this.buffer.append("</TR>");
}
}catch(SQLException e)
{
return false;
}
this.buffer.append("</TABLE>");
return true;
}
}
}