Sunteți pe pagina 1din 30

JAVA LAB FILE

Submited To Manoj Rajora sir

NOVEMBER 20, 2019


IPEM
Your obedient student Rajesh Pathak
JAVA LAB FILE

Name: Rajesh Pathak


Class: B.C.A 5thsem
Section : B
Roll no: 1709101800

Submitted to : Manoj Rajora Sir

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 1
INDEX
PROGRAM 1 - Enter 2 Numbers & display sum of both. 4

PROGRAM 2 - Enter one Number & display table for that 4

PROGRAM 3 - Enter 1 Numbers & display in reverse order 5

PROGRAM 4 - Enter 1 Numbers & display in words, without using if else & switch 5

PROGRAM 5 - print 1 to 50 prime with loop Label 6

PROGRAM 6 - Enter 2 numbers & display b/w prime numbers - in ascending order 6

PROGRAM 7 - default Constructor example 7

PROGRAM 8 - user define Constructor with Constructor overloading example 7

PROGRAM 9 - this operator Example, differentiate b/w global & argument variable 8

PROGRAM 10 - this operator Example, differentiate b/w Local & global variable 8

PROGRAM 11 - inheritance with method overriding example 9

PROGRAM 12 - super class variable calling 9

PROGRAM 13 - final variable example 10

PROGRAM 14 - final method example 10

PROGRAM 15 - Package program example, to create package with tarun name 11

PROGRAM 16 - with Exception handling – using single catch block 11

PROGRAM 17 - Thread Program for control the main Thread 12

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 2
PROGRAM 18 - Array Program- input 10 values & display using array. 12

PROGRAM 19 - Vector Program with Generic Collection with String type 13

PROGRAM 20 - Applet Program for display Hello message with formatted Text. 14

PROGRAM 21 - face Drawing program using Applet 14

PROGRAM 22 - FlowLayout program using Applet 15

PROGRAM 23 - GridLayout program using Applet 16

PROGRAM 24 - Login Page using Custom Layout.. 16

PROGRAM 25 - Calc Program using Custom Layout with event Handling. 17

PROGRAM 26 - Calc Program using Custom Layout with Frame & event Handling. 19

PROGRAM 27 - Socket Programming UDP Server Program. 22

PROGRAM 28 - Socket Programming - UDP Client Program. 23

PROGRAM 29 - Socket Programming – for get the machine address 24

PROGRAM 30 - Socket Programming - TCP Server Program. 24

PROGRAM 31 - Socket Programming - TCP Client Program. 25

PROGRAM 32 - JDBC Program for Insert Record in database table 25

PROGRAM 33 - JDBC Program for Update& delete Record in database table 26

PROGRAM 34 - JDBC Program for fetch Record from database table 27

PROGRAM 35 - Servlets Program. 28

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 3
Program 1 - Enter 2 Numbers & display sum of both
import java.util.Scanner;
class Sum
{
public static void main(String aa[])
{
int a,b,c;
Scanner r = new Scanner(System.in);
System.out.print("Plz Enter 2 Numbers : ");
a = r.nextInt(); // for 1st Num
b = r.nextInt(); // for 2nd Num
c = a + b;
System.out.printf("Sum is = %d",c);
}
}

we can perform Runtime inputs in java with the help of Scanner class, stored inside
java.util package & add in jdk1.5 for Runtime inputs.

Scanner r = new Scanner(System.in);


int nextInt() - for input int value
float nextFloat() - for input float value
long nextInt() - for input long value
double nextDouble() - for input double value
String next() - for input next word only
String nextLine() - for input whole line

//Program 2 - Enter one Number & display table for that


import java.util.Scanner;
class Table
{
public static void main(String aa[])
{
int a,c,x;
Scanner r = new Scanner(System.in);
System.out.print("Plz Enter any Number : ");
a = r.nextInt(); // for 1st Num
for(int i=1,j=10;i<=10;i++,j--)
{

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 4
c = a * i;
x = a * j;
System.out.printf("%02d * %02d = %03d \t %02d * %02d = %03d\n",a,i,c,a,j,x);
}
}
}

// Program 3 - Enter 1 Numbers & display in reverse order


// input - 1234 , output - 4321 , logic - %10, / 10

import java.util.Scanner;
class Rev
{
public static void main(String aa[]) //- with command line arguments.
{
Scanner r = new Scanner(System.in);
System.out.print("Plz enter any Number : ");
int x, a = r.nextInt(); // for 1st num
System.out.print("Number in Reverse : ");
while(a>0)
{
x = a % 10;
System.out.print(x);
a = a / 10;
}
}
}

// Program 4 - Enter 1 Numbers & display in words, without using if else & switch
// case , input - 1234 , output - one two three four only.

import java.util.Scanner;
class Word
{
public static void main(String aa[]) //- with command line arguments.
{ // 0 1 2 3 4 5 6 7 8 9
String s = "only.";
String ar[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
Scanner r = new Scanner(System.in);
System.out.print("Plz enter any Number : ");
int x, a = r.nextInt(); // for 1st num
while(a>0)
{
x = a % 10;

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 5
s = ar[x]+" "+s;
a = a / 10;
}
System.out.println(s);
}
}

Program 5 - print 1 to 50 prime with loop Label

class Prime1
{
public static void main(String aa[])
{
n: for(int i=1;i<=50;i++) // n is the loop name/label
{
for(int j=2;j<=i/2;j++)
if(i%j==0)
continue n; // contd.. the n loop
System.out.println(i);
}
}
}
__________________________________________________________________________

Program 6 - Enter 2 numbers & display b/w prime numbers - in ascending order

input 10 50 - prime b/w 10 to 50


input 50 10 - prime b/w 10 to 50

import java.util.Scanner;
class Prime2
{
public static void main(String aa[])
{
Scanner r = new Scanner(System.in);
System.out.print("Plz Enter 2 Numbers : ");
int a = r.nextInt(); // 1st Num
int b = r.nextInt(); // 2nd Num
if(a>b) // a = 50 , b = 10
{
a = a + b; // 50 + 10; a = 60
b = a - b; // 60 - 10; b = 50
a = a - b; // 60 - 50; a = 10
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 6
n: for(int i=a;i<=b;i++) // n is the loop label/name
{
for(int j=2;j<=i/2;j++)
if(i%j==0)
continue n; // contd.. the n loop
System.out.println(i);
}
}
}

// Program 7 - default Constructor example


class C1
{
void disp()
{
System.out.println("I m inside C1 class disp...");
}
public static void main(String aa[])
{
C1 o = new C1(); // default Constructor calling
o.disp();
}
}
_____________________________________________________________________
// Program 8- user define Constructor with Constructor overloading example
class C2
{
private int a,b;
private C2() // no argument passing Constructor
{
a = b = 5;
}
private C2(int x) // one argument passing Constructor
{
a = b = x;
}
C2(int x,int y) // two argument passing Constructor
{
a = x;
b = y;
}
void disp()
{
System.out.printf("a = %d , b = %d\n",a,b);
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 7
}
public static void main(String aa[])
{
C2 o = new C2(); // no argument passing Constructor calling
C2 n = new C2(100); // one argument passing Constructor calling
C2 k = new C2(10,20); // two argument passing Constructor calling
o.disp(); // a = 5, b = 5
n.disp(); // a = 100, b = 100
k.disp(); // a = 10, b = 20
}
}

Program9 – this operator Example, differentiate b/w global & argument variable

class Th1
{
int a;
Th1()
{
a = 5;
}
Th1(int a)
{
this.a = a; // currentobject.a = argument.a
}
void disp()
{
System.out.println("a = "+a);
}
public static void main(String aa[])
{
Th1 o = new Th1(); // no argument passing Constructor
Th1 n = new Th1(100); // one argument passing Constructor calling
o.disp(); // a = 5
n.disp(); // a = 100?
}
}
_____________________________________________________________

Program10 – this operator Example, differentiate b/w Local & global variable
class Th2
{
int a = 1000;
void disp()
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 8
{
int a = 100;
System.out.println("a = "+a); // local a
System.out.println("a = "+this.a); // currentobject.a - global of current class
}
public static void main(String aa[])
{
Th2 o = new Th2();
o.disp(); // a = 100/1000
}
}

Program 11 – inheritance with method overriding example


Overriding - if parent & child class has one method with same name & same
signature(argument/s & return type) that is called method overriding
class A
{
void disp()
{
System.out.println("Parent says, I m the Boss....");
}
}
class B extends A
{
void disp() // disp method override
{
System.out.println("Child says, I m the Big Boss....");
}
public static void main(String aa[])
{
B o = new B();
o.disp();
}
}
____________________________________________________________________

Program 12 - super class variable calling

class S
{
int a = 1000;
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 9
class S1 extends S
{
int a = 100;
void disp()
{
int a = 10;
System.out.println("a = "+a); // local a - 10
System.out.println("a = "+this.a); // currentobject.a - global of current class - 100
System.out.println("a = "+super.a); // super class a - 1000
}
public static void main(String aa[])
{
S1 n = new S1();
n.disp();
}
}

Program 13 – final variable example


class F1
{
final int a = 100;
void disp()
{
// a = 10; // error: cannot assign a value to final variable a
System.out.println("a = "+a);
}
public static void main(String aa[])
{
F1 o = new F1();
o.disp();
}
}
__________________________________________________________________
Program 14 – final method example
class F
{
final void disp()
{
System.out.println("Parent says, I m the Boss");
}
}
class F2 extends F
{
/*
// error: disp() in F2 cannot override disp() in F, overridden method is final
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 10
void disp()
{
System.out.println("Child says, I m the Big Boss");
}
*/
public static void main(String aa[])
{
F2 o = new F2();
o.disp();
}
}

Program 15 – Package program example, to create package with adarsh name


package tarun;
public class A
{
protected void disp() // protected data members can access iun sub class
{ // of another package, but not access in non sub class of another package
System.out.println("I m inside tarun.A class disp");
}
public static void main(String aa[])
{
A o = new A();
o.disp();
}
}

Compile with – javac –d . A.java


Run with - java tarun.A

__________________________________________________________________
// Program 16- with Exception handling – using single catch block
// 100% use - this is best algo for exception handling

package my;
class Ex2
{
public static void main(String aa[])
{
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 11
try
{
int a = Integer.parseInt(aa[0]); // NumberFormat + ArrayIndex
int x = 100 / a; // ArithmeticException
System.out.println("x is - "+x);
}
catch(Exception ex) // Exception is the base class of all Exceptions,
{ // it can handle all type of Exception
System.out.println("Exception is - "+ex);
}
System.out.println("Thanxxxxxxxxxxxxxxxxxxx");
}
}

// thanx msg always display

// javac -d . Ex2.java
// java my.Ex2

// Program 17 –Thread Program for control the main Thread


import java.util.*;
class Th2
{
public static void main(String aa[])
{
Thread t = Thread.currentThread();
Calendar c;
for(;;)
{
c = Calendar.getInstance(); // get the current os calendar
System.out.printf("%02d : %02d : %02d\n",
c.get(c.HOUR_OF_DAY),c.get(c.MINUTE),c.get(c.SECOND));
try { t.sleep(1000); }catch(Exception ex){}
}
}
}
______________________________________________________
//Program 18 – Array Program- input 10 values & display using array.
import java.util.*;
class Arr1
{
public static void main(String aa[])
{
int ar[] = new int[5];
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 12
Scanner r = new Scanner(System.in);
for(int i=0;i<ar.length;i++)
{
System.out.printf("Enter value for ar[%d] : ",i);
ar[i] = r.nextInt();
}
System.out.println("Display all Array elements with for each loop - ");
for(int n : ar)
System.out.println(n);
// sort elements with sort() method
Arrays.sort(ar);
System.out.println("after sort - ");
for(int n : ar)
System.out.println(n);
}
}

// Program 19 - Vector Program with Generic Collection with String type


import java.util.*;
class VecG
{
public static void main(String aa[])
{
Vector<String> v = new Vector<String>();
v.add("Deepti");
v.add("Anjali");
v.add("Preeti");
v.add("Kajal");
v.add("Meenakshi");
v.add("Jyoti");
v.add("Kanchan");
v.add(2,"Khushboo"); // add on 2nd index - 0 for 1st element
System.out.println(v);
System.out.println("Capacity is - "+v.capacity());
v.remove(2);
v.remove("Kajal");
v.remove("Meenakshi");
System.out.println(v);
Collections.sort(v);
System.out.println("Display with for loop - ");
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 13
for(int i=0;i<v.size();i++)
System.out.println(v.get(i));
System.out.println("Display with for each loop - ");
for(String n : v)
System.out.println(n);
System.out.println("Display with Iterator - ");
Iterator<String> i = v.iterator();
while(i.hasNext())
System.out.println(i.next());
System.out.println("Display with Enumeration - ");
Enumeration<String> e = v.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
v.clear();
if(v.isEmpty())
System.out.println("ur set is Empty");
}
}

Program 20 – Applet Program for display Hello message with formatted Text.

import java.awt.*;
import java.applet.*;
public class Hello extends Applet
{
public void paint(Graphics g)
{
setBackground(new Color(253,245,198));
g.setColor(new Color(70,0,0));
g.setFont(new Font("Courier New",1,40));
g.drawString("Welcome to Java Applet ",100,100);
g.setColor(new Color(0,70,0));
g.setFont(new Font("Times New Roman",2,24));
g.drawString("Designed by - Aman Verma....",10,200);
}
}
//<applet code=Hello height=300 width=700></applet>
__________________________________________________________________
Program 21 – face Drawing program using Applet

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 14
import java.awt.*;
public class Face extends java.applet.Applet {
public void paint(Graphics g) {
setBackground(Color.pink);
g.drawOval(50,50,200,300);
g.drawOval(70,140,70,25); // left eye
g.drawOval(160,140,70,25); // right eye
g.fillOval(95,142,22,23); // left pupil
g.fillOval(185,142,22,23); // right pupil
g.drawOval(140,180,25,80); // for nosee
g.drawArc(110,250,80,50,200,140); // for mouth
g.drawOval(25,150,25,110); // for left ear
g.drawOval(250,150,25,110); // for right ear
}
}
//<applet code=Face height=400 width=300></applet>

Program 22 – FlowLayout program using Applet


import java.applet.*;
import java.awt.*;
public class Layout1 extends Applet
{
Button b1 = new Button("Click Me");
Button b2 = new Button("Press Me");
Button b3 = new Button("Hit Me");
Button b4 = new Button("Leave Me");
Button b5 = new Button("Cancel Me");
public void init()
{
setFont(new Font("Courier New",0,15));
setBackground(new Color(245,218,167));
setLayout(new FlowLayout(2)); // 0-L / 1-C / 2-R
add(b1);
add(b2);
add(b3);
add(b4);

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 15
add(b5);
}
}
//<applet code=Layout1 height=350 width=500></applet>
_____________________________________________________________________
Program 23 – GridLayout program using Applet
import java.applet.*;
import java.awt.*;
public class Layout2 extends Applet
{
Button b1 = new Button("Click Me");
Button b2 = new Button("Press Me");
Button b3 = new Button("Hit Me");
Button b4 = new Button("Leave Me");
Button b5 = new Button("Cancel Me");
public void init()
{
setFont(new Font("Courier New",0,15));
setBackground(new Color(245,218,167));
setLayout(new GridLayout(2,2,5,5));
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
}
}
//<applet code=Layout2 height=350 width=500></applet>

Program 24 – BorderLayout program using Applet


// BorderLayout
import java.applet.*;
import java.awt.*;
public class Layout3 extends Applet
{
Button b1 = new Button("Click Me");
Button b2 = new Button("Press Me");
Button b3 = new Button("Hit Me");
Button b4 = new Button("Leave Me");
Button b5 = new Button("Cancel Me");
public void init()
{
setFont(new Font("Courier New",0,15));
setBackground(new Color(245,218,167));
setLayout(new BorderLayout(5,5));
add(b1,"North");
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 16
add(b2,"South");
add(b3,"West");
add(b4); // default is center
add(b5,"East");
}
}
//<applet code=Layout3 height=350 width=500></applet>
_____________________________________________________________________
Program 24 - Login Page using Custom Layout..

import java.applet.*;
import java.awt.*;
public class Login extends Applet
{
Button b1 = new Button("Sign In");
Button b2 = new Button("Cancel");
Label a1 = new Label("User Id");
Label a2 = new Label("Password");
TextField t1 = new TextField();
TextField t2 = new TextField();
public void init()
{
setFont(new Font("Courier New",0,15));
setBackground(new Color(245,218,167));
setLayout(null); // remove the default Layout also
a1.setBounds(100,100,90,24);
a2.setBounds(100,150,90,24);
t1.setBounds(200,100,180,24);
t2.setBounds(200,150,140,24);
b1.setBounds(150,200,80,24);
b2.setBounds(240,200,80,24);
t2.setEchoChar('$'); // for set the password field - echo char = display char
add(a1);
add(a2);
add(t1);
add(t2);
add(b1);
add(b2);
}
}
//<applet code=Login height=350 width=500></applet>

______________________________________________________________________
Program 25– Calc Program using Custom Layout with event Handling.

import java.awt.*;
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 17
import java.awt.event.*; // step - 1
public class Calc extends java.applet.Applet implements ActionListener // step - 2
{
Label a1 = new Label("1st Num");
Label a2 = new Label("2nd Num");
Label a3 = new Label("Result");
TextField t1 = new TextField();
TextField t2 = new TextField();
TextField t3 = new TextField();
Button b1 = new Button("Sum");
Button b2 = new Button("Sub");
Button b3 = new Button("Multi");
Button b4 = new Button("Clear");
public void init() {
setLayout(null);
setFont(new Font("Courier New",0,14));
setBackground(new Color(250,210,140));
a1.setBounds(100,70,90,24);
a2.setBounds(100,120,90,24);
a3.setBounds(100,170,90,24);
t1.setBounds(190,70,90,24);
t2.setBounds(190,120,90,24);
t3.setBounds(190,170,90,24);
b1.setBounds(70,220,70,24);
b2.setBounds(150,220,70,24);
b3.setBounds(230,220,70,24);
b4.setBounds(310,220,70,24);
t3.setEditable(false);
add(a1);
add(a2);
add(a3);
add(t1);
add(t2);
add(t3);
add(b1);
add(b2);
add(b3);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int a,b,c;
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 18
try { a = Integer.parseInt(t1.getText()); }catch(Exception ex) { a = 0; }
try { b = Integer.parseInt(t2.getText()); }catch(Exception ex) { b = 0; }
// System.out.println("event source object is - "+e.getSource());
// System.out.println("event Action command is - caption - "+e.getActionCommand());
String cmd = e.getActionCommand();
if(cmd.equals("Sum"))
{
c = a + b;
t3.setText(""+c);
}
if(cmd.equals("Sub"))
{
c = a - b;
t3.setText(""+c);
}
if(cmd.equals("Multi"))
{
c = a * b;
t3.setText(""+c);
}
if(cmd.equals("Clear"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
//<applet code=Calc height=300 width=500></applet>

Program 26– Calc Program using Custom Layout with Frame & event Handling.
import java.awt.*;
import java.awt.event.*;
public class CalcF extends Frame implements ActionListener
{
Label a1 = new Label("1st Num");
Label a2 = new Label("2nd Num");
Label a3 = new Label("Result");
NumField t1 = new NumField();
NumField t2 = new NumField();
TextField t3 = new TextField();
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 19
String ar[] = {"Sum","Sub","Multi","Div","Clear"};
CalcF()
{
setLayout(null);
setBackground(new Color(223,211,170));
setForeground(new Color(70,0,0));
setFont(new Font("Courier New",0,15));
a1.setBounds(100,100,90,24);
a2.setBounds(100,150,90,24);
a3.setBounds(100,200,90,24);
t1.setBounds(190,100,90,24);
t2.setBounds(190,150,90,24);
t3.setBounds(190,200,90,24);
t3.setEditable(false);
add(a1);
add(a2);
add(a3);
add(t1);
add(t2);
add(t3);
for(int i=0,x=50;i<ar.length;i++,x+=75)
{
Button b = new Button(ar[i]);
b.setBounds(x,250,70,24);
add(b);
b.addActionListener(this);
}
setBounds(150,150,500,320);
setVisible(true);
addWindowListener(new Win());
}
class Win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
public void actionPerformed(ActionEvent e)
{
int a,b,c;
try { a = Integer.parseInt(t1.getText()); }catch(Exception ex) { a = 0; }
try { b = Integer.parseInt(t2.getText()); }catch(Exception ex) { b = 0; }
String cmd = e.getActionCommand(); // return button caption - like - Sum , Sub , Multi ,
Div , Clear
if(cmd.equals("Sum"))
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 20
{
c = a + b;
t3.setText(""+c);
}
if(cmd.equals("Sub"))
{
c = a - b;
t3.setText(""+c);
}
if(cmd.equals("Multi"))
{
c = a * b;
t3.setText(""+c);
}
if(cmd.equals("Div"))
{
try {
c = a / b;
t3.setText(""+c);
}catch(Exception ex) {t3.setText("Infinity"); }
}
if(cmd.equals("Clear"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
public static void main(String aa[])
{
new CalcF();
}
}
___________________________________________________________
Program 26– Notepad Program using Frame.
import java.awt.*;
import java.awt.event.*;
class Notepad extends Frame
{
MenuBar mb = new MenuBar();
Menu f1 = new Menu("File");
Menu f2 = new Menu("Edit");
Menu f3 = new Menu("Help");
MenuItem a1 = new MenuItem("New");
MenuItem a2 = new MenuItem("Open");
MenuItem a3 = new MenuItem("Save");
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 21
MenuItem a4 = new MenuItem("Save As");
MenuItem a5 = new MenuItem("Exit");
MenuItem a6 = new MenuItem("Cut");
MenuItem a7 = new MenuItem("Copy");
MenuItem a8 = new MenuItem("Paste");
MenuItem a9 = new MenuItem("Select All");
MenuItem a10 = new MenuItem("Help Topic");

TextArea ta = new TextArea();


Notepad()
{
setTitle("Untitled - Notepad");
setFont(new Font("Courier New",0,15));
f1.add(a1);
f1.addSeparator();
f1.add(a2);
f1.addSeparator();
f1.add(a3);
f1.addSeparator();
f1.add(a4);
f1.addSeparator();
f1.add(a5);
// add item in edit menu
f2.add(a6);
f2.addSeparator();
f2.add(a7);
f2.addSeparator();
f2.add(a8);
f2.addSeparator();
f2.add(a9);
f3.add(a10);

mb.add(f1);
mb.add(f2);
mb.add(f3);
setMenuBar(mb);
add(ta);
setBounds(10,10,1000,600);
setVisible(true);
addWindowListener(new Win());
}
class Win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ dispose(); }
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 22
public static void main(String aa[])
{
new Notepad();
}
}
__________________________________________________________________
Program 27– Socket Programming UDP Server Program.

import java.net.*;
class UDPSer
{
public static void main(String aa[])
{
byte msg[];
DatagramPacket pac;
try
{
int ECHO = 7; // for msg send & receive
DatagramSocket s = new DatagramSocket(ECHO); // server socket design with echo Port
while(true)
{
// for receive
msg = new byte[50];
pac = new DatagramPacket(msg,50);
s.receive(pac); // msg received
System.out.println("Client : "+new String(msg));
// for send
msg = new byte[50];
System.out.print("Enter : ");
System.in.read(msg); // msg runtime input
pac = new DatagramPacket(msg,50,pac.getAddress(),pac.getPort());
s.send(pac); // msg send
}
}
catch(Exception ex) { System.out.println(ex);}
}
}

Program 28– Socket Programming - UDP Client Program.


import java.net.*;
class UDPCli
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 23
{
public static void main(String aa[])
{
byte msg[];
DatagramPacket pac;
try
{
int ECHO = 7; // for msg send & receive
DatagramSocket s = new DatagramSocket(); // client socket design
InetAddress ad = InetAddress.getByName("72.168.0.95"); // current machine address
while(true)
{
// for send
msg = new byte[50];
System.out.print("Enter : ");
System.in.read(msg); // msg runtime input
pac = new DatagramPacket(msg,50,ad,ECHO);
s.send(pac); // msg send
// for receive
msg = new byte[50];
pac = new DatagramPacket(msg,50);
s.receive(pac); // msg received
System.out.println("Server : "+new String(msg));
}
}
catch(Exception ex) { System.out.println(ex);}
}
}
___________________________________________________________________

// Program 29– Socket Programming – for get the machine address


import java.net.*;
class Addr
{
public static void main(String aa[]) throws Exception
{
InetAddress ad = InetAddress.getLocalHost(); // - return current Machine Address
System.out.println(ad);
InetAddress ad1 = InetAddress.getByName("www.yahoo.com"); // - return LAN/WAN
System.out.println(ad1);
InetAddress ad2[] = InetAddress.getAllByName("www.google.com"); // Machine all IP
for(int i=0;i<ad2.length;i++)
System.out.println(ad2[i]);
}
}
Program 30– Socket Programming - TCP Server Program.
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 24
import java.io.*;
import java.util.*;
import java.net.*;
class TCPSer
{
public static void main(String aa[])
{
String msg;
try
{
ServerSocket ser = new ServerSocket(4001); // server socket design with 4001 port
Socket s = ser.accept(); // to accept the client request
while(true)
{
// for client input
DataInputStream in = new DataInputStream(s.getInputStream());
// for client output
DataOutputStream out = new DataOutputStream(s.getOutputStream());
// for receive
msg = in.readLine(); // memory allocate for msg
System.out.println("Server : "+msg);
// for send
System.out.print("Enter: ");
Scanner r = new Scanner(System.in);
msg = r.nextLine(); // msg runtime input
out.writeBytes(msg+"\n"); // send to client
out.flush(); // clear the out buffer
}
}
catch(Exception ex) { System.out.println("Exception is - "+ex); }
}
}
______________________________________________________________
Program 31– Socket Programming - TCP Client Program.
import java.io.*;
import java.util.*;
import java.net.*;
class TCPCli
{
public static void main(String aa[])
{
String msg;
try
{
Socket s = new Socket("192.168.1.8",4001); // client socket design with 4001 port
while(true)
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 25
{
// for server input
DataInputStream in = new DataInputStream(s.getInputStream());
// for server output
DataOutputStream out = new DataOutputStream(s.getOutputStream());
// for send
System.out.print("Enter: ");
Scanner r = new Scanner(System.in);
msg = r.nextLine(); // msg runtime input
out.writeBytes(msg+"\n"); // send to client
out.flush(); // clear the out buffer
// for receive
msg = in.readLine(); // memory allocate for msg
System.out.println("Client : "+msg);
}
}
catch(Exception ex) { System.out.println("Exception is - "+ex); }
}
}
_____________________________________________________________________
Program 32–JDBC Program for Insert Record in database table

import java.sql.*;
class Data1
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
String sql = "insert into tblEmp values(1002,'Mr. Abhishek .',22000,'23-12-1994')";
int x = s.executeUpdate(sql);
System.out.printf(x+" Record Successfully Inserted");
}catch(Exception ex) { System.out.println(ex); }
}
}
// download the ojdbc14.jar & paste inside own folder
// terminal - jar -xf ojdbc14.jar
Javac Data1.java

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 26
Java Data1

Program 33– JDBC Program for Update Record in database table


import java.sql.*;
class Data3
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
String sql = "update tblemp set sal = sal+sal*.2 where empno=1002";
int x = s.executeUpdate(sql);
System.out.println(x+" Record Successfully Updated");
}catch(Exception ex) { System.out.println(ex); }
}
}
Program 33– JDBC Program for Delete Record from database table

import java.sql.*;
class Data4
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
String sql = "delete from tblemp where empno=1001";
int x = s.executeUpdate(sql);
System.out.println(x+" Record Successfully Deleted");
}catch(Exception ex) { System.out.println(ex); }
}

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 27
}

Program 34– JDBC Program for fetch Record from database table
import java.sql.*;
class Data1
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select empno,ename,sal,to_char(dob,'dd-Mon-yyyy') as
dob from tblemp");
ResultSetMetaData mt = rs.getMetaData(); // mt hold the rs meta data
for(int i=1;i<=mt.getColumnCount();i++)
System.out.printf("%-10s | ",mt.getColumnName(i));
while(rs.next())
{
System.out.println();
for(int i=1;i<=mt.getColumnCount();i++)
System.out.printf("%-10s | ",rs.getString(i));
}
}catch(Exception ex) { System.out.println(ex); }
}
}
______________________________________________________________________
Program 35– Servlets Program..
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 28
{
String u = (String)req.getAttribute("id");
PrintWriter out = res.getWriter(); // out hold the response writer for response write
out.println("<body bgcolor=lightyellow><pre><h1>Hello "+u+", Welcome to
Servlets</h1>");
}
}

Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 29

S-ar putea să vă placă și