Slip11
Q1. Write a JDBC
program to remove “percentage” column from student (rno, sname, percentage)
table.
import
java.sql.*;
import
java.io.*;
import
javax.sql.*;
class
slip11
{
public static void main(String a[])
{
Connection con;
Statement stmt;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:lab.dsn");
stmt=con.createStatement();
int no =
stmt.executeUpdate("alter table student drop column per");
if(no!=0)
System.out.println("Drop
col sucessfully");
else
System.out.println("NOT Drop col ");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Q2. Write a
SERVLET program to change inactive time interval of session.
import
java.util.*;
import
java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public
class slip11 extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Get the current session object, create
one if necessary
HttpSession session = req.getSession();
out.println("<HTML><HEAD><TITLE>SessionTimer</TITLE></HEAD>");
out.println("<BODY><H1>Session Timer</H1>");
// Display the previous timeout
out.println("The previous timeout was
" +
session.getMaxInactiveInterval());
out.println("<BR>");
// Set the new timeout
session.setMaxInactiveInterval(2*60*60);
// two hours
// Display the new timeout
out.println("The newly assigned
timeout is " +
session.getMaxInactiveInterval());
out.println("</BODY></HTML>");
}
}
Another way
import
java.io.*;
import
java.lang.*;
import
java.sql.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public
class ses extends HttpServlet implements Servlet
{
protected void doGet(HttpServletRequest
request, HttpServletResponse response)throws ServletException, IOException
{
HttpSession
session=request.getSession(true);
Integer
cnt=(Integer)session.getAttribute("mycnter");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if(cnt==null)
{
cnt=new Integer(0);
out.print("Welcome.....");
}
else
{
String
uname=request.getParameter("tname");
Cookie cookie = new
Cookie("uname",uname);
cookie.setMaxAge(365*24*60*60);
response.addCookie(cookie);
out.println("cookie
Added");
if(uname!= null)
session.setAttribute("who",uname);
else uname
=(String)session.getAttribute("who");
cnt=new Integer(cnt.intValue()+1);
out.print("<h1>Welcome
back"+uname+"<h1>");
out.println("This is your visit
no. "+cnt+"<BR>");
}
session.setAttribute("mycnter",cnt);
}
}