You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by khozaima shakir <sk...@hotmail.com> on 2002/08/02 00:20:55 UTC

Connecting to JDBC mysql database in a servlet

Hi all!
I am trying to connect to JDBC in a servlet, the html page gets displayed 
the try statement and i find that the connection is not made. I am using 
tomact 4.0.4 and have deployed the class file in 
\webapps\ROOT\WEB-INF\classes directory. Can anyone please suggest a path 
forward? In case i need to map the servlet in web.xml, i don't know how to !
Thanks in advance!
Khozaima
my code:
public class Servlet_JDBC extends HttpServlet{
	Connection conn;
	private ServletConfig config;
	public void init(ServletConfig config)
		throws ServletException{
		this.config=config;
	}
    public void service (HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
       	  HttpSession session = req.getSession(true);
	  res.setContentType("text/html");
	  PrintWriter out = res.getWriter();
          out.println("<HTML><HEAD><TITLE>Query Results</TITLE>");
	  out.println("</HEAD>");
	  out.println("<BODY bgColor=blanchedalmond text=#008000 topMargin=0>");
        out.println("<P align=center><FONT face=Helvetica><FONT 
color=fuchsia style=\"BACKGROUND COLOR
:white\"><BIG><BIG>List</BIG></BIG></FONT></P>");
	  out.println("<P align=center>");
        out.println("<TABLE align=center border=1 cellPadding=1 
cellSpacing=1 width=\"75%\">");

        out.println("<TR>");
        out.println("<TD>PARAMETER</TD>");
        out.println("<TD>VALUE</TD>");
        out.println("</TR>");
	try{
            Class.forName("org.gjt.mm.mysql.Driver");
            conn = 
DriverManager.getConnection("jdbc:mysql://databaseURL/dataBaseName", 
"userName", "password");
		Statement stmt=conn.createStatement();
	      ResultSet rs=stmt.executeQuery("SELECT PARAMETER, VALUE FROM 
MYTABLE");
		while(rs.next()){
    			out.println("<TR>");
    			out.println("<TD>" + rs.getString(1) + "</TD>");
    			out.println("<TD>" + rs.getString(2) + "</TD>");
    			out.println("</TR>");
			}
               rs.close();stmt.close();conn.close();
            	     }catch(Exception e){out.println(e.getMessage());}
       out.println("</TABLE></P>");
       out.println("<P>&nbsp;</P></FONT></BODY></HTML>");
	}
	public void destroy(){}
}




_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


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