B.Tech CS
Object Oriented Techniques
Practical File
Sudeep Singh
Syntax Of Classes
♦♦ Simple class ♦♦
<class- keyword> <class- name>
{
<method- access specifier> <type- return/non_return> <method- name>()
{ operations }
}
♦♦ static top most class ♦♦
<static- keyword><class- keyword> <class- name>
{
<method- access specifier> <type- return/non_return> <method- name>()
{ operations }
}
♦♦ main class ♦♦
<class- keyword> <class- name>
{
public static void main (String [ ]s )
{
System.out.println(“Welcome In BHABHA GROUP OF INSTITUTION”) ;
}
}
♦♦1 program to add two number ♦♦
class sum
{
public static void main(String[]s)
{
int a,b,c;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
c=a+b;
System.out.println("sum = "+c);
} }
♦♦2 program to multiply two number ♦♦
class multi
{
public static void main(String[]s)
{
int a,b,c;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
c=a*b;
System.out.print("sum = "+c);
}
}
♦♦3 program to print factorial of any number ♦♦
class factorial
{
public static void main(String[]s)
{
int a,f=1;
a=Integer.parseInt(s[0]);
for(int i=a;i>=1;i--)
{ f=f*i; }
System.out.println("Factorial of n = "+f);
} }
♦♦4 program to find number is palindrom or not♦♦
class palindrom
{
public static void main(String[]s)
{
int a,b,c=0,d;
a=Integer.parseInt(s[0]);
d=a;
do
{ b=a%10;
c=c*10+b;
a=a/10; }
while(a>0);
if(d==c)
System.out.println("number is palindrom "+d);
else System.out.println("number is not palindrom "+d); } }
♦♦5 program to find out no.is Armstrong or not ♦♦
class armstrong
{
public static void main(String[]s)
{
int a,b,c=0,d;
a=Integer.parseInt(s[0]);
{
d=a;
do
{ b=a%10;
c=c+b*b*b;
a=a/10; }
while(a>0);
if(d==c)
System.out.println("number is armstrong "+d);
else System.out.println("number is not armstrong "+d); } } }
♦♦6 program to find out even & odd number ♦♦
class number
{
public static void main(String[]s)
{
int a,b;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
for(int i=a;i<=b;i++)
{ if(i%2==0)
System.out.println("Number is even = "+i);
else System.out.println("Number is odd = "+i); } } }
♦♦7 program to print the table from “x” to “y”♦♦
class table
{
public static void main(String []s)
{
int a,b;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
if(a<=b)
{
for(int i=a;i<=b;i++)
{
for(int j=1;j<=10;j++)
{
System.out.print(j*i+" ");
}
System.out.println(" ");
}}
else
{
for(int i=b;i<=a;i++)
{
for(int j=1;j<=10;j++)
{
System.out.print(j*i+" ");
}
System.out.println(" ");
}
}
}
}
♦♦ 8 program to print no. in various format ♦♦
class print
{
public static void main(String[]s)
{
int a,b;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
for(int i=a;i<=b;i++)
{
for(int j=a;j<=i;j++)
{
System.out.print(" "+j);
}
System.out.println(" ");
} System.out.println(" ");
for(int i=b;i>=a;i--)
{
for(int j=a;j<=i;j++)
{
System.out.print(" "+j);
}
System.out.println(" ");
}
System.out.println(" ");
for(int i=b;i>=a;i--)
{
for(int j=b;j>=i;j--)
{
System.out.print(" "+j);
}
System.out.println(" ");
}
System.out.println(" ");
for(int i=a;i<=b;i++)
{
for(int j=b;j>=i;j--)
{
System.out.print(" "+j);
}
System.out.println(" ");
}
System.out.println(" ");
for(int i=a;i<=b;i++)
{
for(int j=i;j>=a;j--)
{
System.out.print(" "+j);
}
System.out.println(" ");
} System.out.println(" ");
for(int i=b;i>=a;i--)
{
for(int j=i;j>=a;j--)
{
System.out.print(" "+j);
} System.out.println(" ");
} } }
♦♦ 9 program Of Overriding ♦♦
class x
{
public void test(int a,int b )
{
int c= a + b ;
System.out.println("from class x sum ="+c);
}
}
class y extends x
{
public void test(int a,int b )
{
int c= a * b ;
System.out.println("from class y multiplication ="+c);
}
}
class overriding
{
public static void main(String [ ]s)
{
int a,b;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
y obj=new y( );
obj.test(a,b);
}
}
♦♦ 10 program Of Overloading ♦♦
class x
{
public void test(int a, int b )
{
int c = a * b ;
System.out.println("multiplication of number = "+c);
} }
class y
{
public void test(int a, int b, int d )
{
int c= a + b + d ;
System.out.println("sum of given number = "+c);
} }
class overloading
{
public static void main (String [ ] s)
{
int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
int d=Integer.parseInt(s[2]);
x obj = new x( );
obj.test(a ,b );
y obj1 = new y( );
obj1.test(a ,b ,d );
} }
♦♦11 Communication of class by creating object♦♦
class A
{
public void show( )
{
System.out.println("welcome from A");
}
}
class B
{
public void show1( )
{
A obj=new A( );
obj.show( );
}
}
class C
{
public static void main (String [ ] s)
{
B obj=new B( );
obj.show1( );
}
}
♦♦ 12 Communication of class by inheritance ♦♦
class A
{
public void show( )
{
System.out.println("welcome from A");
} }
class B extends A
{
public void show1( )
{
System.out.println("welcome from B");
} }
class D
{
public static void main (String [ ] s)
{
B obj = new B( );
obj.show( );
} }
♦ 13 Area of circle & rectangle using object ♦
class circle
{
public void area_c(int r )
{
int ar;
ar=r*r*22/7;
System.out.println("Area of circle= "+ar);
}
}
class rect
{
public void area_rec(int le, int br )
{
int ar;
ar=le*br;
System.out.println("Area of rectangle= "+ar);
}
}
class area
{
public static void main (String [ ] s)
{
int r=Integer.parseInt(s[0]);
int le=Integer.parseInt(s[1]);
int br=Integer.parseInt(s[2]);
circle obj=new circle( );
obj.area_c(r );
rect obj1=new rect( );
obj1.area_rec(le, br );
}
}
♦♦14 use of abstract class ♦♦
abstract class test
{
abstract public void show( );
{ }
}
class help extends test
{
public void show( )
{
System.out.println(" from class help ");
} }
class take extends test
{
public void show( )
{
System.out.println(" from class take ");
} }
class give extends test
{
public void show( )
{
System.out.println(" from class give ");
} }
class abstractdemo
{
public static void main (String [ ] s)
{
help obj=new help( );
obj.show( );
take obj1=new take( );
obj1.show( );
give obj2=new give( );
obj2.show( ); } }
♦♦ 15 use of package ♦♦
packagea;
public class a1
{ public void sum(inta, int b)
{ int c= a + b;
System.out.println("sum="+c); } }
packagea;
public class a5
{ public void fact(int a, int b)
{for(inti=a;i<=b;i++)
{ int f=1;
for(intj=i;j>=1;j--)
{ f=f*j; }
System.out.print("Factof "+i);
System.out.println("="+f); } } }
packagea;
public class a4
{ public void table(int a,int b)
{ for(int i=a;i<=b;i++)
{ System.out.print("tableof "+i);
System.out.print("=");
for(intj=1;j<=10;j++)
{ System.out.print(" "+j*i); }
System.out.println(" "); } } }
packagea;
public class a3
{ public void mul(int a, int b)
{ int c= a * b;
System.out.println("mul="+c); } }
packagea;
public class a2
{ public void sub(inta, int b)
{ if(a>b)
{ int c= a - b;
System.out.println("sub ="+c); }
else
{ int c= b - a;
System.out.println("sub ="+c); } } }
import a.*;
class cal
{ public static void main(String []s)
{ int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
a1 obj=new a1();
obj.sum(a,b);
a2 obj1=new a2();
obj1.sub(a,b);
a3 obj3=new a3();
obj3.mul(a,b);
a4 obj4=new a4();
obj4.table(a,b);
a5 obj5=new a5();
obj5.fact(a,b); } }
♦♦ 16 “try” and “catch” block ♦♦
class ab
{
public static void main(String [ ]s)
{
try
{
int i=Integer.parseInt(s[0]);
int j=Integer.parseInt(s[1]);
int k=i/j;
System.out.print("result="+k);
}
catch(Exception e)
{System.out.print(e);}
}
}
♦♦17 Userthread by extending Thread class ♦♦
class abc extends Thread
{
public abc()
{
start();
}
public void run()
{
try
{
for(int i=5;i<=9;i++)
{ System.out.print("table of "+i);
System.out.print("=");
for(int j=1;j<=10;j++)
{ System.out.print(" "+j*i); }
System.out.println(" ");
sleep(500);
}
}
catch(Exception e)
{System.out.print(e);}
}
}
class test
{
public static void main (String []s)
{
try
{
abc obj=new abc();
}
catch(Exception e)
{System.out.print(e);}
} }
♦♦18 multiplethread with join(); ♦♦
class sud extends Thread
{
String name;
public sud(String name)
{
this.name=name;
start();
}
public void run()
{
try
{
for(int i=0;i<5;i++)
{
System.out.println("name =" +" " +name +"=" +i);
sleep(500);
}
}
catch(Exception e)
{System.out.print(e);}
}
}
class main
{
public static void main(String []s)
{
try
{
sud one=new sud("first ");
sud sec=new sud("second");
sud third=new sud("third ");
sec.join();
one.join();
third.join();
for(int i=0;i<5;i++)
{
System.out.println("main = " +i);
}
}
catch(Exception e)
{System.out.print(e);}
}
}
♦♦19 Take input until char= ’z’ in io-package ♦♦
import java.io.*;
classabc
{
public static void main(String []s)
{
char c;
try
{
BufferedReader B=new BufferedReader(new
InputStreamReader(System.in));
do
{
System.out.print("enter until z =");
c=(char)B.read();
}
while(c!= 'z');
}
catch(Exception e)
{System.out.print(e);}
}
}
♦♦20 Take input until string=’off’ in io package
import java.io.*;
classabc
{
public static void main(String []s)
{
String l;
try
{
BufferedReader B=new BufferedReader(new
InputStreamReader(System.in));
do
{
l=B.readLine();
System.out.print("enter until off =");
if(l.equals ("off"))
{break;}
}
while(true);
}
catch(Exception e)
{System.out.print(e);}
}
}
♦♦21 Open a file in read mode in io-package ♦♦
import java.io.*;
class file
{
public static void main(String []s)
{
try
{
FileInputStream B=new FileInputStream("sud.txt");
while (B.read()!=-1)
{
char c=(char)B.read();
System.out.print(c);
}
}
catch(Exception e)
{System.out.print(e);}
}
}
♦♦22insert value in database ♦♦
importjava.sql.*;
class insert
{
public static void main(String []s)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("Jdbc:Odbc:Contact_detail");
PreparedStatementpst=con.prepareStatement("insert into
values('abc' ,+918009000260,'abc@gmail.com')");
pst.close();
con.close();
}
catch(Exception e)
{System.out.print(e);}
}
}
♦♦23featch value from database ♦♦
importjava.sql.*;
classfeatch
{
public static void main(String []s1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("Jdbc:Odbc:Contact_detail");
Statement smt=con.createStatement();
ResultSetrs=smt.executeQuery("select * from emp");
while(rs.next())
{
int a=rs.getInt(1);
String s=rs.getString(2);
System.out.print(a +" " +s);
} }
catch(Exception e)
{System.out.print(e);}
}}
<HTML>
<from method=”get” action=”demo”>Enter the number : -
<input type="text" name="number"/><br>
<input type="submit" name="submit"/><br>
</form></html>
♦♦24Display Welcome with user Servlat♦♦
import java.io.*;
importjavax.servlet.*;
public class demo extends GenericServlet
{
publi void service(ServletRequestreq,ServletResponse res)
throws ServletException, IOexception
{
res.setContent Type("text/HTML");
PrintWriter out=res.getWriter();
for(inti=0;i<10;i++)
{
out.print("Welcome");
} } }
♦25receive data from HTML and cheak even/odd ♦
import java.io.*;
importjavax.servlet.*;
public class demo extends GenericServlet
{
publi void service(ServletRequestreq,ServletResponse res)
throws ServletException, IOexception
{
res.setContent Type("text/HTML");
PrintWriter out=res.getWriter();
int a=Interger.ParseInt(req.getParameter("number"));
if(a%2==0)
<html><form method="get" action="first">
Enter Name:-<input type="text" name="name"/><br><br>
Enter Password :- <input type="password" name="password"/><br><br>
<input type="submit" name="submit"/><br>
</form></html>
import java.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public class firstextends GenericServlet
{
public void service(ServletRequestreq,ServletResponseres) throws
ServletException, IoException
{
RequestDispatcherrd=req.getRequestDispatcher("home")
res.setContentType("text/HTML");
PrintWriter out=res.getWriter();
String name=(String)req.getParameter("name");
String password=(String)req.getParameter("password");
if (name.equals("sudeep") &&password.equals("abc123"))
rd.forword(req, res)
out.print("Number is even = "+a);
else
out.print("Number is odd = "+a);
}}
♦♦ 26passing values between servlet ♦♦
“login.html”
“first.java”
import java.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public class firstextends GenericServlet
{
publi void service(ServletRequestreq,ServletResponseres) throws
ServletException, IOexception
{
res.setContentType("text/HTML");
PrintWriter out=res.getWriter();
String n=(String)req.getAttribute("name");
String p=(String)req.getAttribute("password");
out.print("name="+n +" " +"password="+p));
}
}
“home.java”

Java Program

  • 1.
    B.Tech CS Object OrientedTechniques Practical File Sudeep Singh
  • 2.
    Syntax Of Classes ♦♦Simple class ♦♦ <class- keyword> <class- name> { <method- access specifier> <type- return/non_return> <method- name>() { operations } } ♦♦ static top most class ♦♦ <static- keyword><class- keyword> <class- name> { <method- access specifier> <type- return/non_return> <method- name>() { operations } } ♦♦ main class ♦♦ <class- keyword> <class- name> { public static void main (String [ ]s ) { System.out.println(“Welcome In BHABHA GROUP OF INSTITUTION”) ; } }
  • 3.
    ♦♦1 program toadd two number ♦♦ class sum { public static void main(String[]s) { int a,b,c; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); c=a+b; System.out.println("sum = "+c); } } ♦♦2 program to multiply two number ♦♦ class multi { public static void main(String[]s) { int a,b,c; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); c=a*b; System.out.print("sum = "+c); } }
  • 4.
    ♦♦3 program toprint factorial of any number ♦♦ class factorial { public static void main(String[]s) { int a,f=1; a=Integer.parseInt(s[0]); for(int i=a;i>=1;i--) { f=f*i; } System.out.println("Factorial of n = "+f); } } ♦♦4 program to find number is palindrom or not♦♦ class palindrom { public static void main(String[]s) { int a,b,c=0,d; a=Integer.parseInt(s[0]); d=a; do { b=a%10; c=c*10+b; a=a/10; } while(a>0); if(d==c) System.out.println("number is palindrom "+d); else System.out.println("number is not palindrom "+d); } }
  • 5.
    ♦♦5 program tofind out no.is Armstrong or not ♦♦ class armstrong { public static void main(String[]s) { int a,b,c=0,d; a=Integer.parseInt(s[0]); { d=a; do { b=a%10; c=c+b*b*b; a=a/10; } while(a>0); if(d==c) System.out.println("number is armstrong "+d); else System.out.println("number is not armstrong "+d); } } } ♦♦6 program to find out even & odd number ♦♦ class number { public static void main(String[]s) { int a,b; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); for(int i=a;i<=b;i++) { if(i%2==0) System.out.println("Number is even = "+i);
  • 6.
    else System.out.println("Number isodd = "+i); } } } ♦♦7 program to print the table from “x” to “y”♦♦ class table { public static void main(String []s) { int a,b; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); if(a<=b) { for(int i=a;i<=b;i++) { for(int j=1;j<=10;j++) { System.out.print(j*i+" "); } System.out.println(" "); }} else { for(int i=b;i<=a;i++) { for(int j=1;j<=10;j++) { System.out.print(j*i+" "); } System.out.println(" "); } } } }
  • 7.
    ♦♦ 8 programto print no. in various format ♦♦ class print { public static void main(String[]s) { int a,b; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); for(int i=a;i<=b;i++) { for(int j=a;j<=i;j++) { System.out.print(" "+j); } System.out.println(" "); } System.out.println(" "); for(int i=b;i>=a;i--) { for(int j=a;j<=i;j++) { System.out.print(" "+j); } System.out.println(" "); } System.out.println(" "); for(int i=b;i>=a;i--) { for(int j=b;j>=i;j--) { System.out.print(" "+j);
  • 8.
    } System.out.println(" "); } System.out.println(" "); for(inti=a;i<=b;i++) { for(int j=b;j>=i;j--) { System.out.print(" "+j); } System.out.println(" "); } System.out.println(" "); for(int i=a;i<=b;i++) { for(int j=i;j>=a;j--) { System.out.print(" "+j); } System.out.println(" "); } System.out.println(" "); for(int i=b;i>=a;i--) { for(int j=i;j>=a;j--) { System.out.print(" "+j); } System.out.println(" "); } } }
  • 9.
    ♦♦ 9 programOf Overriding ♦♦ class x { public void test(int a,int b ) { int c= a + b ; System.out.println("from class x sum ="+c); } } class y extends x { public void test(int a,int b )
  • 10.
    { int c= a* b ; System.out.println("from class y multiplication ="+c); } } class overriding { public static void main(String [ ]s) { int a,b; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); y obj=new y( ); obj.test(a,b); } } ♦♦ 10 program Of Overloading ♦♦ class x { public void test(int a, int b ) { int c = a * b ; System.out.println("multiplication of number = "+c); } } class y { public void test(int a, int b, int d )
  • 11.
    { int c= a+ b + d ; System.out.println("sum of given number = "+c); } } class overloading { public static void main (String [ ] s) { int a=Integer.parseInt(s[0]); int b=Integer.parseInt(s[1]); int d=Integer.parseInt(s[2]); x obj = new x( ); obj.test(a ,b ); y obj1 = new y( ); obj1.test(a ,b ,d ); } } ♦♦11 Communication of class by creating object♦♦ class A { public void show( ) { System.out.println("welcome from A"); } } class B { public void show1( )
  • 12.
    { A obj=new A(); obj.show( ); } } class C { public static void main (String [ ] s) { B obj=new B( ); obj.show1( ); } } ♦♦ 12 Communication of class by inheritance ♦♦ class A { public void show( ) { System.out.println("welcome from A"); } } class B extends A { public void show1( ) { System.out.println("welcome from B"); } }
  • 13.
    class D { public staticvoid main (String [ ] s) { B obj = new B( ); obj.show( ); } } ♦ 13 Area of circle & rectangle using object ♦ class circle { public void area_c(int r ) { int ar; ar=r*r*22/7; System.out.println("Area of circle= "+ar); } } class rect {
  • 14.
    public void area_rec(intle, int br ) { int ar; ar=le*br; System.out.println("Area of rectangle= "+ar); } } class area { public static void main (String [ ] s) { int r=Integer.parseInt(s[0]); int le=Integer.parseInt(s[1]); int br=Integer.parseInt(s[2]); circle obj=new circle( ); obj.area_c(r ); rect obj1=new rect( ); obj1.area_rec(le, br ); } } ♦♦14 use of abstract class ♦♦ abstract class test { abstract public void show( ); { } } class help extends test { public void show( ) { System.out.println(" from class help "); } }
  • 15.
    class take extendstest { public void show( ) { System.out.println(" from class take "); } } class give extends test { public void show( ) { System.out.println(" from class give "); } } class abstractdemo { public static void main (String [ ] s) { help obj=new help( ); obj.show( ); take obj1=new take( ); obj1.show( ); give obj2=new give( ); obj2.show( ); } }
  • 16.
    ♦♦ 15 useof package ♦♦ packagea; public class a1 { public void sum(inta, int b) { int c= a + b; System.out.println("sum="+c); } } packagea; public class a5 { public void fact(int a, int b) {for(inti=a;i<=b;i++) { int f=1; for(intj=i;j>=1;j--) { f=f*j; } System.out.print("Factof "+i); System.out.println("="+f); } } } packagea; public class a4 { public void table(int a,int b) { for(int i=a;i<=b;i++) { System.out.print("tableof "+i); System.out.print("="); for(intj=1;j<=10;j++) { System.out.print(" "+j*i); } System.out.println(" "); } } } packagea; public class a3 { public void mul(int a, int b) { int c= a * b; System.out.println("mul="+c); } } packagea; public class a2 { public void sub(inta, int b) { if(a>b) { int c= a - b; System.out.println("sub ="+c); } else { int c= b - a; System.out.println("sub ="+c); } } }
  • 17.
    import a.*; class cal {public static void main(String []s) { int a=Integer.parseInt(s[0]); int b=Integer.parseInt(s[1]); a1 obj=new a1(); obj.sum(a,b); a2 obj1=new a2(); obj1.sub(a,b); a3 obj3=new a3(); obj3.mul(a,b); a4 obj4=new a4(); obj4.table(a,b); a5 obj5=new a5(); obj5.fact(a,b); } }
  • 18.
    ♦♦ 16 “try”and “catch” block ♦♦ class ab { public static void main(String [ ]s) { try { int i=Integer.parseInt(s[0]); int j=Integer.parseInt(s[1]); int k=i/j; System.out.print("result="+k); } catch(Exception e) {System.out.print(e);} } }
  • 19.
    ♦♦17 Userthread byextending Thread class ♦♦ class abc extends Thread { public abc() { start(); } public void run() { try { for(int i=5;i<=9;i++) { System.out.print("table of "+i); System.out.print("="); for(int j=1;j<=10;j++) { System.out.print(" "+j*i); } System.out.println(" "); sleep(500); } } catch(Exception e) {System.out.print(e);} } } class test { public static void main (String []s) { try { abc obj=new abc(); } catch(Exception e) {System.out.print(e);} } }
  • 20.
    ♦♦18 multiplethread withjoin(); ♦♦ class sud extends Thread { String name; public sud(String name) { this.name=name; start(); } public void run() { try { for(int i=0;i<5;i++) { System.out.println("name =" +" " +name +"=" +i); sleep(500); } } catch(Exception e) {System.out.print(e);} } } class main { public static void main(String []s) { try { sud one=new sud("first "); sud sec=new sud("second"); sud third=new sud("third "); sec.join(); one.join(); third.join(); for(int i=0;i<5;i++) { System.out.println("main = " +i); } } catch(Exception e) {System.out.print(e);} } }
  • 22.
    ♦♦19 Take inputuntil char= ’z’ in io-package ♦♦ import java.io.*; classabc { public static void main(String []s) { char c; try { BufferedReader B=new BufferedReader(new InputStreamReader(System.in)); do { System.out.print("enter until z ="); c=(char)B.read(); } while(c!= 'z'); } catch(Exception e) {System.out.print(e);} } }
  • 23.
    ♦♦20 Take inputuntil string=’off’ in io package import java.io.*; classabc { public static void main(String []s) { String l; try { BufferedReader B=new BufferedReader(new InputStreamReader(System.in)); do { l=B.readLine(); System.out.print("enter until off ="); if(l.equals ("off")) {break;} } while(true); } catch(Exception e) {System.out.print(e);} } }
  • 24.
    ♦♦21 Open afile in read mode in io-package ♦♦ import java.io.*; class file { public static void main(String []s) { try { FileInputStream B=new FileInputStream("sud.txt"); while (B.read()!=-1) { char c=(char)B.read(); System.out.print(c); } } catch(Exception e) {System.out.print(e);} } }
  • 25.
    ♦♦22insert value indatabase ♦♦ importjava.sql.*; class insert { public static void main(String []s) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:Contact_detail"); PreparedStatementpst=con.prepareStatement("insert into values('abc' ,+918009000260,'abc@gmail.com')"); pst.close(); con.close(); } catch(Exception e) {System.out.print(e);} } } ♦♦23featch value from database ♦♦ importjava.sql.*; classfeatch { public static void main(String []s1) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:Contact_detail"); Statement smt=con.createStatement(); ResultSetrs=smt.executeQuery("select * from emp"); while(rs.next()) { int a=rs.getInt(1); String s=rs.getString(2); System.out.print(a +" " +s); } } catch(Exception e) {System.out.print(e);} }}
  • 26.
    <HTML> <from method=”get” action=”demo”>Enterthe number : - <input type="text" name="number"/><br> <input type="submit" name="submit"/><br> </form></html> ♦♦24Display Welcome with user Servlat♦♦ import java.io.*; importjavax.servlet.*; public class demo extends GenericServlet { publi void service(ServletRequestreq,ServletResponse res) throws ServletException, IOexception { res.setContent Type("text/HTML"); PrintWriter out=res.getWriter(); for(inti=0;i<10;i++) { out.print("Welcome"); } } } ♦25receive data from HTML and cheak even/odd ♦ import java.io.*; importjavax.servlet.*; public class demo extends GenericServlet { publi void service(ServletRequestreq,ServletResponse res) throws ServletException, IOexception { res.setContent Type("text/HTML"); PrintWriter out=res.getWriter(); int a=Interger.ParseInt(req.getParameter("number")); if(a%2==0)
  • 27.
    <html><form method="get" action="first"> EnterName:-<input type="text" name="name"/><br><br> Enter Password :- <input type="password" name="password"/><br><br> <input type="submit" name="submit"/><br> </form></html> import java.io.*; importjavax.servlet.*; importjavax.servlet.http.*; public class firstextends GenericServlet { public void service(ServletRequestreq,ServletResponseres) throws ServletException, IoException { RequestDispatcherrd=req.getRequestDispatcher("home") res.setContentType("text/HTML"); PrintWriter out=res.getWriter(); String name=(String)req.getParameter("name"); String password=(String)req.getParameter("password"); if (name.equals("sudeep") &&password.equals("abc123")) rd.forword(req, res) out.print("Number is even = "+a); else out.print("Number is odd = "+a); }} ♦♦ 26passing values between servlet ♦♦ “login.html” “first.java”
  • 28.
    import java.io.*; importjavax.servlet.*; importjavax.servlet.http.*; public classfirstextends GenericServlet { publi void service(ServletRequestreq,ServletResponseres) throws ServletException, IOexception { res.setContentType("text/HTML"); PrintWriter out=res.getWriter(); String n=(String)req.getAttribute("name"); String p=(String)req.getAttribute("password"); out.print("name="+n +" " +"password="+p)); } } “home.java”