tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nishant_Awas...@progressive.com
Subject DB2 and servlet (Correct servlet file) ??? Help !!!
Date Thu, 01 Aug 2002 17:51:34 GMT
Hello everyone...

Sorry In my prevous file I attach wrong file in the body...

I am trying to connect the run a simple servlet to retrieve the
query results from DB2 database.

1. Before writing a servlet, I wrote a simple java file and was successful
in retrieving the
contents from DB2 UDB.

2. Then I just converted the same JAVA file into servlet by adding Servlet
API and syntax in it.
3. It compiles fine.
4. Then from a web page I try to pass one parameter to my servlet.
5. The servlet doesnot retrieve any records from the database which
initially it was bringing
    when I wrote simple java file.
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class Ndb2Websphere extends HttpServlet {

      Statement stmt;
                ResultSet rs;
               int count =0;

    public void doGet (HttpServletRequest req, HttpServletResponse res)
     throws ServletException, IOException
    {


              res.setContentType("text/html");

     ServletOutputStream out = res.getOutputStream();

             String emp = req.getParameter("empnumber");
             out.println("paramater passed is "+emp +" <BR>");
                  out.println("<BR><BR>Count is"+ count);
                 try{
          Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
           out.println("<BR>Found the JDBC driver <BR>");
                     }
                     catch( Exception e)
                     {
          //        e.printStackTrace();
                            out.println("\nDriver class not found
exception");
             }
                     finally
                      {
                           out.println("<BR>Driver is properly loaded and
registered ");
                      }

               try{
                Connection con = null;


                           String url;

                       out.println("<BR>Connection URL is  good");


                        try{
                   con = DriverManager.getConnection
("jdbc:db2://100.3.13.34/SAMPLE","db2admin","db2pwd");
                        }
                        catch( Exception e4)
                        {
                                   e4.getMessage();
                e4.printStackTrace();
                        }


              // retrieve data from the database

                           out.println("<BR>Retrieve some data from the
database...");

                        try{
                stmt = con.createStatement();
                                rs = stmt.executeQuery("SELECT empno from
db2admin.employee");
          }
                         catch( Exception e5)
                         {
                                       e5.getMessage();
                e5.printStackTrace();
                         }

                   out.println("<BR>Received results:");

                         try{
                                 if(!rs.next())
                                   {
                                                   count = count +1;
                                     String a = rs.getString(1);

                           out.println(" empno is " + a );
                    out.println("<BR><BR>While Count is"+ count);
                   }
           }
                           catch( Exception e6)
                           {
                                   e6.printStackTrace();
                           }
                           out.println("<BR><BR>Count is"+ count);

               rs.close();
                       stmt.close();
                            con.close();

                }
                catch (SQLException e1)
                {
                       e1.getMessage();
                       e1.printStackTrace();
                }
                catch( Exception e)
                {
                       e.getMessage();
               e.printStackTrace();
                }
                finally
                {
                        out.println("<BR>I am out");
                }
      }
    public void doPost (HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
        doGet(req,res);
    }


}
------------------------------------------------------------------------------------

Nishant Awasthi





--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message