This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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");
} 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");
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);
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>");
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.