import java.io.*;
import java.sql.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class listAll extends HttpServlet
{
        Connection con = null;
        PrintWriter out = null;
        ResultSet rs = null;
        PreparedStatement ps;

        public void init(ServletConfig config) throws ServletException
        {
                super.init(config);
        }

        public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
        {
                res.setContentType("text/html");
                out.println("ContentType : "+req.getContentType());
                HttpSession session = req.getSession( true );
                out = res.getWriter();

                // connect to database, fetch all the records and display them
            	
                try
                {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con = DriverManager.getConnection("jdbc:odbc:board","","");
                        out.println("Hello 111 U R Being printed...");

                        if(con != null)
                        {
                                Statement stmt = con.createStatement();
                                out.println("Hello222 U R Being printed...");
                                rs = stmt.executeQuery("select * from mBoardDB where questAns='0'");
                                int i = 0;
                                String abc = "";
                                while(rs.next())
                                {
                                        i++;
                                        abc = rs.getString("SNo");
                                        out.println(abc);
                                        out.println(i);
                                        out.println(". ");
                                        out.println(" <a href=http://xcorp:8080/examples/servlet/showMessage?num='"+abc+"'"+" > ");
                                        out.println(rs.getString("subject"));
                                        out.println("</a>");
                                        out.println("<br>");
                                }
                        }
                        rs.close();
                catch(Exception sqlE)
                {
                        out.println("SQL Exception 1: "+sqlE.toString());
                }
                finally
                {
                        try
                        {
                                if(con != null)
                                {
                                        con.close();
                                }
                        }
                        catch(SQLException e)
                        {
                                out.println("SQL Exception 2: "+e.toString());
                        }

                        out.println(getServletInfo());

                        out.close();
                }
            }
        }
 	
        public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
        {
                Enumeration parameters = req.getParameterNames();
                String param = null;
                String sno = "3";
                String mBy = "";
                String sub = "";
                String date= "3/2/2000";
                String msg = "";
                String flagQA = "0";
                StringBuffer buffer = new StringBuffer();
        	
                while(parameters.hasMoreElements())
                {
                        param = (String)parameters.nextElement();
                        buffer.append(req.getParameter(param));
                }
                String record = buffer.toString();
                sno = record.substring(0,11);
                mBy = record.substring(11,61);
                sub = record.substring(61,161);
//              date= record.substring("3/2/2000");
                msg = record.substring(171,372);
        	
                String query = "insert into mBoardDB values('"+sno+"','"+mBy+"','"+sub+"','"+date+"','"+msg+"','','','','"+flagQA+"')";

                try
                {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con = DriverManager.getConnection("jdbc:odbc:board","","");
                	
                        if(con != null)
                        {
                                Statement stmt = con.createStatement();
                                ps = con.prepareStatement(query);
                        }
                }
                catch(Exception e)
                {
                        cleanUp();              	
                }
                finally
                {
                        try
                        {
                                if(con != null)
                                {
                                        con.close();
                                }
                        }
                        catch(Exception e)
                        {
                        }
                        out.println(getServletInfo());
                }
        	
        }
 	
    public void cleanUp() 
    { 
        try 
        { 
            System.out.println("Closing database connection"); 
            con.close(); 
        } 
        catch (SQLException e) 
        { 
            e.printStackTrace(); 
        } 
    } 	
 	
        public String getServletInfo()
        {
                return "<br>Rajneesh Garg -- rajneeshgarg@usa.net";
        }
}