• Java Program: Guest Book Application Using Servlets

    BennieLGLgel Member
    // Java Program: Guest Book Application Using Servlets //
    
    import java.sql.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class guestbk extends httpservlet
    {
    	Connection con;
    	public void doPost(HttpServletRequest req,HttpServletResponse res) 
    throws
    IOException,ServletException
    	{
    		PrintWriter out=new PrintWriter(res.getOutputStream());
    		int ctr=0;
    		res.setContentType("text/html");
    	try
    	{
       		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       		con=DriverManager.getConnection("jdbc:odbc:booksrc","","");
       		Statement stmt=con.createStatement();
       		ResultSet rs=stmt.executeQuery("select * from gbook");
       		out.println("");
    		out.println("");
    	   	out.println("GuestBook");
    	   	out.println("");
    	   	out.println("");
       		out.println("

    GUEST BOOK DETAILS


    "); out.println(""); while(rs.next()) { ctr++; String email=rs.getString("emailid"); String cmt=rs.getString("comment"); out.println(""); } out.println("
    E-Mail AddressComment
    "+email+""+cmt+"
    "); if(ctr==0) out.println("
    No Records Found
    "); stmt.close(); con.close(); } catch(SQLException SQLExcep) { System.out.println("error"); } catch(ClassNotFoundException CNFE) { System.out.println("error"); } out.println(""); out.close(); } } // submit.java // import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class submit extends HttpServlet { Connection con; public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { String str1,str2; str1=req.getParameter("emailid"); str2=req.getParameter("comment"); System.out.println(str1); System.out.println(str2); res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.println("

    Guest Book is Successfully Updated

    "); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:booksrc","",""); Statement stmt=con.createStatement(); ResultSet rs; int i= stmt.executeUpdate("insert into gbook values('"+str1+"','"+str2+"')"); if(i==1) { System.out.println("ok"); } stmt.close(); con.close(); } catch(SQLException SQLExcep) { System.out.println("error"); } catch(ClassNotFoundException CNFE) { System.out.println("error"); }} } //guestbk.htm// GUEST BOOK


    ENTER YOUR EMAIL ADDRESS

    COMMENTS ARE ALWAYS WELCOME



Viewing 0 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish