//
//  start with:
//  http://localhost/.../servlet/jdbcexample?query=select+*+from+yourtable
//
//

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class jdbcexample extends HttpServlet
{
   public boolean POOL=true;  // true : with connectionpool false:without 
   // String driver="com.informix.jdbc.IfxDriver";
   String driver="sun.jdbc.odbc.JdbcOdbcDriver";
   // String url="jdbc:informix-sqli://jaguar2:1525/db:informixserver=server1";
   String url="jdbc:odbc:yourOdbcDsn";
   String user="";
   String password="";
   
   public void init (ServletConfig config) throws ServletException
   {
	super.init (config);
	ServletContext context=config.getServletContext();
	ConnectionPoolObject cpo=new ConnectionPoolObject();
	cpo.startTimer();
	context.setAttribute("currentConnections",cpo);
   }
      
   public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
   {
      response.setContentType("Text/html");
      PrintWriter out = response.getWriter();

    	String query = request.getParameter("query");  //  query-string


      out.println("<HTML>");
      out.println("<HEAD><TITLE>JDBC Example</TITLE></HEAD>");
      out.println("<BODY BGCOLOR=\"#FFFFFF\">");
      out.println("<CENTER>");
  

      Connection conn = null;
      ConnectionPoolObject cpo=new ConnectionPoolObject();

      try
      {
         if (POOL) {
       		ServletContext context=getServletContext();
            cpo=(ConnectionPoolObject)context.getAttribute("currentConnections");
            conn = cpo.getConnection(driver,url,user,password);  
         } else { 
            Class.forName(driver);                                                                                
            conn = DriverManager.getConnection(url,user,password);                                                  
         }
         
         
         Statement stmt = conn.createStatement();                               
         ResultSet rs = stmt.executeQuery(query);         
         ResultSetMetaData md = null;
         md = rs.getMetaData();
         int numCols = md.getColumnCount();

         out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">");                                                                               
         
         out.println("<TR>");                      
        	for (int i=1; i<= numCols; i++) {    
	          out.println("<TH>"+ md.getColumnLabel(i) + "</TH>");
      	};
         out.println("</TR>");                      
     
         while(rs.next()) {                                                                      
            out.println("<TR>");                      
            for ( int i=0; i < numCols; i++ ){
               out.println("<TD>" + rs.getString(i+1) + "</TD>");              
            }
            out.println("</TR>");    
         }                                                                      
         
         out.println("</TABLE>");
      }                                                                         
      catch(SQLException e)                                                     
      {                                                                         
         out.println("SQLException: " + e.getMessage() + "<BR>");               
         while((e = e.getNextException()) != null)                              
            out.println(e.getMessage() + "<BR>");                               
      }                                                                         
      catch(ClassNotFoundException e)                                           
      {                                                                         
         out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");     
      }                                                                         
      finally                                                                   
      {                                                                         
         if(conn != null)                                                       
         {                                                                      
            try                                                                 
            {                                                                   
               if (POOL) {
           		   //  ConnectionPoolObject cpo=new ConnectionPoolObject();
 		              cpo.putConnection(url,user,conn,5*60*1000); // keep open for 5 minutes
               } else {
                  conn.close();                                                    
               }
            }                                                                   
            catch (Exception ignored) {}                                        
         }                                                                      
      }                                                                         
                                                                                
      out.println("</BODY>");                                                   
      out.println("</HTML>");                                                   
                                                                                
   }                                                                            
}                                                                               
