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 16:16:26 UTC

Database access through JDBC-SERVLET

I am a student and new user of tomcat, and am working on a project that 
involves accessing database through servlet. The code that i have written 
compiles and when i run it on tomcat 4.0.4, the statment till the "try{..." 
gets executed, but the connection to the database is not made. I would 
really appriciate if anyone could help me. Thanks,
-Khozaima.
PS: code and web.xml (I haven't done any editing in /conf/server.xml)
the application is stored in /webapps/ROOT/WEB-INF/classes/
web.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
  <!DOCTYPE web-app (View Source for full doctype...)>
<web-app>
<servlet>
  <servlet-name>Serve</servlet-name>
  <servlet-class>Servlet_JDBC</servlet-class>
  </servlet>
<servlet-mapping>
  <servlet-name>Serve</servlet-name>
  <url-pattern>/Serve</url-pattern>
  </servlet-mapping>
  </web-app>

Servlet 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></HEAD>");
	  out.println("<BODY bgColor=green text=#008000 >");
      out.println("<TABLE align=center border=1 width=\"75%\">");
      out.println("<TR><TD>RESCFG</TD></TR>");
	try{
           Class.forName("org.gjt.mm.mysql.Driver");
           conn = DriverManager.getConnection         
("jdbc:mysql://dbServerURL/DBNAME", "myUserName", "pw");
		   Statement stmt=conn.createStatement();
	       ResultSet rs=stmt.executeQuery("SELECT PARAMETER, VALUE FROM 
RESTABLE");
		while(rs.next()){
    		out.println("<TR><TD>" + rs.getString(1) + "</TD>");
    		out.println("<TD>" + rs.getString(2) + "</TD></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(){}
}





_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com


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