aspose file tools*
The moose likes Servlets and the fly likes problems in servelts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problems in servelts" Watch "problems in servelts" New topic
Author

problems in servelts

Pawan Sinha
Greenhorn

Joined: Jan 16, 2001
Posts: 10
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;
}


}
}




Pawan Sinha
Greenhorn

Joined: Jan 16, 2001
Posts: 10
It gives Null pointer Exception in Runtime that is when we run it through html page.. please guide me whats wrong
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
Do you know where exatly you got it?
For example, if you didn't specify any of the following parameters, you will have nullpointer, becose you call their methods later:
name=req.getParameter("txtName");
speciality=req.getParameter("txtSpeciality");
zip=req.getParameter("txtZip");
money=req.getParameter("txtMoney");
Pawan Sinha
Greenhorn

Joined: Jan 16, 2001
Posts: 10
Thanks for reply.I am fetching the data through HTML forms, So the data is being entered by the user and the servlet should put up the result on front end...Am I right.So why null pointer exception.
Leave that even if u explicitly assign the values of the parameters, null pointer exception comes even in that case ..
Thanks in Advance for reply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems in servelts