Wednesday, 24 January 2018

Advancedjava-Slip21



Slip21
Q1. Write a JDBC Program in java to display the names of Employees starting with ‘S’ character.     

import java.sql.*;

class Slip21

{
            public static void main(String args[])
            {          Connection con;
                        try
                        {
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    con=DriverManager.getConnection("jdbc:odbc:lab.dsn");
                                   
                                    Statement stmt=con.createStatement();
                                    ResultSet rs=stmt.executeQuery("select * from employee where name like 'S%'");
                                    System.out.println("eno\t"+"ename\t"+"department\t"+"sal");
                                    while(rs.next())
                                    {
                                                System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4));   
                                    }
                        }
                        catch(Exception e)
                        {
                                    System.out.println(e);

                        }
            }
  }

                                                                                                                    
Q2. Write a SERVLET program to Design an HTML page containing 4 option buttons (Painting, Drawing, Singing and swimming) and 2 buttons reset and submit. When the user clicks submit, the server responds by adding cookie containing the selected hobby and sends the HTML page to the client. Program should not allow duplicate cookies to be written.



Setb2.html



<html>
<body>
<form method="GET" action="http://localhost:8080/choice">
select your hobbie:<br>
<input type="radio" name="ch" value="painting">PAINTING<br>
<input type="radio" name="ch" value="drawing">DRAWING<br>
<input type="radio" name="ch" value="swiming">SWIMING<br>
<input type="radio" name="ch" value="singing">SINGING<br>
<br><input type="submit" value="select">
<br><input type="reset">
</form>
</body>
</html>   


setb2n.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


 public class setb2n extends HttpServlet 
{
 
 public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
    {
     
        PrintWriter out = null;
      try
       {  
        out=res.getWriter();
        Cookie c[]=req.getCookies();
        for(int i=0;i<c.length;i++)
        {
         String s1=c[i].getName();
         out.println("selected hobby are :"+s1); 
        }
       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
    }
 }


Setb21.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


 public class Setb21 extends HttpServlet {


    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
    {
     
        PrintWriter out = null;
      try
       {  
         out=res.getWriter();
         String hb=req.getParameter("ch");
         Cookie c1=new Cookie("hobby",hb);
         out.println("hobby are:"+hb);
         //res.addCookie(c1);
       }
       catch(Exception e)
       {
         e.printStackTrace();

       }
    }
 }