why omit the try block.correct if wrong.

hi guys i need some real help with this servlet code.i want to understand why the try block is always omitted.here is the sample:

package myservlets;

import com.sun.tools.internal.ws.processor.model.Request;
import java.io.*;
import java.net.*;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import javax.servlet.*;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.ResultSet;
import javax.servlet.http.*;
import org.apache.tools.ant.taskdefs.SQLExec;

/**
*
* @author ndifreke
* @version
*/
public class searchPatientIDServlet extends HttpServlet {

/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
sendHeader(response);
sendSearchForm(response);
sendFooter(response);

}

//
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("");
out.println("");
out.println("Patient ID search");
out.println("");
out.println("");
out.println("
");
try{
String sql = "select * from user_tbl";
Connection con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/emr_records","root","akanboy");
Statement stmt = (Statement)con.createStatement();
ResultSet rs = (ResultSet)stmt.executeQuery(sql);
while (rs.next()){
out.println(rs.getInt(1) + " "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
stmt.close();
con.close();
}catch (SQLException ex){

}
out.println("This is just a test");
out.close();

}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
//
public void sendFooter(HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.println("
");
}
public void sendHeader(HttpServletResponse response)throws IOException {
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("Patient ID search");
out.println("");
out.println("");
out.println("
");
}

public void init(ServletConfig config) throws ServletException {
try{
Class.forName("com.mysql.jdbc.Driver");

}catch (ClassNotFoundException ex){
System.out.println(ex.getMessage());
}
}

public void sendSearchForm(HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.println(" ");
out.println("");
out.println("
First Name:
Last Name:
");
}

/* public void sendSearchResult(HttpServletResponse response,HttpServletRequest request)
throws IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
out.println("
The results are");
try{
String sql = "select patient_id,first_name,last_name,sex,origin,tribe from patient_data_tble " +
"where first_name='"+firstName +"' and last_name='"+lastName +"'";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emr_records","root","akanboy");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String fName,lName,sex,origin,tribe;
int id;
out.println("");
while (rs.next()){
fName = rs.getString("first_name");
lName = rs.getString("last_name");
sex = rs.getString("sex");
origin = rs.getString("origin");
tribe = rs.getString("tribe");
id = Integer.parseInt(String.valueOf(rs.getInt("patient_id")));
out.println("" +
"");
out.println("" +
"");

}
out.println("
Patient IDFirst NameLast NameSexOriginTribe
"+fName+""+lName+""+sex+""+ origin +""+tribe+"
");
}catch (SQLException ex){
System.out.println(ex.getMessage());
}
}*/
}


both the try block in the dopost() and sendresult method get omitted and everyother thing works.now i got all the necessary requirements working.and i am connecting to mysql5.0.i use netbeans id.with jdk1.6.this thing is getting me nervouse about this whole programming.


Comments

  • : hi guys i need some real help with this servlet code.i want to
    : understand why the try block is always omitted.here is the sample:
    :
    : package myservlets;
    :
    : import com.sun.tools.internal.ws.processor.model.Request;
    : import java.io.*;
    : import java.net.*;
    : import com.mysql.jdbc.Connection;
    : import java.sql.DriverManager;
    : import javax.servlet.*;
    : import java.sql.SQLException;
    : import com.mysql.jdbc.Statement;
    : import com.mysql.jdbc.ResultSet;
    : import javax.servlet.http.*;
    : import org.apache.tools.ant.taskdefs.SQLExec;
    :
    : /**
    : *
    : * @author ndifreke
    : * @version
    : */
    : public class searchPatientIDServlet extends HttpServlet {
    :
    : /** Processes requests for both HTTP GET and
    : POST methods.
    : * @param request servlet request
    : * @param response servlet response
    : */
    : protected void processRequest(HttpServletRequest request,
    : HttpServletResponse response)
    : throws ServletException, IOException {
    : response.setContentType("text/html;charset=UTF-8");
    : PrintWriter out = response.getWriter();
    : sendHeader(response);
    : sendSearchForm(response);
    : sendFooter(response);
    :
    : }
    :
    : //
    : /** Handles the HTTP GET method.
    : * @param request servlet request
    : * @param response servlet response
    : */
    : protected void doGet(HttpServletRequest request, HttpServletResponse
    : response)
    : throws ServletException, IOException {
    : processRequest(request, response);
    : }
    :
    : /** Handles the HTTP POST method.
    : * @param request servlet request
    : * @param response servlet response
    : */
    : protected void doPost(HttpServletRequest request,
    : HttpServletResponse response)
    : throws ServletException, IOException{
    :
    : PrintWriter out = response.getWriter();
    : response.setContentType("text/html");
    : out.println("");
    : out.println("");
    : out.println("Patient ID search");
    : out.println("");
    : out.println("");
    : out.println("
    ");
    : try{
    : String sql = "select * from user_tbl";
    : Connection con =
    : (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/
    : emr_records","root","akanboy");
    : Statement stmt = (Statement)con.createStatement();
    : ResultSet rs = (ResultSet)stmt.executeQuery(sql);
    : while (rs.next()){
    : out.println(rs.getInt(1) + " "+rs.getString(2)+" "+rs.getString(3));
    : }
    : rs.close();
    : stmt.close();
    : con.close();
    : }catch (SQLException ex){
    :
    : }
    : out.println("This is just a test");
    : out.close();
    :
    : }
    :
    : /** Returns a short description of the servlet.
    : */
    : public String getServletInfo() {
    : return "Short description";
    : }
    : //
    : public void sendFooter(HttpServletResponse response) throws
    : IOException {
    : PrintWriter out = response.getWriter();
    : out.println("
    ");
    : }
    : public void sendHeader(HttpServletResponse response)throws
    : IOException {
    : PrintWriter out = response.getWriter();
    : out.println("");
    : out.println("");
    : out.println("Patient ID search");
    : out.println("");
    : out.println("");
    : out.println("
    ");
    : }
    :
    : public void init(ServletConfig config) throws ServletException {
    : try{
    : Class.forName("com.mysql.jdbc.Driver");
    :
    : }catch (ClassNotFoundException ex){
    : System.out.println(ex.getMessage());
    : }
    : }
    :
    : public void sendSearchForm(HttpServletResponse response) throws
    : IOException {
    : PrintWriter out = response.getWriter();
    : out.println(" ");
    : out.println("");
    : out.println("
    First Name:
    Last Name:
    ");
    : }
    :
    : /* public void sendSearchResult(HttpServletResponse
    : response,HttpServletRequest request)
    : throws IOException {
    : PrintWriter out = response.getWriter();
    : response.setContentType("text/html");
    : String firstName = request.getParameter("firstName");
    : String lastName = request.getParameter("lastName");
    : out.println("
    The results are");
    : try{
    : String sql = "select
    : patient_id,first_name,last_name,sex,origin,tribe from
    : patient_data_tble " +
    : "where first_name='"+firstName +"' and last_name='"+lastName +"'";
    : Connection conn =
    : DriverManager.getConnection("jdbc:mysql://localhost:3306/emr_records"
    : ,"root","akanboy");
    : Statement stmt = conn.createStatement();
    : ResultSet rs = stmt.executeQuery(sql);
    : String fName,lName,sex,origin,tribe;
    : int id;
    : out.println("");
    : while (rs.next()){
    : fName = rs.getString("first_name");
    : lName = rs.getString("last_name");
    : sex = rs.getString("sex");
    : origin = rs.getString("origin");
    : tribe = rs.getString("tribe");
    : id = Integer.parseInt(String.valueOf(rs.getInt("patient_id")));
    : out.println("" +
    : "");
    : out.println(" : " +
    : "");
    :
    : }
    : out.println("
    Patient IDFirst NameLast
    : Name
    SexOriginTribe
    "+fName+""+lName+""+sex+""+ origin +""+tribe+"
    ");
    : }catch (SQLException ex){
    : System.out.println(ex.getMessage());
    : }
    : }*/
    : }
    :
    :
    : both the try block in the dopost() and sendresult method get omitted
    : and everyother thing works.now i got all the necessary requirements
    : working.and i am connecting to mysql5.0.i use netbeans id.with
    : jdk1.6.this thing is getting me nervouse about this whole
    : programming.
    :
    :
    :
    What makes you think that the try-except block is being omitted? Your try-except block only catches SQLExceptions, all other exceptions are not caught and might still break your code.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion