You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by hari hari <ha...@hotmail.com> on 2002/09/25 18:24:48 UTC

Simplest Servlet-JDBC not working

hi my many freinds,

i having simplest of Servlet program (DBServlet.java) which is trying to 
connecting to my ORCL DB.

my Application and DB Server are sit on different machines. I having my JDBC 
Drivers (in ZIP format) on my App Server installed with CLASSPATH.

when I trying to run i getting below errors: police helping me
# java DBServlet
Exception in thread "main" java.lang.NoSuchMethodError: main
#

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DBServlet extends HttpServlet {

private Connection con;
private PrintWriter out;
private String url = "jdbc:oracle:thin:@MY_IP_ADDRESS:1521:MY_SID";


public void init(ServletConfig conf)
        throws ServletException {
    super.init(conf);
    try{

       //Class.forName("oracle.jdbc.OracleDriver");
       //con =DriverManager.getConnection(url, "scott", "tiger");
    }catch(Exception e) {
            System.err.println(e);
    }
}

public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    res.setContentType("text/html");
    try {
        out = res.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title> Sample JDBC Servlet Demo" + "</title>");
        out.println("</head>");
        out.println("<body>");

        Class.forName("oracle.jdbc.OracleDriver");
        con =DriverManager.getConnection(url, "scott", "tiger");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from emp");
        out.println("<UL>");
        while(rs.next()) {
            out.println("<LI>" +  rs.getString("EName"));
        }
        out.println("</UL>");
        rs.close();
        stmt.close();
    } catch(Exception e) { System.err.println(e);    }
      out.println("</body>");
      out.println("</html>");
      out.close();
}

--HARI



_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Simplest Servlet-JDBC not working

Posted by Vivek Chopra <vi...@yahoo.com>.
Hari,

You don't typically try to execute a servlet
directly.. it is invoked by the servlet engine when
you browse over to the appropriate URL. The error you
see is due to the fact that the JRE is looking for a
main() method. Since this is a servlet, it doesnt have
one!

Please read the Tomcat app developer's guide at
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/appdev/index.html
on how to deploy a servlet in Tomcat.

- Vivek

--- hari hari <ha...@hotmail.com> wrote:
> hi my many freinds,
> 
> i having simplest of Servlet program
> (DBServlet.java) which is trying to 
> connecting to my ORCL DB.
> 
> my Application and DB Server are sit on different
> machines. I having my JDBC 
> Drivers (in ZIP format) on my App Server installed
> with CLASSPATH.
> 
> when I trying to run i getting below errors: police
> helping me
> # java DBServlet
> Exception in thread "main"
> java.lang.NoSuchMethodError: main
> #
> 
> import java.io.*;
> import java.sql.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> 
> public class DBServlet extends HttpServlet {
> 
> private Connection con;
> private PrintWriter out;
> private String url =
> "jdbc:oracle:thin:@MY_IP_ADDRESS:1521:MY_SID";
> 
> 
> public void init(ServletConfig conf)
>         throws ServletException {
>     super.init(conf);
>     try{
> 
>        //Class.forName("oracle.jdbc.OracleDriver");
>        //con =DriverManager.getConnection(url,
> "scott", "tiger");
>     }catch(Exception e) {
>             System.err.println(e);
>     }
> }
> 
> public void service(HttpServletRequest req,
> HttpServletResponse res)
>     throws ServletException, IOException {
>     res.setContentType("text/html");
>     try {
>         out = res.getWriter();
>         out.println("<html>");
>         out.println("<head>");
>         out.println("<title> Sample JDBC Servlet
> Demo" + "</title>");
>         out.println("</head>");
>         out.println("<body>");
> 
>         Class.forName("oracle.jdbc.OracleDriver");
>         con =DriverManager.getConnection(url,
> "scott", "tiger");
>         Statement stmt = con.createStatement();
>         ResultSet rs = stmt.executeQuery("select *
> from emp");
>         out.println("<UL>");
>         while(rs.next()) {
>             out.println("<LI>" + 
> rs.getString("EName"));
>         }
>         out.println("</UL>");
>         rs.close();
>         stmt.close();
>     } catch(Exception e) { System.err.println(e);   
> }
>       out.println("</body>");
>       out.println("</html>");
>       out.close();
> }
> 
> --HARI
> 
> 
> 
>
_________________________________________________________________
> MSN Photos is the easiest way to share and print
> your photos: 
> http://photos.msn.com/support/worldwide.aspx
> 
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 


__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>