views:

115

answers:

2

I have written a code to add user into DB. I need to redirect to EmpInfo.jsp, when we receive duplicate entries. I need to use more Exceptions for that and also i want to know how to redirect.

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "cervlet";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
String password = "1234";
int Empid =Integer.parseInt(request.getParameter("Empid").toString()); 
String Name = request.getParameter("Name").toString();
int Age =Integer.parseInt(request.getParameter("Age").toString()); 
int Salary =Integer.parseInt(request.getParameter("Salary").toString());
PreparedStatement stmt;
try {
 Class.forName(driver).newInstance();
 conn = DriverManager.getConnection(url+dbName,userName,password);
 System.out.println("Connected to the database");
 //ArrayList al=null;
 //ArrayList userList =new ArrayList();
 String query = "insert into employee set Empid='"+Empid+"',name='"+Name+"',Age='"+Age+"',Salary='"+Salary+"'";
stmt = (PreparedStatement) conn.prepareStatement(query);
  int i = 0;
try {
   i = stmt.executeUpdate(query);
    }
  catch (SQLException e) {
    String nextj = "/AddUser.jsp";
    RequestDispatcher rd = getServletContext().getRequestDispatcher(nextj);
   rd.forward(request, response);
    }
   System.out.println("i="+i);
System.out.println("query: " + query);
//if(i==0)
 //{
  //String nextj = "/EmpInfo.jsp";
    //RequestDispatcher cd = getServletContext().getRequestDispatcher(nextj);
    //cd.forward(request, response);
 //response.sendRedirect("servletRecord");
//}
 response.sendRedirect("/EmpInfo.jsp");
 conn.close();
 System.out.println("Disconnected from database");

} catch (Exception e) {
e.printStackTrace();

Updates

I need to redirect to EmpInfo.jsp, when it adds a new entry. After the execution of the method executeUpadte(), i used its return value to redirect to another page called EmpInfo.jsp. But its not redirecting. I'm using eclipse. Tell me the common ways to multiple redirection of .jsp pages.

+9  A: 

Well, if you are asking how to catch different Exceptions the following code is how you do it:

try {
    ...
    // Code that may throw a few types of exceptions
    ...
} catch(FileNotFoundException fnfe) {
    // handle very specific exceptions
} catch (IOException ioe) {
    // handle less specific exceptions
} catch (Exception e) {
    // handle the most generic exception case
} 

This will allow you to handle multiple exception types in one code block.

jjnguy
+2  A: 

Handling multiple exceptions is already answered.

About redirect to other page : You can use (As you have already used) forward method (if other page is in same domain )and sendRedirect method (other page can be in some other domain). But you need to take care that nothing is written to browser else you get exception while forwarding/redirecting.

In your case you have already written to browser in form of these statements:

   response.setContentType("text/html");
   PrintWriter out = response.getWriter();

Looking at your code it seems you don't even require these statements , As in any case you should be forwarding to other page.

NOTE: You are using forward at one place and sendRedirect at one place. Hope you are aware of difference between these. In my opinion you need to just use forward method at both places.

YoK