0% found this document useful (0 votes)
2 views48 pages

Java Lab Manual R21

The Java Programming Lab Manual outlines a series of experiments for II B.Tech students at Vidya Jyothi Institute of Technology, focusing on Object-Oriented Programming through Java. It includes various programming tasks such as function usage, class and object illustration, inheritance types, exception handling, and more, with sample code and expected outputs. The manual serves as a comprehensive guide for students to practice and understand core Java concepts.

Uploaded by

annelashalini
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)
2 views48 pages

Java Lab Manual R21

The Java Programming Lab Manual outlines a series of experiments for II B.Tech students at Vidya Jyothi Institute of Technology, focusing on Object-Oriented Programming through Java. It includes various programming tasks such as function usage, class and object illustration, inheritance types, exception handling, and more, with sample code and expected outputs. The manual serves as a comprehensive guide for students to practice and understand core Java concepts.

Uploaded by

annelashalini
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 Manual

VIDYA JYOTHI INSTITUTE OF TECHNOLOGY


(Autonomous)
Aziz Nagar Gate, C.B. Post, Hyderabad-75

Department of Information Technology

Year : II [Link] Semester : II

OOPS THROUGH JAVA LABORATORY

LAB MANUAL (R22)

Week [Link] Name of The Experiment Page No

Department of Information Technology, VJIT Page 1


Java Programming Lab Manual

1 Write a program to find total, average of given two numbers by using


function with command-line arguments, static data members. 4
Week 2 Write a program to illustrate class and objects. 5
1&2 3 Write a program to illustrate method & constructor overloading. 6-7
4 Write a program to illustrate parameter passing using objects. 8
5 Write a program to illustrate Array Manipulation. 9-10
6 Write a program to illustrate different types of inheritances. 11-14
7 Write a java program to illustrate Method overriding. 15
Week 3 8 Write a java program to demonstrate the concept of polymorphism (Dynamic
Method Dispatch). 16-17
9 Write a program to demonstrate final keyword. 18-20
10 Write a program to illustrate the use of creation of packages. 21-22
Week 11 Write a java program to handle the situation of exception handling using
4&5 multiple catch blocks 23-24
12 Write a program to implement the concept of User defined Exceptions. 25
13 Write a program to illustrate Multithreading and Multitasking. 26-27
Week 14 Write a program to illustrate thread priorities. 28
6&7 15 Write a program to illustrate Synchronization 29-30
Week 16 Write a program to implement StringTokenizer. 31
8&9 17 Write a program to read one line at a time, and write it to another file. 32-33
Week 18 Write a program to illustrate Event Handling (keyboard, Mouse events) 34-39
10 & 11 19 Write a program to illustrate applet life cycle and parameter passing. 40-43
Week 12 20 Write a program to develop a calculator application using AWT. 44-46
Week 13 21 Write a program to illustrate JDBC. 47-48

Week 1 & 2

Department of Information Technology, VJIT Page 2


Java Programming Lab Manual

[Link] a program to find total, average of given two numbers by using function with command-line
arguments, static data members.
Program:
public class Sum
{
public static void tot(int a,int b)
{
int total=a+b;
int avg=total/2;
[Link]("THE SUM OF TWO NUMBER IS " +total);
[Link]("THE AVERAGE OF TWO NUMBER IS " +avg);
}
public static void main(String args[])
{
tot(56,90);
}
}

OUT PUT:
THE SUM OF TWO NUMBER IS 146
THE AVERAGE OF TWO NUMBER IS
73

[Link] of this keyword and command line arguments


Program:
Department of Information Technology, VJIT Page 3
Java Programming Lab Manual

class ThisEx
{
int a,b;
public void ThisEx1(int a,int b)
{
this.a=a;
this.b=b;
int c=a+b;
int avg=c/2;
[Link]("THE SUM OF TWO NUMBER IS " +c);
[Link]("THE AVERAGE OF TWO NUMBER IS " +avg);
}
public static void main(String args[])
{
int x,y;
x=[Link](args[0]);
y=[Link](args[1]);

ThisEx S=new ThisEx();


S.ThisEx1(x,y);
}
}
OUTPUT:
THE SUM OF TWO NUMBER IS 146
THE AVERAGE OF TWO NUMBER IS 73

2. Write a program to illustrate class and objects.

Department of Information Technology, VJIT Page 4


Java Programming Lab Manual

Program:
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){[Link](rollno+" "+name);}
}
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Karan");
[Link](222,"Aryan");
[Link]();
[Link]();
}
}
Output:
111 Karan
222 Aryan

3. Write a program to illustrate method & constructor overloading.

Department of Information Technology, VJIT Page 5


Java Programming Lab Manual

Program:
public class Student
{
//instance variables of the class
int id;
String name;
Student(){
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
void Teacher()
{
[Link]("this a method overloading");

}
void Teacher(int a,int b)
{
int i,k;
i=a;
k=b;
[Link](i+k);
}
public static void main(String[] args) {
//object creation
Student s = new Student();
[Link]("\nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);

Department of Information Technology, VJIT Page 6


Java Programming Lab Manual

Student T = new Student();


[Link]();
[Link](5,6);
}
}
Output:
this a default constructor
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
Student Name : David
this a default constructor
this a method overloading
11

4. Write a program to illustrate parameter passing using objects.

Department of Information Technology, VJIT Page 7


Java Programming Lab Manual

Program:
class Data
{
int data1;
int data2;
}
class SetData
{
void setData(Data da,int d1,int d2)
{
da.data1 = d1;
da.data2 = d2;
}
void getData(Data da)
{
[Link]("data1 : "+da.data1);
[Link]("data2 : "+da.data2);
}
}
public class Javaapp
{
public static void main(String[] args)
{
Data da = new Data();
SetData sd = new SetData();
[Link](da,50,100);
[Link](da);
}
}
Output:
data1 : 50
data2 : 100
[Link] a program to illustrate Array Manipulation.

Department of Information Technology, VJIT Page 8


Java Programming Lab Manual

Program:
import [Link];
public class ArrayInputExample1
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of elements you want to store: ");
//reading the number of elements from the that we want to enter
n=[Link]();
//creates an array in the memory of length 10
int[] array = new int[10];
[Link]("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
//reading array elements from the user
array[i]=[Link]();
}
[Link]("Array elements are: ");
// accessing array elements using the for loop
for (int i=0; i<n; i++)
{
[Link](array[i]);
}
}
}
Output:
Enter the number of elements you want to store: 5
Enter the elements of the array:
2
6
9

Department of Information Technology, VJIT Page 9


Java Programming Lab Manual

3
1
Array elements are:
2
6
9
3
1

Week 3

Department of Information Technology, VJIT Page 10


Java Programming Lab Manual

6. Write a program to illustrate different types of inheritances


[Link] Inheritance
Program:
class A
{
public void methodA()
{
[Link]("Base class method");
}
}
class B extends A
{
public void methodB()
{
[Link]("Child class method");
}
public static void main(String args[])
{
B obj = new B();
[Link]();
[Link]();
}
}
Output:
Base class method
Child class method

Department of Information Technology, VJIT Page 11


Java Programming Lab Manual

[Link] Inheritance
class X
{
public void methodX()
{
[Link]("Class X method");
}
}
class Y extends X
{
public void methodY()
{
[Link]("class Y method");
}
}
class Z extends Y
{
public void methodZ()
{
[Link]("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
[Link](); //calling grand parent class method
[Link](); //calling parent class method
[Link](); //calling local method
}
}
Output:
Class X method
class Y method
class Z method

Department of Information Technology, VJIT Page 12


Java Programming Lab Manual

[Link] Inheritance
Program:

// creating the base class(or superclass)


class BaseClass
{
int parentNum = 10;
}
// creating the subclass1 that inherits the base class
class SubClass1 extends BaseClass
{
int childNum1 = 1;
}
// creating the subclass2 that inherits the base class
class SubClass2 extends BaseClass
{
int childNum2 = 2;
}
// creating the subclass3 that inherits the base class
class SubClass3 extends BaseClass
{
int childNum3 = 3;
}
public class Main
{
public static void main(String args[])
{
SubClass1 childObj1 = new SubClass1 ();
SubClass2 childObj2 = new SubClass2 ();
SubClass3 childObj3 = new SubClass3 ();
[Link]("parentNum * childNum1 = " + [Link] * childObj1.childNum1);
[Link]("parentNum * childNum2 = " + [Link] * childObj2.childNum2);
[Link]("parentNum * childNum3 = " + [Link] * childObj3.childNum3); }
}

Department of Information Technology, VJIT Page 13


Java Programming Lab Manual

Output:
parentNum * childNum1 = 10
parentNum * childNum2 = 20
parentNum * childNum3 = 30

Department of Information Technology, VJIT Page 14


Java Programming Lab Manual

7. Write a java program to illustrate Method overriding.


Program:
class Vehicle
{
void run()
{
[Link]("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
[Link]("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
[Link]();
}
}
Output:
Bike is running safely

Department of Information Technology, VJIT Page 15


Java Programming Lab Manual

8. Write a java program to demonstrate the concept of polymorphism (Dynamic Method Dispatch).
Program:
class student1
{
int rollno;
String name, branch;
void display()
{
[Link]("SUPER CLASS DISPLAY");
[Link]("HELLO");
}
}
class studentdemo extends student1
{
void display()
{
[Link]("SUB CLASS DISPLAY");
[Link]("WELCOME");
}
}
class St1
{
public static void main (String a[])
{
student1 s;
student1 s1= new student1();
studentdemo s2= new studentdemo();
s=s1;
[Link]();
s=s2;
[Link]();
}
}

Department of Information Technology, VJIT Page 16


Java Programming Lab Manual

Output:
SUPER CLASS DISPLAY
HELLO
SUB CLASS DISPLAY
WELCOME

Department of Information Technology, VJIT Page 17


Java Programming Lab Manual

9. Write a program to demonstrate final keyword.


[Link] with variable:
Program:
class Bike9
{
final int speedlimit=90;//final variable
void run()
{
speedlimit=400; //error
}
}
class St1
{
public static void main (String a[])
{
Bike9 s1= new Bike9();
[Link]();
}
}
Output:
[Link]: error: cannot assign a value to final variable speedlimit
speedlimit=400; //error
^
1 error

Department of Information Technology, VJIT Page 18


Java Programming Lab Manual

[Link] method
Program:
class Bike{
final void run(){[Link]("running");} }
class Honda extends
Bike{ void run() //error
{
[Link]("running safely with 100kmph");
}
public static void main(String args[])
{
Honda honda= new
Honda(); [Link]();
}
}
Output:
[Link]: error: run() in Honda cannot override run() in Bike
Bike{ void run() //error
^
overridden method is final
1 error

Department of Information Technology, VJIT Page 19


Java Programming Lab Manual

[Link] class
Program:
final class Bike{}
class Honda1 extends Bike//error can not be inherited
{ void run(){[Link]("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
[Link]();
} }
Output:
[Link]: error: cannot inherit from final Bike
class Honda1 extends Bike//error can not be inherited
^
1 error

Department of Information Technology, VJIT Page 20


Java Programming Lab Manual

Week 4 & 5
10. Write a program to illustrate the use of creation of packages.
Note: Create package “calculator” and “DoubleCalculator” class.
//save as [Link]
package calculator;
public class DoubleCalculator
{
public double add(double a, double b)
{
return a+b;
}
public double sub(double a, double b)
{
return a-b;
}
public double mul(double a, double b)
{
return a*b;
}
public double div(double a, double b)
{
return a/b;
}
}
Compile “DoubleCalculator” class of “calculator” package.

>javac –d . [Link]

// importing [Link]
//save as [Link]

import [Link];
public class Calc

Department of Information Technology, VJIT Page 21


Java Programming Lab Manual

{
public static void main(String args[])
{
DoubleCalculator c1=new DoubleCalculator();
double r;
r= [Link](10.0,20.0);
[Link]("sum = "+r);
r= [Link](10.0,20.0);
[Link]("diff = "+r);
r= [Link](10.0,20.0);
[Link]("product = "+r);
r= [Link](10.0,20.0);
[Link]("div = " +r);
}
}

Compile & Run [Link]


>Javac [Link]
>java Calc

OUTPUT:
sum = 30.0
diff = -10.0
product = 200.0
div = 0.5

Department of Information Technology, VJIT Page 22


Java Programming Lab Manual

11. Write a java program to handle the situation of exception handling using multiple catch blocks
Program:
import [Link].*;
public class ExceptionDemo
{
public static void main(String args[])
{
int a,b,c;
Scanner in=new Scanner([Link]);

/*IMPLEMENTATION OF ARITHMETIC EXCEPTION */

[Link]("\nPLEASE ENTER TWO NUMBER FOR DIVISION : ");


a=[Link]();
b=[Link]();
try
{
[Link]("I AM IN TRY BLOCK 1");
c=a/b;
[Link]("\nDIVISION OF TWO NUMBER IS : " +c);
}
catch(InputMismatchException e)
{
[Link]("\nI AM IN CATCH BLOCK 1");
[Link]("\n CHECK INPUT \n");
[Link](e);
}
catch(ArithmeticException e)
{
[Link]("\nI AM IN CATCH BLOCK 1");
[Link]("\nDIVISION BY ZERO IS NOT POSSIBLE\n");
[Link](e);
}

Department of Information Technology, VJIT Page 23


Java Programming Lab Manual

[Link]("\nEND OF ARITHMETIC EXCEPTION CONCEPT");

}}
Output:
PLEASE ENTER TWO NUMBER FOR DIVISION : 5
0
I AM IN TRY BLOCK 1
I AM IN CATCH BLOCK 1
DIVISION BY ZERO IS NOT POSSIBLE
[Link]: / by zero
END OF ARITHMETIC EXCEPTION CONCEPT

Department of Information Technology, VJIT Page 24


Java Programming Lab Manual

12. Write a program to implement the concept of User defined Exceptions.


Program:
class AgeException extends Exception
{
AgeException(String s)
{
super(s);
}
}
public class CustomException
{
static void validate(int age) throws AgeException
{
if(age<18)
throw new AgeException("cant vote");
else
[Link]("can be voted");
}
public static void main(String args[]) throws AgeException
{
validate(5);

}
}
Output:
Exception in thread "main" AgeException: cant vote
at [Link]([Link])
at [Link]([Link])

Department of Information Technology, VJIT Page 25


Java Programming Lab Manual

Week 6 & 7
13. Write a program to illustrate Multithreading and Multitasking.
Program:
public class SleepTest extends Thread
{
public void run()
{
for(int i=1;i<5;i++)
{
try
{
[Link](500);
}
catch(InterruptedException e)
{
[Link](e);
}
[Link](i);
}
}
public static void main(String args[])
{
SleepTest t1=new SleepTest(); SleepTest2 t2=new SleepTest2(); [Link]();
[Link]();
}
}
class SleepTest2 extends Thread
{
public void run()
{
for(int i=5;i<10;i++)
{
try

Department of Information Technology, VJIT Page 26


Java Programming Lab Manual

{
[Link](500);
}
catch(InterruptedException e)
{
[Link](e);
}
[Link](i);
}
}
}
Output:
1
5
2
6
7
3
4
8
9

Department of Information Technology, VJIT Page 27


Java Programming Lab Manual

14. Write a program to illustrate thread priorities.


Program:
class TestPriority1 extends Thread
{
public void run(){
[Link]("running thread name is:"+[Link]().getName());
[Link]("running thread priority is:"+[Link]().getPriority());
}
public static void main(String args[])
{
TestPriority1 m1=new TestPriority1();
TestPriority1 m2=new TestPriority1();
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}
Output:
running thread name is:Thread-0
running thread priority is:1
running thread name is:Thread-1
running thread priority is:10

Department of Information Technology, VJIT Page 28


Java Programming Lab Manual

15. Write a program to illustrate Synchronization


Program:
class Disp
{
synchronized void disp1(int n)
{
for(int i=1;i<=n;i++)
{
[Link](i);
try
{
[Link](500);

} catch (Exception e) {
}
}
}
}
public class SyncThread extends Thread
{
Disp d;
SyncThread(Disp d)
{
this.d=d;
}
public void run()
{
d.disp1(5);
}
public static void main(String args[])
{
Disp d1=new Disp();
SyncThread t1=new SyncThread(d1);

Department of Information Technology, VJIT Page 29


Java Programming Lab Manual

SyncThread t2=new SyncThread(d1);

[Link]();
[Link]();
}
}
Output:
1
2
3
4
5
1
2
3
4
5

Department of Information Technology, VJIT Page 30


Java Programming Lab Manual

Week 8 & 9:
16. Write a program to implement StringTokenizer.
Program:
import [Link];
public class MyStringTokenizer
{
public static void main(String args[])
{
StringTokenizer s1=new StringTokenizer("Welcome To Vjit");
while([Link]() )
{
[Link]([Link]());
}

StringTokenizer s2=new StringTokenizer("Hello;IT", ";");


while([Link]() )
{
[Link]([Link]());
}}}
Output:
Welcome
To
Vjit
Hello
IT

Department of Information Technology, VJIT Page 31


Java Programming Lab Manual

17. Write a program to read one line at a time, and write it to another file.
Program:
import [Link].*;
public class CharacterFileExample
{
public static void main(String[] args)
{
File inFile = new File("[Link]");
File outFile =new File("[Link]");
FileReader ins = null; //Create File Stream for Reading
FileWriter outs = null; //Create File Stream for Writing

try
{
ins = new FileReader(inFile);
outs = new FileWriter(outFile);

int ch;
while((ch = [Link]()) != -1)
[Link](ch);
}
catch(IOException e)
{
[Link](e);
[Link](-1);
}
finally
{
try
{
[Link]();
[Link]();
}

Department of Information Technology, VJIT Page 32


Java Programming Lab Manual

catch(IOException e)
{}
}
}
}
INPUT:
[Link] file data is
Hi
How are you?
OUTPUT:
open [Link] file for the result
[Link] data will be
Hi
How are you?

Department of Information Technology, VJIT Page 33


Java Programming Lab Manual

Week 10 & 11
18. Write a program to illustrate Event Handling (keyboard, Mouse events)
a) Keyboard Events program
Program:
import [Link].*; import [Link].*; import [Link].*;
public class KeyListenerExample extends JFrame implements KeyListener
{
Label l= new Label();; TextField tf= new TextField(); KeyListenerExample()
{
[Link](20,50,100,30); [Link](20,80,200,30); [Link](this);

add(l);
add(tf); setSize(300,300); setLayout(null); setVisible(true);
}
public void keyPressed(KeyEvent e)
{
[Link]("Key Pressed");
}
public void keyReleased(KeyEvent e)
{
[Link]("Key Released");
}
public void keyTyped(KeyEvent e)
{
[Link]("Key Typed");
}
public static void main(String[] args)
{
new KeyListenerExample();
}
}

Department of Information Technology, VJIT Page 34


Java Programming Lab Manual

Output:

Department of Information Technology, VJIT Page 35


Java Programming Lab Manual

b) MouseListener Event Program


Program:
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseListenerExample extends JFrame implements MouseListener
{
Label l =new Label();
MouseListenerExample()
{
[Link](20,50,100,20);
addMouseListener(this);
add(l);
setSize(300,300);
setLayout(null); setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
[Link]("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{
[Link]("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
[Link]("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{
[Link]("Mouse Pressed");
}

Department of Information Technology, VJIT Page 36


Java Programming Lab Manual

public void mouseReleased(MouseEvent e)


{
[Link]("Mouse Released");
}
public static void main(String[] args)
{
new MouseListenerExample();
}
}
Output

Department of Information Technology, VJIT Page 37


Java Programming Lab Manual

c) MouseMotionListener Event Handling


d) Program:
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseMotionListenerExample extends Frame implements MouseMotionListener
{
Label l=new Label();
MouseMotionListenerExample()
{
[Link](20,50,500,20); add(l);
addMouseMotionListener(this);
setSize(300,300); setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
[Link]("mouse dragging at "+[Link]()+","+[Link]());
}
public void mouseMoved(MouseEvent e)
{
[Link]("mouse moving at "+[Link]()+","+[Link]());
}
public static void main(String[] args)
{
new MouseMotionListenerExample();
}
}
Output:

Department of Information Technology, VJIT Page 38


Java Programming Lab Manual

Department of Information Technology, VJIT Page 39


Java Programming Lab Manual

19. Write a program to illustrate applet life cycle and parameter passing.
a) Implementing Applet Life cycle
Program:
/*
<html>
<body>
<applet code="MyApplet" width=500 height=500>
</applet>
</body>
</html>
*/
import [Link].*;
import [Link];
public class MyApplet extends Applet
{
public void init()
{
[Link]("Applet initialized");
}
public void start()
{
[Link]("Applet execution started");
}
public void stop()
{
[Link]("Applet execution stopped");
}
public void paint(Graphics g)
{
[Link]("Painting...");
}
public void destroy()
{

Department of Information Technology, VJIT Page 40


Java Programming Lab Manual

[Link]("Applet destroyed");
}}
Output:
Applet initialized
Applet execution started
Painting...
Painting...
Applet execution stopped
Applet destroyed

Department of Information Technology, VJIT Page 41


Java Programming Lab Manual

b) Parameter Passing to Applet


Program:
/*
<html>
<body>
<applet code="[Link]" width="300" height="300">
<param name="msg" value="welcome" >
</applet>
</body>
</html> */

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

public class EventApplet extends Applet implements ActionListener


{
Button b;
TextField tf;
String s;

public void init()


{
tf=new TextField();

[Link](30,40,150,20);

b=new Button("Click");

[Link](80,150,60,50);

add(b);
add(tf);

Department of Information Technology, VJIT Page 42


Java Programming Lab Manual

[Link](this);

setLayout(null);
}
public void paint(Graphics g)
{
s=getParameter("msg"); //parameter from applet
}
public void actionPerformed(ActionEvent e)
{
[Link](s);
}
}
Output:

Department of Information Technology, VJIT Page 43


Java Programming Lab Manual

Week 12:
20. Write a program to develop a calculator application using AWT.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code="Calculator" width="700" height="200">
</applet>*/
public class Calculator extends Applet implements ActionListener {
String msg = "";
TextField t1, t2, t3;
Button b1, b2, b3, b4;
Label l1, l2, l3;
public void init() {
l1 = new Label("First Number");
add(l1);
t1 = new TextField(15);
add(t1);
l2 = new Label("Second Number");
add(l2);
t2 = new TextField(15);
add(t2);
l3 = new Label("Result");
add(l3);
t3 = new TextField(15);
add(t3);
b1 = new Button("ADD");
add(b1);
[Link](this);
b2 = new Button("SUB");
add(b2);
[Link](this);
b3 = new Button("MULT");

Department of Information Technology, VJIT Page 44


Java Programming Lab Manual

add(b3);
[Link](this);
b4 = new Button("DIV");
add(b4);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
if ([Link]() == b1) {
int x = [Link]([Link]());
int y = [Link]([Link]());
int sum = x + y;
[Link](" " + sum);
}
if ([Link]() == b2) {
int x = [Link]([Link]());
int y = [Link]([Link]());
int sub = x - y;
[Link](" " + sub);
}
if ([Link]() == b3) {
int x = [Link]([Link]());
int y = [Link]([Link]());
int mul = x * y;
[Link](" " + mul);
}
if ([Link]() == b4) {
int x = [Link]([Link]());
int y = [Link]([Link]());
int div = x / y;
[Link](" " + div);
}
showStatus(" text & button example");
repaint();

Department of Information Technology, VJIT Page 45


Java Programming Lab Manual

}
}

Output:

Department of Information Technology, VJIT Page 46


Java Programming Lab Manual

Week 13
21. Write a program to illustrate JDBC.
Program:
import [Link].*;
class ExSqlCon
{
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection con=[Link](
"jdbc:mysql://localhost:3306/jplab","root","root");
// jplab is database name, root is username & password
Statement stmt=[Link]();
ResultSetrs=[Link]("select * from emp");
while([Link]())
[Link]([Link](1)+" "+[Link](2)+" "+[Link](3));
[Link]();
}
catch(Exception e){ [Link](e);}
}
}

OUTPUT:
1201 eswar 25
1202 banala 24
1203 Deva 24
1204 Kishan 24
MySql code:
⮚ create database jplab;
⮚ Use jplab;
⮚ Create table emp( id int(10), name varchar(40), age int(3));

Department of Information Technology, VJIT Page 47


Java Programming Lab Manual

⮚ Insert into emp values(1201,’eswar’,15);


⮚ Select * from emp

Department of Information Technology, VJIT Page 48

You might also like