0% found this document useful (0 votes)
4 views88 pages

Java Programming Lab-2

Uploaded by

anubhavth537
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views88 pages

Java Programming Lab-2

Uploaded by

anubhavth537
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programming Lab

Name : Anubhav thakur


Branch : IT – 3
Roll No : 03476803121

Submitted to : Mr. Gaurav Sandhu

Java Programming Lab


anubhav
IT-3
03476803121
Page 1
Index

Sno Practical Date Remarks


1 Design a java program 23 – March - 2023
to find the biggest of
three given integers.

2 Design a java 23 – March - 2023


program to define a
class, describe its
constructor, over-
load the constructor
and instantiate the
object.

3 Design a java 23 – March - 2023


program to
demonstrate the use
of nested class

4 Design a java program 23 – March - 2023


to print all the real
solutions of the
quadratic equation

5 Design a java program 04 - April - 2023


that uses both recursive
and non-recursive
methods to print the
nth value of the
Fibonacci series
sequence

6 Design a java program 04 - April - 2023


to prompt user for an
integer and print all the
prime numbers up to
that integer.

Java Programming Lab


anubhav
IT-3
03476803121
Page 2
7 Design a java program 04 - April - 2023
to check whether a
string is palindrome or
not.

8 Design a java program 04 - April - 2023


that reads a line of
integers, then display
each integer and the
sum of all integers.

9 Design a java program 04 - April - 2023


to implement stack and
queue concept

10 Design a java program 04 - April - 2023


to create an abstract
class named Shape,
that contains an empty
method called
numberOfSides().
Provide four classes
named Trapezoid,
Triangle, Rectangle
and Hexagon such that
each one of the classes
contains only the
method
numberOfSides(), that
contains number of
sides in each
geometrical figure.

11 Design a java program 04 - April - 2023


to show dynamic
polymorphism.

12 Design a java program 04 - April - 2023


to implement
interfaces.

13 Design a java program 11 - April - 2023


to create a customized
Java Programming Lab
anubhav
IT-3
03476803121
Page 3
exception and also
make use of all 5
exception keywords.

14 Design a java program 11 - April - 2023


to sort list of names in
ascending order.

15

16 Design a java program


to implement producer
consumer problem
using concept of inter
thread communication.

17 Design a java program


to create three threads
runnable interface.
First thread display
“GOOD MORNING”
at every 1 second.
Second thread displays
“HELLO” at every 2
seconds and third
thread displays
“WELCOME” at every
3 seconds.

18 Design a java program


to create three threads
runnable interface.
First thread display
“GOOD MORNING”
at every 1 second.
Second thread displays
“HELLO” at every 2
seconds and third
thread displays
“WELCOME” at every
Java Programming Lab
anubhav
IT-3
03476803121
Page 4
3 seconds.

19 Design an applet to do
the following tasks:

a) To output the
question “WHO IS
THE PRIME
MINISTER OF
INDIA?”

b) To accept the answer


and print “CORRECT”
and then stop if the
answer is correct.

c) To print “TRY
AGAIN” and if the
answer is not correct.

d) To display the
correct answer, if the
answer is wrong even
after third attempt.

20 Design a java
program to write an
applet that computes
the payment of loan
which is based on the
amount of loan,
interest rate and
number of months. It
takes one parameter
from browser which
is called Monthly
Rate. If it is True
then interest rate is
per month else it is

Java Programming Lab


anubhav
IT-3
03476803121
Page 5
annual. (Use label,
textfield, button,
check box, checkbox
group)

21 - Design a java
program to write an
applet with following
AWT components :
textarea and button.

22 Design a java program


to write an calculator
applet. Use grid layout
to arrange the buttons
for digits and for the
+,-,*,/,% operations.
Add textfield to display
the result.

23 Design a java program


to handle keyboard
events.

24 Design a program to
implement card
layout in applets.

25 Program in java to
demonstrate file
writer, file reader
and buffer reader.

Java Programming Lab


anubhav
IT-3
03476803121
Page 6
26 program to
implement various
functions giving
details about file.

27 Design a program in
java to implement
client-server TCP/IP
socket application
which exchanges
strings among them .

28 program to create
database connectivity
using jdbc, create
statement object
create resultset object
and display.

29 p program to create
database connectivity
using jdbc, create
statement object
create resultset object
and Insert a record
using create
statement.

30 program to create
database connectivity
using jdbc, create
statement object
create resultset object
and update a record
using create
statement.

31 program to create
database connectivity
using jdbc, create
Java Programming Lab
anubhav
IT-3
03476803121
Page 7
statement object
create resultset object
and delete a record
using create
statement.

Practical 1
Aim : Design a java program to find the biggest of three given integers. Description: The name of
class is Largest, having data members num1, num2, num3, res. Member functions of class are
inputData(), findLargest() and displayLargest(). Make separate class to create void main.

Code :

class Largest {
int num1, num2, num3, result;

void input() {
num1 = 8;
num2 = 9;
num3 = 10;
}

void largest() {
if (num1 > num2 && num1 > num3) {
[Link]("Largest : " + result);
result = num1;
} else if (num2 > num1 && num2 > num3) {
result = num2;
[Link]("Largest : " + result);
} else {
result = num3;
Java Programming Lab
anubhav
IT-3
03476803121
Page 8
[Link]("Largest : " + result);
}
}
}

class Execute {
public static void main(String[] args) {
Largest l = new Largest();

[Link]();
[Link]();
}
}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 9
Practical 2
Aim : Design a java program to define a class, describe its constructor, over-load the
constructor and instantiate the object.

Code :

class Shapes {
double len;
double bre;
double result;

Shapes() {
len = 0.0;
bre = 0.0;
}

Shapes(double l) {
len = l;
bre = 0.0;
}

Shapes(double l, double b) {
len = l;
bre = b;
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 10
void area() {
if (len != 0.0 && bre == 0.0) {
result = len * len;
[Link]("AREA SQUARE " + result);
}
if (len != 0.0 && bre != 0.0) {
result = len * bre;
[Link]("AREA RECTANGLE " + result);
} else if (len == 0.0 && bre == 0.0) {
[Link]("NO OUTPUT");
}
}
}

class Execute2 {
public static void main(String a[]) {
double para1, para2;
para1 = 10.0;
para2 = 20.0;
Shapes s1 = new Shapes();
Shapes s2 = new Shapes(para1);
Shapes s3 = new Shapes(para1, para2);
[Link]();
[Link]();
[Link]();
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 11
Output : -

Practical 3
Aim : Design a java program to demonstrate the use of nested class.
Code :

class Outer {
private int outer_x = 100;

void test() {
Inner inner = new Inner();
Java Programming Lab
anubhav
IT-3
03476803121
Page 12
[Link]();
[Link]();
}

class Inner {
private void hello() {
[Link]("Private Method Hello() of Inner Class");
}

void display() {
[Link]("Display: outer_x = " + outer_x);
Outer o = new Outer();
[Link]("Display: outer_x = " + o.outer_x);
}
}
}

class Execute3 {
public static void main(String args[]) {
Outer outer = new Outer();
[Link]();
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 13
Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 14
Practical 4
Aim : Design a java program to print all the real solutions of the quadratic equation
ax2+bx+c=0, read in the values of a,b,c and use the quadratic equation formula. If the
discriminant b2-4ac is negative, display a message stating that there are no real solutions.
Description: The name of class is Quadratic, having data members a,b,c,r1,r2. Member
functions of class are inputEquation(), findRoots() and displayRoots(). Make separate class to
create void main.

Code :

import static [Link].*;

class Quadratic {
double a, b, c, r1, r2, d;

void input_equation(double a1, double b1, double c1) {


a = a1;
b = b1;
c = c1;
}

void calculate_roots() {
d = b * b - 4 * a * c;
if (d > 0) {
[Link]("The roots of the equation are real and
different. \n");
r1 = -b + sqrt(abs(d)) / 2 * a;
r2 = -b - sqrt(abs(d)) / 2 * a;
[Link](" roots are : " + r1 + " " + r2);
} else if (d == 0) {
[Link]("The roots of the equation are real and
same. \n");
r1 = -b / 2 * a;
r2 = -b / 2 * a;
[Link](" roots are : " + r1 + " " + r2);
}
else{
[Link]("The roots of the equation are complex and
different. \n");
r1 = -b / 2 * a;
r2 = -b / 2 * a;
Java Programming Lab
anubhav
IT-3
03476803121
Page 15
[Link](r1 + " + i"+ d + "\n"+ r2+ " - i" + d);
}
}
}

class Execute4{
public static void main(String[] args) {
Quadratic q = new Quadratic();
q.input_equation(1.0,3.0,5.0);
q.calculate_roots();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 16
Practical 5
Aim : . Design a java program that uses both recursive and non-recursive methods to print
the nth value of the Fibonacci series sequence.

Code :

class Fibonacci_with_recursion {
int n1 = 0, n2 = 1, n3 = 0;

void printFibonacci(int count) {


if (count > 0) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
[Link](" " + n3);
printFibonacci(count - 1);
}
}
}

class Fibonacci_without_recursion {
int n1 = 0, n2 = 1, n3 = 0;

void fibonaaci2(int count) {


for (int i = 2; i < count; ++i)// loop starts from 2 because 0
and 1 are already printed
{
n3 = n1 + n2;
[Link](" " + n3);
n1 = n2;
n2 = n3;
}
}

class Execute5 {
public static void main(String[] args) {
Fibonacci_with_recursion f = new Fibonacci_with_recursion();
[Link]("Fibonacci with recursion : ");
Java Programming Lab
anubhav
IT-3
03476803121
Page 17
[Link](7);
Fibonacci_without_recursion f2 = new
Fibonacci_without_recursion();
[Link]("\n");
[Link]("Fibonacci without recursion : ");
f2.fibonaaci2(7);
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 18
Practical 6
Aim : Design a java program to prompt user for an integer and print all the prime numbers
up to that integer.

Code :

import [Link].*;

class Prime {
int value, num;
int i, flag;

void inputNumber() {
Scanner sc = new Scanner([Link]);
[Link]("ENTER A NUMBER ");
value = [Link]();
}

void displayPrime() {
for (num = 2; num <= value; num++) {
flag = 0;
for (i = 2; i <= num - 1; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
[Link](num);
}
}
}
}

class Execute6 {
public static void main(String a[]) {
Prime o = new Prime();

Java Programming Lab


anubhav
IT-3
03476803121
Page 19
[Link]();
[Link]();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 20
Practical 7

Aim : . Design a java program to check whether a string is palindrome or not.


Code :

import [Link].*;

class P_Check {
void check(String data) {
String rdata = "";
int i, len;
len = [Link]();
for (i = len - 1; i >= 0; i--)
rdata = rdata + [Link](i);
if ([Link](rdata))
[Link]("Entered string is a palindrome.");
else
[Link]("Entered string is not a palindrome.");
}
}

class Execute7 {
public static void main(String a[]) {
String name;
P_Check pc = new P_Check();
Scanner in = new Scanner([Link]);
[Link]("Enter a string");
name = [Link]();
[Link](name);
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 21
Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 22
Practical 8

Aim : Design a java program that reads a line of integers, then display each integer and the sum of
all integers.

Code :

import [Link].*;

class IntData {
int arr[];
int i, sum;

IntData(int size) {
arr = new int[size];
sum = 0;
}

void input() {
Scanner sc = new Scanner([Link]);
for (i = 0; i < [Link]; i++) {
arr[i] = [Link]();
}
}

void display() {
for (i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
}

void sumCal() {
for (i = 0; i < [Link]; i++) {
sum = sum + arr[i];
}
[Link]("SUM IS " + sum);
}
}

class Execute8 {
public static void main(String a[]) {
IntData obj = new IntData(5);
[Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 23
[Link]();
[Link]();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 24
Practical 9

Aim : . Design a java program to implement stack and queue concept.


Code :

class Stack {
int stck[];
int tos;

Stack(int size) {
stck = new int[size];
tos = -1;
}

void push(int item) {


if (tos == [Link] - 1)
[Link]("Stack is full.");
else
stck[++tos] = item;
}

int pop() {
if (tos < 0) {
[Link]("Stack underflow.");
return 0;
} else
return stck[tos--];
}
}

class Execute9 {
public static void main(String args[]) {
Stack mystack1 = new Stack(5);
Stack mystack2 = new Stack(8);

for (int i = 0; i < 5; i++)


[Link](i);
for (int i = 0; i < 8; i++)
[Link](i);

[Link]("Stack in mystack1:");
for (int i = 0; i < 5; i++)
Java Programming Lab
anubhav
IT-3
03476803121
Page 25
[Link]([Link]());
[Link]("Stack in mystack2:");
for (int i = 0; i < 8; i++)
[Link]([Link]());
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 26
Practical 10

Aim : Design a java program to create an abstract class named Shape, that contains an empty
method called numberOfSides(). Provide four classes named Trapezoid, Triangle, Rectangle and
Hexagon such that each one of the classes contains only the method numberOfSides(), that contains
number of sides in each geometrical figure.

Code :

abstract class MyArea {


abstract void area();
}

class SQArea extends MyArea {


double s;

void area() {
double areasq;
s = 10.0;
areasq = s * s;
[Link]("Area of Square " + areasq);
}
}

class RECTArea extends MyArea {


double l, b;

void area() {
double arearect;
l = 10.0;
b = 2.0;
arearect = l * b;
[Link]("Area of Rectangle " + arearect);
}
}

class Execute10 {
public static void main(String a[]) {
// MyArea ma= new MyArea();
// [Link]();
MyArea sa = new SQArea();
[Link]();
MyArea ra = new RECTArea();
Java Programming Lab
anubhav
IT-3
03476803121
Page 27
[Link]();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 28
Practical 11

Aim : Design a java program to show dynamic polymorphism.


Code :

class MyArea {
void area() {
[Link]("A concrete function called Area()");
}
}

class SQArea extends MyArea {


double s;

void area() // Same name function in base class


{
double areasq;
s = 10.0;
areasq = s * s;
[Link]("Area of Square " + areasq);
}
}

class RECTArea extends MyArea {


double l, b;

void area() // Same name function in base class


{
double arearect;
l = 10.0;
b = 2.0;
arearect = l * b;
[Link]("Area of Rectangle " + arearect);
}
}

class Execute11 {
public static void main(String a[]) {
MyArea ma; // Base class object
ma = new SQArea(); // Refering to SQArea
[Link](); // Calling SQArea method (Dynamic Ploymorphism)
ma = new RECTArea(); // Refering to RECTArea
Java Programming Lab
anubhav
IT-3
03476803121
Page 29
[Link](); // Calling RECTArea method (Dynamic Ploymorphism)
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 30
Practical 12
Aim : . Design a java program to implement interfaces.
Code :

interface IntStack {
void push(int item); // Store an item.

int pop(); // Retrieve an item.


}

class DynStack implements IntStack {


private int stck[];
private int tos;

// Allocate and initialize stack.


DynStack(int size) {
stck = new int[size];
tos = -1;
}

// Push an item onto the stack.


public void push(int item) { // If stack is full, allocate a larger
stack.
if (tos == [Link] - 1) {
int temp[] = new int[[Link] * 2];
// Making a double size array.
for (int i = 0; i < [Link]; i++)
temp[i] = stck[i];
stck = temp;
stck[++tos] = item;
} else
stck[++tos] = item;
}

// Pop an item from the stack.


public int pop() {
if (tos < 0) {
[Link]("Stack underflow.");
return 0;
} else
return stck[tos--];
Java Programming Lab
anubhav
IT-3
03476803121
Page 31
}
}

class Execute12 {
public static void main(String args[]) {
DynStack mystack1 = new DynStack(5);
DynStack mystack2 = new DynStack(8);
// These loops cause each stack to grow.
for (int i = 0; i < 12; i++)
[Link](i);
for (int i = 0; i < 20; i++)
[Link](i);
[Link]("Stack in mystack1:");
for (int i = 0; i < 12; i++)
[Link]([Link]());
[Link]("Stack in mystack2:");
for (int i = 0; i < 20; i++)
[Link]([Link]());
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 32
Practical 13
Aim : Design a java program to create a customized exception and also make use of all 5 exception
keywords.

Code :

class NSalException extends Exception {


public NSalException(String s) {
super(s); // CALLING CONSTRUCTOR Exception()
}
}

class PSalException extends RuntimeException {


public PSalException(String s) {
super(s); // CALLING CONSTRUCTOR RuntimeException()
}
}

class Employee {
public void decideSal(String s1) throws NSalException, PSalException,
NumberFormatException {
int sal = [Link](s1);
Java Programming Lab
anubhav
IT-3
03476803121
Page 33
if (sal <= 0) {
NSalException no = new NSalException("Invalid Salary");
throw (no);
} else {
PSalException po = new PSalException("Valid Salary");
throw (po);
}
}
}

class Execute13 {
public static void main(String args[]) {
try {
Employee e = new Employee();
[Link](args[0]);
} catch (NumberFormatException nfe) {
[Link]("Please enter integer values.");
} catch (NSalException no) {
[Link]("Negative Salary.");
} catch (PSalException po) {
[Link]("Valid Positive Salary.");
} finally {
[Link]("Finally Block executing");
}
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 34
Practical 14
Aim : Design a java program to sort list of names in ascending order.
Code :

import [Link].*;

Java Programming Lab


anubhav
IT-3
03476803121
Page 35
class SortNames {
String name[] = new String[5];
int i, n = 5;

SortNames() {
for (i = 0; i < n; i++) {
name[i] = null;
}
}

void inputNames() {
name[0] = "Yogesh";
name[1] = "Deepak";
name[2] = "Aman";
name[3] = "Srishti";
name[4] = "Baljinder";
}

void bubbleSort() {
int j;
String temp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (name[j].compareTo(name[j + 1]) > 0) {
temp = name[j];
name[j] = name[j + 1];
name[j + 1] = temp;
}
}
}
}

void displayNames() {
for (i = 0; i < n; i++) {
[Link](name[i]);
}
}
}

class Execute14 {
public static void main(String a[]) {
SortNames sn = new SortNames();
[Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 36
[Link]();
[Link]();
}
}

Output :

Aim : - Design a java program to create two threads: one for odd numbers and other for even
numbers.

Code : -

class EvenThread extends Thread {

Java Programming Lab


anubhav
IT-3
03476803121
Page 37
int num;

void set() {
[Link] = 0;
}

public void run() {


for (int i = 1; i <= 10; i++) {
try {
even();
[Link](500);
} catch (InterruptedException e) {
[Link]("INTERRUPTED EXCEPTION ");
}
}
}

void even() {
num = num + 2;
[Link]("Even : " + num);
}
}

class OddThread implements Runnable {

int num;

void set() {
[Link] = 1;
}

public void run() {


for (int i = 1; i <= 10; i++) {
try {
odd();
[Link](500);
} catch (InterruptedException e) {
[Link]("INTERRUPTED EXCEPTION ");
}
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 38
void odd() {
num = num + 2;
[Link]("Odd : " + num);
}
}

class Execute15 {

public static void main(String a[]) {


int i;
EvenThread et = new EvenThread();
OddThread ot = new OddThread();
Thread t = new Thread(ot);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 39
Aim : -Design a java program to implement producer consumer problem using concept of inter
thread communication.

Code : -

class Q {

int n;
boolean valueSet = false;

synchronized int get() {


while (valueSet == false) try {
wait();
} catch (InterruptedException e) {
[Link]("InterruptedException caught");
}
[Link]("Got by Consumer: " + n);
valueSet = false;
notify();
return n;
}

synchronized void put(int n) {


Java Programming Lab
anubhav
IT-3
03476803121
Page 40
while (valueSet == true) try {
wait();
} catch (InterruptedException e) {
[Link]("InterruptedException caught");
}
this.n = n;
valueSet = true;
[Link]("Put by Producer: " + n);
notify();
}
}

class Producer implements Runnable {

Q q;

Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}

public void run() {


int i = 0;
while (true) {
[Link](i++);
}
}
}

class Consumer implements Runnable {

Q q;

Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}

public void run() {


while (true) {
[Link]();
}
}
Java Programming Lab
anubhav
IT-3
03476803121
Page 41
}

class Execute16 {

public static void main(String args[]) {


Q q = new Q();
new Producer(q);
new Consumer(q);
[Link]("Press Control-C to stop.");
}
}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 42
Java Programming Lab
anubhav
IT-3
03476803121
Page 43
Aim : - Design a java program to create three threads runnable interface. First thread display
“GOOD MORNING” at every 1 second. Second thread displays “HELLO” at every 2 seconds and
third thread displays “WELCOME” at every 3 seconds.

Code : -

class Callme {

void call(String msg, int t) {


[Link](msg);
try {
[Link](t * 1000);
} catch (InterruptedException e) {
[Link]("Interrupted");
}
}
}

class Caller implements Runnable {

String msg;
int time;
Callme target;
Thread t;

public Caller(Callme targ, String s, int et) {


target = targ;
msg = s;
time = et;
t = new Thread(this);
[Link]();
}

public void run() {


int i;
for (i = 1; i <= 5; i++) {
synchronized (target) {
[Link](msg, time);
}
}
Java Programming Lab
anubhav
IT-3
03476803121
Page 44
}
}

class Execute17 {

public static void main(String args[]) {


Callme target = new Callme();
Caller ob1 = new Caller(target, "GOOD MORNING", 1);
Caller ob2 = new Caller(target, "HELLO", 2);
Caller ob3 = new Caller(target, "WELCOME", 3);
try {
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Interrupted");
}
}
}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 45
Java Programming Lab
anubhav
IT-3
03476803121
Page 46
Aim : - Design a java program to create three threads runnable interface. First thread display
“GOOD MORNING” at every 1 second. Second thread displays “HELLO” at every 2 seconds and
third thread displays “WELCOME” at every 3 seconds.

Code : -

class Callme {

void call(String msg, int t) {


[Link](msg);
try {
[Link](t * 1000);
} catch (InterruptedException e) {
[Link]("Interrupted");
}
}
}

class Caller implements Runnable {

String msg;
int time;
Callme target;
Thread t;

public Caller(Callme targ, String s, int et) {


target = targ;
msg = s;
time = et;
t = new Thread(this);
[Link]();
}

public void run() {


int i;
for (i = 1; i <= 5; i++) {
synchronized (target) {
[Link](msg, time);
}
}
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 47
class Execute18 {

public static void main(String args[]) {


Callme target = new Callme();
Caller ob1 = new Caller(target, "GOOD MORNING", 1);
Caller ob2 = new Caller(target, "HELLO", 2);
Caller ob3 = new Caller(target, "WELCOME", 3);
try {
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Interrupted");
}
}
}

Output :-

Java Programming Lab


anubhav
IT-3
03476803121
Page 48
Aim :- Design an applet to do the following tasks:
a) To output the question “WHO IS THE PRIME MINISTER OF INDIA?”

b) To accept the answer and print “CORRECT” and then stop if the answer is correct.

c) To print “TRY AGAIN” and if the answer is not correct.

d) To display the correct answer, if the answer is wrong even after third attempt.

Code : -

import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code=MyELClass width=600 height=600></applet> */


public class MyELClass extends Applet implements ActionListener {

String ans = "";


Button sub;
TextField tf1;
int count = 0;

public void init() {


Label f = new Label("ENTER PRIME MINISTER NAME", [Link]);
tf1 = new TextField(10);
add(f);
add(tf1);
sub = new Button("SUBMIT");
add(sub);
[Link](this);
}

public void actionPerformed(ActionEvent ae) {


String name = [Link]();
if ([Link]() == sub) {
if ([Link]("Narendra Modi")) {
ans = "CORRECT";
[Link](false);
} else {
ans = "INCORRECT";
Java Programming Lab
anubhav
IT-3
03476803121
Page 49
count++;
if (count == 3) {
ans = "NAME OF PRIME MINISTER IS Sh. NARENDRA MODI";
}
}
}
repaint();
}

public void paint(Graphics g) {


[Link](ans, 10, 100);
}
}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 50
Aim :- Design a java program to write an applet that computes the payment of loan which is
based on the amount of loan, interest rate and number of months. It takes one parameter from
browser which is called Monthly Rate. If it is True then interest rate is per month else it is
annual. (Use label, textfield, button, check box, checkbox group)

Code : -

import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code=prog20 width=150 height=150></applet>


<param name=N1 value=5>
*/
public class prog20 extends Applet implements ItemListener,
ActionListener {

String s1;
Int n1;
TextField tf1, tf2;
Button result;
Checkbox cb1, cb2;
CheckboxGroup cbg;

public void init() {


Label f = new Label("ENTER AMOUNT OF LOAN", [Link]);
Label s = new Label("ENTER INTEREST RATE", [Link]);
tf1 = new TextField(4);
tf2 = new TextField(4);
add(f);
add(tf1);
add(s);
add(tf2);
cbg = new CheckboxGroup();
cb1 = new Checkbox("MONTHLY RATE", cbg, true);
cb2 = new Checkbox("YEARLY RATE", cbg, false);
result = new Button("LOAN PAYMENT AMOUNT");
add(cb1);
add(cb2);
add(result);
[Link](this);
[Link](this);

Java Programming Lab


anubhav
IT-3
03476803121
Page 51
[Link](this);
s1 = getParameter("N1");
if (s1 != null) {
n1 = [Link](s1);
}
}

public void itemStateChanged(ItemEvent ie) {


int n1 = [Link]([Link]());
int n2 = [Link]([Link]());
int res;
if([Link]()==true){
res = n1+n2/12*100;
String ans = "Result : "+res;
}
if([Link]()==true){
res = n1+n2/12*100;
String ans = "Result : "+res;
}
}

public void actionPerformed(ActionEvent ae) {


repaint();
}

public void paint(Graphics g) {


[Link](s1, 10, 150);
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 52
Output :-

Java Programming Lab


anubhav
IT-3
03476803121
Page 53
Aim : - Design a java program to write an applet with following AWT components : textarea and
button.

Code : -

import [Link].*;
import [Link].*;
import [Link].*;

/*<applet code=MyELClass width=300 height=300> </applet>*/


public class MyELClass extends Applet implements ActionListener {

String msg = "";


String data = "";
Button select;
TextArea ta;

public void init() {


data =
"COUNTRY NAME: INDIA\n" + "CAPITAL NAME: DELHI\n" + "STATE NAME:
DELHI";
ta = new TextArea(data, 5, 30);
add(ta);
select = new Button("SELECT");
add(select);
[Link](this);
}

public void actionPerformed(ActionEvent ae) {


if ([Link]() == select) {
msg = [Link]();
}
repaint();
}

public void paint(Graphics g) {


[Link](msg, 10, 150);
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 54
}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 55
Aim : - Design a java program to write an calculator applet. Use grid layout to arrange the buttons
for digits and for the +,-,*,/,% operations. Add textfield to display the result.

Code : -

import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code=prog22 width=300 height=300></applet>*/


public class prog22 extends Applet implements ActionListener {

String s, s1, s2, s3, s4;


Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
Button add, sub, eq, cl, mul, div, rem;
TextField t1;
int a, b, c;

public void init() {


t1 = new TextField(10);
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
add = new Button("+");
sub = new Button("-");
mul = new Button("*");
div = new Button("/");
eq = new Button("=");

Java Programming Lab


anubhav
IT-3
03476803121
Page 56
cl = new Button("Clear");
rem = new Button("%");
GridLayout gb = new GridLayout(4, 5);
setLayout(gb);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(add);
add(sub);
add(mul);
add(div);
add(rem);
add(eq);
add(cl);

[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}

public void actionPerformed(ActionEvent e) {


if ([Link]() == add) {
Java Programming Lab
anubhav
IT-3
03476803121
Page 57
msg = t1+t2;
}
if ([Link]() == sub) {
msg = t1-t2;
}
if ([Link]() == mul) {
msg = t1*t2;
}
if ([Link]() == div) {
msg = t1/t2;
}
repaint();
}
}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 58
Aim : - . Design a java program to handle keyboard events.
Code : -

import [Link].*;
import [Link].*;
import [Link].*;

/*<applet code="ELClass1" width=400 height=200> </applet>*/


public class ELClass1 extends Applet implements KeyListener {

String data = "";


int x, y, px, py;

public void init() {


addKeyListener(this);
px = 0;
py = 0;
x = px;
y = py;
}

public void keyPressed(KeyEvent k) {


int code = [Link]();
if (code == KeyEvent.VK_LEFT) {
x--;
}
if (code == KeyEvent.VK_RIGHT) {
x++;
}
if (code == KeyEvent.VK_UP) {
y--;
}
if (code == KeyEvent.VK_DOWN) {
Java Programming Lab
anubhav
IT-3
03476803121
Page 59
y++;
}
Graphics g = getGraphics();
[Link](px, py, x, y);
px = x;
py = y;
}

public void keyTyped(KeyEvent k) {}

public void keyReleased(KeyEvent k) {}


}

Output : -

Java Programming Lab


anubhav
IT-3
03476803121
Page 60
Aim : Design a program to implement card layout in applets.

Code :

import [Link].*;
import [Link].*;
import [Link].*;

/*<applet code=MyELClass width=650 height=100></applet>*/


public class MyELClass extends Applet implements ItemListener,
ActionListener {

Panel main, P1, P2;


CardLayout cl;
String ans = "";
Button plus, sub, mul, div;
Checkbox cb1, cb2;
CheckboxGroup cbg;
TextField tf1, tf2;

public void init() {


cl = new CardLayout();
main = new Panel();
[Link](cl);
Label f = new Label("ENTER FIRST NUMBER");
Label s = new Label("ENTER SECOND NUMBER");
tf1 = new TextField(4);
tf2 = new TextField(4);
cbg = new CheckboxGroup();
cb1 = new Checkbox("ADDITION/SUBTRACTION", cbg, false);
cb2 = new Checkbox("MULTIPLICATION/DIVISION", cbg, false);
add(f);
add(tf1);
add(s);
Java Programming Lab
anubhav
IT-3
03476803121
Page 61
add(tf2);
add(cb1);
add(cb2);
plus = new Button("ADDITION");
sub = new Button("SUBTRACTION");
mul = new Button("MULTIPLICATION");
div = new Button("DIVISION");
P1 = new Panel();
[Link](plus);
[Link](sub);
P2 = new Panel();
[Link](mul);
[Link](div);
add(main);
[Link](P1, "as");
[Link](P2, "md");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}

public void actionPerformed(ActionEvent ae) {


if ([Link]() == add) {
msg = t1+t2;
}
if ([Link]() == sub) {
msg = t1-t2;
}
if ([Link]() == mul) {
msg = t1*t2;
}
if ([Link]() == mul) {
msg = t1/t2;
}

public void itemStateChanged(ItemEvent ie) {


if ([Link]() == true) {
Java Programming Lab
anubhav
IT-3
03476803121
Page 62
[Link](main, "as");
}
if ([Link]() == true) {
[Link](main, "md");
}
repaint();
}

public void paint(Graphics g) {


[Link](ans, 10, 100);
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 63
Aim : Program in java to demonstrate file writer, file reader and buffer reader.

Code :

import [Link].*;

class program25 {

public static void main(String s[]) throws IOException {


String name;
FileWriter fw = new FileWriter("[Link]");
name = "INDIA";
[Link](name);
name = "\n";
[Link](name);
name = "DELHI";
[Link](name);
[Link]();
String newname;

Java Programming Lab


anubhav
IT-3
03476803121
Page 64
FileReader fr = new FileReader("[Link]");
BufferedReader br = new BufferedReader(fr);
newname = [Link]();
[Link](newname);
newname = [Link]();
[Link](newname);
[Link]();
[Link]();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 65
Java Programming Lab
anubhav
IT-3
03476803121
Page 66
Aim : program to implement various functions giving details about file.

Code :

import [Link];
import [Link];

class program26 {

static void p(String s) {


[Link](s);
}

public static void main(String args[]) {


File f1 = new File("D:/JAVA_TESTING.txt");
p("File Name: " + [Link]());
p("Path: " + [Link]());
p("Abs Path: " + [Link]());
p("Parent: " + [Link]());
p([Link]() ? "exists" : "does not exist");
p([Link]() ? "is writeable" : "is not writeable");
p([Link]() ? "is readable" : "is not readable");
p("is " + ([Link]() ? "" : "not" + " a directory"));
p([Link]() ? "is normal file" : "path");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
p("File last modified: " + [Link]([Link]()));
p("File size: " + [Link]() + " Bytes");
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 67
Out put :

Aim : Design a program in java to implement client-server TCP/IP socket application which
exchanges strings among them .
Java Programming Lab
anubhav
IT-3
03476803121
Page 68
Code :

[Link]

import [Link].*;
import [Link].*;
class server
{
public static void main(String args[])
{
int port=6666;
try
{
ServerSocket ss=new ServerSocket(port);
[Link]("WAITING FOR CLIENT ");
Socket socket=[Link]();
[Link]("GOT A CLIENT ");
InputStream sin=[Link]();
OutputStream sout=[Link]();
DataInputStream in=new DataInputStream(sin);
DataOutputStream out=new DataOutputStream(sout);
String line=null;
while(true)
{
line=[Link]();
[Link]("LINE RECEIVED FROM CLIENT "+line);
[Link]("SENDING LINE BACK....");
[Link](line);
[Link]();
[Link]("WAITING FOR ANOTHER LINE");
}
}
catch(IOException e)
{
[Link]("EXCEPTION OCCURRED………");
}
}
}

[Link]

Java Programming Lab


anubhav
IT-3
03476803121
Page 69
import [Link].*;
import [Link].*;

class client {

public static void main(String args[]) {


int serverport = 6666;
String address = "[Link]";
try {
InetAddress ip = [Link](address);
[Link]("SERVER IP ADDRESS " + address);
[Link]("SERVER PORT " + serverport);
Socket socket = new Socket(ip, serverport);
InputStream sin = [Link]();
OutputStream sout = [Link]();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);

InputStreamReader is = new InputStreamReader([Link]);


BufferedReader br = new BufferedReader(is);
String line = null;
String msg = null;
while (true) {
line = [Link]();
if ([Link]("end")) {
break;
}
[Link](line);
[Link]();
msg = [Link]();
[Link]("DATA RECEIVED FROM SERVER " + msg);
}
} catch (IOException e) {
[Link]("EXCEPTION OCCURRED………");
}
}
}

Ouput :
Java Programming Lab
anubhav
IT-3
03476803121
Page 70
Aim : program to create database connectivity using jdbc, create statement object create
resultset object and display.

Java Programming Lab


anubhav
IT-3
03476803121
Page 71
Code :

import [Link].*;

class DBConnectivity {

public Connection CO;

void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link]("jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}

void displayFirst() {
try {
ResultSet RS;
Statement SMT;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);

[Link]();
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 72
void displayThird() {
try {
ResultSet RS;
Statement SMT;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link](3);
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
[Link]();
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

void displayLast() {
try {
ResultSet RS;
Statement SMT;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);

[Link]();
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 73
void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

void displayNextAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}

[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}

void displayPreviousAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 74
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}

[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}
}

public class program28 {

public static void main(String args[]) {


DBConnectivity Obj = new DBConnectivity();
[Link]();
[Link]("DIPSLAYING FIRST RECORD ");
[Link]();
[Link]("DIPSLAYING THIRD RECORD ");
[Link]();
[Link]("DIPSLAYING LAST RECORD ");
[Link]();
[Link]("DIPSLAYING ALL RECORDS ");
[Link]();
[Link]("DIPSLAYING ALL RECORDS ");
[Link]();
[Link]();
}
Java Programming Lab
anubhav
IT-3
03476803121
Page 75
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 76
Aim : program to create database connectivity using jdbc, create statement object create
resultset object and insert a record using prepared statement.

Code :

import [Link].*;
import [Link].*;
import [Link].*;

class DBConnectivity {

public Connection CO;

void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link](
"jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}

void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

void displayNextAll() {
try {

Java Programming Lab


anubhav
IT-3
03476803121
Page 77
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}

[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}

void insertRecord() {
int Hnum, Hrooms;
String Hloc, Howner;
Scanner sc = new Scanner([Link]);
try {
Statement SMT;
String QU;
SMT = [Link]();
[Link]("ENTER HOUSE NUMBER ");
Hnum = [Link]();
[Link]("ENTER HOUSE LOCALITY ");
Hloc = [Link]();
[Link]("ENTER Mr. / Mrs. ");
String salu = [Link]();
[Link]("ENTER HOUSE OWNER NAME ");
Java Programming Lab
anubhav
IT-3
03476803121
Page 78
Howner = [Link]();
Howner = salu + " " + Howner;
[Link]("ENTER HOUSE ROOMS ");
Hrooms = [Link]();
QU =
"INSERT INTO HOUSE VALUES(" +
Hnum +
",'" +
Hloc +
"'," +
Hrooms +
",'" +
Howner +
"');";
[Link](QU);
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Insert .....");
}
}

void insertRecordUsingPrepared() {
int Hnum, Hrooms;
String Hloc, Howner;
Scanner sc = new Scanner([Link]);
try {
PreparedStatement PSMT;
String QU;
PSMT = [Link]("INSERT INTO HOUSE VALUES(?,?,?,?)");
[Link]("ENTER HOUSE NUMBER ");
Hnum = [Link]();
[Link]("ENTER HOUSE LOCALITY ");
Hloc = [Link]();
[Link]("ENTER Mr. / Mrs. ");
String salu = [Link]();
[Link]("ENTER HOUSE OWNER NAME ");
Howner = [Link]();
Howner = salu + " " + Howner;
[Link]("ENTER HOUSE ROOMS ");
Hrooms = [Link]();
[Link](1, Hnum);
[Link](2, Hloc);
[Link](3, Hrooms);
Java Programming Lab
anubhav
IT-3
03476803121
Page 79
[Link](4, Howner);
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Insert .....");
[Link](Ee);
}
}
}

public class program29{

public static void main(String args[]) {


DBConnectivity Obj = new DBConnectivity();
[Link]();
//[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 80
Java Programming Lab
anubhav
IT-3
03476803121
Page 81
Aim : program to create database connectivity using jdbc, create statement object create
resultset object and update a record using prepared statement.

Code :

import [Link].*;
import [Link].*;
import [Link].*;

class DBConnectivity {

public Connection CO;

void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link](
"jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}

void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

void displayNextAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";

Java Programming Lab


anubhav
IT-3
03476803121
Page 82
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}

[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}

void updateRecord() {
int Hnum, Hrooms;
String Hloc, Howner;
Scanner sc = new Scanner([Link]);
try {
Statement SMT;
String QU;
SMT = [Link]();
[Link]("ENTER HOUSE NUMBER TO UPDATE ");
Hnum = [Link]();
[Link]("ENTER HOUSE LOCALITY ");
Hloc = [Link]();
[Link]("ENTER Mr. / Mrs. ");
String salu = [Link]();
[Link]("ENTER HOUSE OWNER NAME ");
Howner = [Link]();
Howner = salu + " " + Howner;
[Link]("ENTER HOUSE ROOMS ");
Hrooms = [Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 83
QU =
"UPDATE HOUSE SET H_LOCALITY = '" +
Hloc +
"', H_ROOMS = " +
Hrooms +
", H_OWNER = '" +
Howner +
"' WHERE H_NO = " +
Hnum +
";";
[Link](QU);
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Update .....");
}
}
}

public class program30 {

public static void main(String args[]) {


DBConnectivity Obj = new DBConnectivity();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Java Programming Lab


anubhav
IT-3
03476803121
Page 84
Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 85
Aim : program to create database connectivity using jdbc, create statement object create
resultset object and delete a record using prepared statement.

Code :

import [Link].*;
import [Link].*;
import [Link].*;

class DBConnectivity {

public Connection CO;

void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link](
"jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}

void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}

void displayNextAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 86
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}

void deleteRecord() {
int Hnum;
Scanner sc = new Scanner([Link]);
try {
Statement SMT;
String QU;
SMT = [Link]();
[Link]("ENTER HOUSE NUMBER, TO DELETE ");
Hnum = [Link]();
QU = "DELETE FROM HOUSE WHERE H_NO = " + Hnum + ";";
[Link](QU);
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Delete .....");
}
}
}

public class program31 {

public static void main(String args[]) {


DBConnectivity Obj = new DBConnectivity();
Java Programming Lab
anubhav
IT-3
03476803121
Page 87
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output :

Java Programming Lab


anubhav
IT-3
03476803121
Page 88

You might also like