0% found this document useful (0 votes)
3 views

JAVA practical

The document outlines the practical assignments for the Java Based Application Development course for the academic year 2023-2024, specifically for a student named Sahil Ramjan Saiyad. It includes a detailed index of practical tasks such as implementing constructor and method overloading, inheritance, abstract classes, user-defined exceptions, and various Java collections. Each practical is accompanied by source code examples and output expectations, demonstrating the application of Java programming concepts.

Uploaded by

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

JAVA practical

The document outlines the practical assignments for the Java Based Application Development course for the academic year 2023-2024, specifically for a student named Sahil Ramjan Saiyad. It includes a detailed index of practical tasks such as implementing constructor and method overloading, inheritance, abstract classes, user-defined exceptions, and various Java collections. Each practical is accompanied by source code examples and output expectations, demonstrating the application of Java programming concepts.

Uploaded by

armansaiyad1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

SATISH PRADHAN DNAYANASADHANA COLLEGE,THANE A.Y.

[2023-2024]
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Name: SAHIL RAMJAN SAIYAD Roll No.=52,Batch=B,Div=A
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
Subject= Java Based Application Development– Practical Std=S.Y.Bsc C.S.

Index
Sr. List of Practical Date Page Signature
No no.
1 a. Write a program to create a class and
implement the concepts of Constructor
Overloading, Method Overloading, Static
methods 11/07/2023 3-9
b. Write a program to implement the
concept of Inheritance and Method
Overriding
2 a. Write a program to implement the concepts
of Abstract classes and methods
18/07/2023 10-13
b. Write a program to implement the concept of
interfaces
3 Write a program to define user defined
exceptions and raise them as per the 25/07/2023 14-15
requirements
4 Write a program to demonstrate the
methods of: a. List interface
08/08/2023 16-21
b. Set interface
c. Map interface

5 Write a program using various swing


components to design Java application to 22/08/2023 22-26
accept a student's resume. (Design form)

6 a. Write a JDBC program that displays the


data of a given table
b. Write a JDBC program to return the
29/09/2023 27-30
data of a specified record from a given
table
c. Write a JDBC program to insert / update /

1
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

delete records into a given table

7 a. Construct a simple calculator using the


JAVA Swings with minimum
functionality.
b. Construct a GUI using JAVA Swings to 05/09/2023 31-39
accept details of a record of a given table and
submit it to the database using JDBC
technology on the click of a button.
8 Write Java application to encoding and
decoding JSON in Java. 26/10/2023 40-43

2
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 1
Aim:
a. Write a program to create a class and implement the concepts of
Constructor Overloading, Method Overloading, Static methods.

Method Overloading
Source Code:
methodOverloadEx.java
class Demo {
public void display()
{
System.out.println("in Demo display ");
}
public void display(int n)
{
System.out.println("in Demo display n : " + n);
}

public int Product(int a, int b, int c)


{

// int prod1 = a * b * c;
// return prod1;
return (a*b*c);
}

// Multiplying three double values.


public double Product(double a, double b, double c)
{
return a * b * c;
}
public void Product(int a, int b)
{
int prod1 = a * b;
System.out.println("Product of the two integer value :" +
prod1); }

3
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

}
class methodOverloadEx{
public static void main(String[] args)
{

Demo d = new Demo();

// calling display() method of Demo class


d.display();

// calling display(int n) method of Demo class


d.display(10);

int prod1 = d.Product(1, 2, 3);


System.out.println("Product of the three integer value :" +
prod1);
System.out.println("Product of the three double value :" + d.Product(1.0,
2.0, 3.0)); d.Product(20, 3);

}
}

Output:

Constructor Overloading
Source Code:
constructorOverloadEx.java
class Student {
//instance variables of the class
int id,age;
4
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

String name;
Student(){
age=18;
System.out.println("this a default constructor");
}

Student(int i, String name,int age){


id = i;
this.name = name;
this.age =age;
}
}
class constructorOverloadEx
{
public static void main(String[] args) {
//object creation
Student s = new Student();

System.out.println("\nDefault Constructor values: \n");


System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name+
"\nStudent Age : "+s.age);

System.out.println("\nParameterized Constructor values: \n");


Student s1 = new Student(10, "David",22);
System.out.println("Student Id : "+s1.id + "\nStudent Name :
"+s1.name+"\nStudent Age : "+s1.age);
}
}
Output:

Static method.
5
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Source code
import java.io.*;
class Demo{
static int a = 40;
int b = 50;
void simpleDisplay()
{
System.out.println(a);
System.out.println(b);
}
static void staticDisplay()
{
System.out.println(a);
}
static int id = 100;
static String name ="ABCD";
static void display()
{
System.out.println("static number
is"+id); System.out.println("static
string is"+name); }
void nonstatic()
{
display();
}
}
class StaticEx{
public static void
main(String[]args) {
Demo obj=new Demo();
obj.simpleDisplay();
Demo.staticDisplay();
obj.nonstatic();
Demo.display();
}
}
Output:

6
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

b.Write a program to implement the concept of inheritance and


Method Overriding 1.Single Inheritence
Source Code
import java.io.*;
import java.util.*;
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class SingleInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Output:

2. Multilevel Inheritence
Source code
import java.io.*;
import java.util.*;
class Animal{
void eat(){System.out.println("eating...");}
7
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class MultilevelInheritanceEx{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output

c.HIerarchical Inhritence
Source Code
Import java.io.*;
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class HierarchicalInheritanceEx{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
8
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

c.eat();
//c.bark();//C.T.Error
}}

Output:

9
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 2

Aim: a. Write a program to implement the concepts of Abstract


classes and methods

Source code
abstract class Bank{
abstract int getRateOfInterest();
void Display(){
System.out.println("Bank Details: ");}
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class AbstractionEx{
public static void main(String args[]){
Bank b;
b=new SBI();
b.Display();
System.out.println("Rate of Interest is:"+b.getRateOfInterest()+" %");
b=new PNB();
b.Display();
System.out.println("Rate of Interest is:"+b.getRateOfInterest()+" %");
}}

Output:

10
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

b. Write a program to implement the concept of interfaces


Source code

interface Vehicle{
void changeGear(int a);
void Speedup(int a);
void applyBreaks(int a);
}
class Bicycle implements Vehicle{
int speed;
int gear;
public void changeGear(int newGear){
gear=newGear;
}
public void Speedup(int increment){
speed=speed+increment;
}
public void applyBreaks(int decrement){
speed=speed+decrement;
}
public void printStates(){
System.out.println("speed:"+speed +"gear:"+gear);
}
}
class Bike implements Vehicle{
int speed;
int gear;
public void changeGear(int newGear){
gear=newGear;
11
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

}
public void Speedup(int increment){
speed=speed+increment;
}
public void applyBreaks(int decrement){
speed=speed+decrement;
}
public void printStates(){
System.out.println("speed:"+speed +"gear:"+gear);
}
}
class InterfaceEx{
public static void main(String []args){
Bicycle b = new Bicycle();
b.changeGear(2);
b.Speedup(2);
b.applyBreaks(1);
System.out.println("Bicycle present State:");
b.printStates();

Bike b2= new Bike();


b2.changeGear(1);
b2.Speedup(4);
b2.applyBreaks(3);
System.out.println("Bike present State:");
b2.printStates();
}
}

Output:

12
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

13
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 3
Aim: Write a program to define user defined exceptions and raise
them as per the requirements.

Source Code:
import java.io.*;
class EmployeeException extends Exception
{
public EmployeeException(String s)
{
super(s);
}
}
class ExpHandling
{
void EmpIDcheck(int EmpID) throws
EmployeeException
{
if(EmpID<=0 || EmpID>999)
throw new EmployeeException("Invalid Employee ID");
}
public static void main(String[]args)
{
ExpHandling eh=new ExpHandling();
try
{
eh.EmpIDcheck(0);
}
catch(EmployeeException e)
{
System.out.println("Exception caught");
System.out.println(e.getMessage());
}
}
}

Output:

14
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

15
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 4
Aim: Write a program to demonstrate the
methods of: a. List interface
import java.util.ArrayList;
import java.util.List;
public class ListExample{
public static void main(String[]args){
List l1=new ArrayList();
l1.add(0,1);
l1.add(1,2);
l1.add(2,3);
System.out.println("list="+l1);
List l2=new ArrayList();
l2.add(11);
l2.add(12);
l2.add(13);
l1.addAll(1,l2);
System.out.println("new list l1="+l1);
System.out.println("get element at index 2="+l1.get(2));
System.out.println("index of 12 object="+l1.indexOf(12));
l1.remove(1);
System.out.println("after removing 2 object list becomes="+l1);
l1.set(1,4);
System.out.println("list changes to="+l1);
l1.clear();
System.out.println("list is cleared="+l1);
}
}

Output:
list is cleared=[]list=[1, 2, 3]
new list l1=[1, 11, 12, 13, 2, 3]
get element at index 2=12
index of 12 object=2
after removing 2 object list becomes=[1, 12, 13, 2, 3]
list changes to=[1, 4, 13, 2, 3]

For iterator
16
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListEx{
public static void main(String[]args){
ArrayList<String>list=new ArrayList<>();//creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
ArrayList<Integer>al=new ArrayList<>();
//Appending new elements at the end of the list
for(int i=1;i<=5;i++)
al.add(i);
//printing elements
System.out.println(al);
//Remove element at index 3
al.remove(3);
//Display the ArrayList
//after deletion
System.out.println(al);
//Printing elements one by one
for (int i=0;i<al.size();i++)
System.out.print(al.get(i)+" ");
}
}

Output:
Ravi
Vijay
Ravi
Ajay
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1235

For Linked list


17
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

package linkedlistex;
import java.util.Iterator;
import java.util.linkedlist;
public class LinkedListEx{
public static void main(String[]args){
LinkedList<Integer>l1=new LinkedList<Integer>();
for(int i=1;i<=5;i++)
ILadd(i);
System.out.println(l1);
l1.remove(3);
System.out.println(l1);
for(int i=0;i<l1.size();i++)
System.out.print(l1.get(i)+" ");
LinkedList<String>a1=new LinkedList<String>();
a1.add("Ravi");
a1.add("Vijay");
a1.add("Ravi");
a1.add("Ajay");
Iterator<String>itr=a1.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:
run:
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 Ravi
Vijay
Ravi
Ajay

b. Set interface
Using Hashset
package hashsetex;
import java.util.HashSet;
import java.util.Iterator;
public class Hashsetex {
public static void main(String[] args) {
// TODO code application logic here
18
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

HashSet<Integer>evenNumber=new HashSet<>();
evenNumber.add(2);
evenNumber.add(4);
evenNumber.add(6);
System.out.println("HashSet:"+evenNumber);
HashSet<Integer>numbers=new HashSet<>();
numbers.addAll(evenNumber);
numbers.add(5);
System.out.println("New HashSet:"+numbers);
Iterator<Integer>itr=numbers.iterator();
System.out.print("HAshSet using iterator:");
while(itr.hasNext()){
System.out.print(itr.next());
}
boolean value1 = numbers.remove(5);
System.out.println("is 5 removed?"+value1);
System.out.println(numbers);
boolean value2 = numbers.removeAll(numbers);
System.out.println("Are All elements removed?"+value2);
}
}

Output:
HashSet:[2, 4, 6]
New HashSet:[2, 4, 5, 6]
HAshSet using iterator:2456is 5 removed?true
[2, 4, 6]
Are All elements removed?true

Using Treeset
package treesetex;
import java.util.TreeSet;
import java.util.Iterator;
public class TreeSetEx {
public static void main(String[] args) {
TreeSet<Integer>evenNumber=new TreeSet<>();
evenNumber.add(2);
evenNumber.add(4);
evenNumber.add(6);
System.out.println("TreeSet:"+evenNumber);
TreeSet<Integer>numbers=new TreeSet<>();
numbers.addAll(evenNumber);
numbers.add(1);
19
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

System.out.println("New TreeSet:"+numbers);
Iterator<Integer>itr=numbers.iterator();
System.out.print("TreeSet using iterator:");
while(itr.hasNext()){
System.out.print(itr.next());
}
boolean value1 = numbers.remove(5);
System.out.println("is 5 removed?"+value1);
System.out.println(numbers);
boolean value2 = numbers.removeAll(numbers);
System.out.println("Are all elements removed?"+value2);
}
}

Output:
TreeSet:[2, 4, 6]
New TreeSet: [1, 2, 4, 6]
TreeSet using Iterator:
1
2
4
6
Is % removed? false
[1, 2, 4, 6]
Are all elements removes? True

c. Map interface
package hashmapsex;
import java.util.HashMap;
/**
*
* @author spdc
*/
public class Hashmapsex {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
HashMap<Integer, String> languages = new HashMap<>();

languages.put(1, "Java");
20
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);

// return set view of keys


// using keySet()
System.out.println("Keys: " + languages.keySet());

// return set view of values


// using values()
System.out.println("Values: " + languages.values());

// return set view of key/value pairs


// using entrySet()
System.out.println("Key/Value mappings: " + languages.entrySet());

// change element with key 2


languages.replace(2, "C++");
System.out.println("HashMap using replace(): " + languages);

// remove element associated with key 2


String value = languages.remove(2);
System.out.println("Removed value: " + value);

System.out.println("Updated HashMap: " + languages);


}

Output:
HashMap: {1=Java, 2=Python, 3=JavaScript}
Keys: [1, 2, 3]
Values: [Java, Python, JavaScript]
Key/Value mappings: [1=Java, 2=Python, 3=JavaScript]
HashMap using replace(): {1=Java, 2=C++, 3=JavaScript}
Removed value: C++
Updated HashMap: {1=Java, 3=JavaScript

21
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 5
Aim: Write a program using various swing components to design
Java application to accept a student's resume. (Design form)
Source Code:
package rform;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class RForm extends JFrame implements ActionListener {


JFrame main_frame;
JLabel lname, lcontact, lemail;
JLabel lschool, lsy, lsa;
JLabel ljcollege, ljcy, ljca;
JLabel lscollege, lscy, lsca;
JLabel laddress, lskills;
JTextField sname, scontact, semail;
JTextField sschool, ssy, ssa;
JTextField sjcollege, sjcy, sjca;
JTextField sscollege, sscy, ssca;
JTextArea saddress, sskills, result;
JRadioButton male;
JRadioButton female;
ButtonGroup gender;
JButton submit, reset;
RForm() {
super();
main_frame = new JFrame("Resume Form");
lname = new JLabel("Name");
lcontact = new JLabel("Contact Number");
lemail = new JLabel("EMail");

lname.setBounds(10, 20, 300, 30);


lcontact.setBounds(10, 50, 300, 30);
lemail.setBounds(10, 80, 300, 30);

lschool = new JLabel("School Name");


lsy = new JLabel("Passing Year");
lsa = new JLabel("Aggregate");

lschool.setBounds(10, 250, 200, 50);


lsy.setBounds(310, 250, 200, 50);
lsa.setBounds(610, 250, 200, 50);
22
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

ljcollege = new JLabel("Jr. College Name");


ljcy = new JLabel("Passing Year");
ljca = new JLabel("Aggregate");

ljcollege.setBounds(10, 350, 200, 50);


ljcy.setBounds(310, 350, 200, 50);
ljca.setBounds(610, 350, 200, 50);

lscollege = new JLabel("Graduation College Name");


lscy = new JLabel("Passing Year");
lsca = new JLabel("Aggregate");

lscollege.setBounds(10, 450, 200, 50);


lscy.setBounds(310, 450, 200, 50);
lsca.setBounds(610, 450, 200, 50);

laddress = new JLabel("Address");


lskills = new JLabel("Skills");

laddress.setBounds(10, 140, 100, 30);


lskills.setBounds(300, 140, 100, 30);

sname = new JTextField();


scontact = new JTextField();
semail = new JTextField();

sname.setBounds(220, 10, 300, 30);


scontact.setBounds(220, 40, 300, 30);
semail.setBounds(220, 70, 300, 30);

sschool = new JTextField();


ssy = new JTextField();
ssa = new JTextField();

sschool.setBounds(10, 300, 300, 30);


ssy.setBounds(310, 300, 300, 30);
ssa.setBounds(610, 300, 300, 30);

sjcollege = new JTextField();


sjcy = new JTextField();
sjca = new JTextField();

sjcollege.setBounds(10, 400, 300, 30);


sjcy.setBounds(310, 400, 300, 30);
23
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

sjca.setBounds(610, 400, 300, 30);

sscollege = new JTextField();


sscy = new JTextField();
ssca = new JTextField();

sscollege.setBounds(10, 500, 300, 30);


sscy.setBounds(310, 500, 300, 30);
ssca.setBounds(610, 500, 300, 30);

saddress = new JTextArea();


sskills = new JTextArea();

saddress.setBounds(10, 170, 275, 70);


sskills.setBounds(300, 170, 275, 70);

male = new JRadioButton("Male");


male.setSelected(false);
female = new JRadioButton("Female");
female.setSelected(false);

male.setBounds(220, 110, 100, 30);


female.setBounds(320, 110, 100, 30);

gender = new ButtonGroup();

submit = new JButton("Submit");


submit.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);

submit.setBounds(275, 550, 100, 30);


reset.setBounds(425, 550, 100, 30);

result = new JTextArea();


result.setLocation(580, 175);

gender.add(male);
gender.add(female);

main_frame.add(lname);
main_frame.add(lcontact);
main_frame.add(lemail);
main_frame.add(sname);
main_frame.add(scontact);
24
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

main_frame.add(semail);
main_frame.add(laddress);
main_frame.add(lskills);
main_frame.add(saddress);
main_frame.add(sskills);
main_frame.add(male);
main_frame.add(female);
main_frame.add(lschool);
main_frame.add(lsy);
main_frame.add(lsa);
main_frame.add(sschool);
main_frame.add(ssy);
main_frame.add(ssa);
main_frame.add(ljcollege);
main_frame.add(ljcy);
main_frame.add(ljca);
main_frame.add(sjcollege);
main_frame.add(sjcy);
main_frame.add(sjca);
main_frame.add(lscollege);
main_frame.add(lscy);
main_frame.add(lsca);
main_frame.add(sscollege);
main_frame.add(sscy);
main_frame.add(ssca);
main_frame.add(submit);
main_frame.add(reset);
main_frame.add(result);
main_frame.setSize(1000, 1000);
main_frame.setLayout(null);
main_frame.setVisible(true);
main_frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit) {
result.setText("Hemlo");
} else {
sname.setText("");
scontact.setText("");
semail.setText("");
sschool.setText("");
ssy.setText("");
ssa.setText("");
25
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

sjcollege.setText("");
sjcy.setText("");
sjca.setText("");
sscollege.setText("");
sscy.setText("");
ssca.setText("");
saddress.setText("");
sskills.setText("");
male.setSelected(false);
female.setSelected(false);
}
}
public static void main(String[] args) throws Exception {
RForm rForm = new RForm();
}
}

Output:

26
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 6

Aim: a. Write a JDBC program that displays the data of a given


table. b. Write a JDBC program to return the data of a
specified record from a given table
c. Write a JDBC program to insert / update / delete records
into a given table

a. Write a JDBC program that displays the data of a given table.


b. Write a JDBC program to return the data of a specified record from a given
table
Source Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jdbcex1;
import java.sql.*;
/**
*
* @author spdc
*/
public class JDBCEx1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
//1. Create “Emp” table in mysql
//Create table emp(emp_id int, empname varchar(10), email varchar(30),
city varchar(10));
//insert into emp values(101,'snehal','snehal@gmail.com','mum');

//Loading the mysql driver


Class.forName("com.mysql.jdbc.Driver");

//Create the connection object


27
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","
12345");
//here mysql is database name, root is username and password is 12345

// Display the record


String sql1 = "SELECT * FROM Emp";
String sql2 = "SELECT * FROM Emp where emp_id=101";
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(sql1);

while (result.next())
{
System.out.println (result.getInt(1)+" "+ result.getString(2)+" "+
result.getString(3)+" "+ result.getString(4));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}

Output:

1 xyz xyz@gmail.com thane


2 abc abc@gmail.com mumbai
3 efg efg@gmail.com pune
101 snehal snehal@gmail.com mum
BUILD SUCCESSFUL (total time: 0 seconds)
C.Write a JDBC program to insert / update / delete records into a given
table.
package sql_operationex;
import java.sql.*;

public class SQL_OperationEx {


//1. Create “Emp” table in mysql
//Create table emp(emp_id int, empname varchar(10), email varchar(30), city
varchar(10));

28
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

//insert into emp values(101,'snehal','snehal@gmail.com','mum');

//Loading the mysql driver


Class.forName("com.mysql.jdbc.Driver");

//Create the connection object


Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","12345
");
//here mysql is database name, root is username and password is 12345
//Insert the record
String sql = "INSERT INTO emp (emp_id, empname, email, city) VALUES
(?, ?, ?, ?)";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, 105);
statement.setString(2, "sneha");
statement.setString(3, "sneha@gmail.com");
statement.setString(4, "Pune");

int rowsInserted = statement.executeUpdate();


if (rowsInserted > 0)
{
System.out.println("A new employee was inserted successfully!\n");
}
// Display the record
String sql1 = "SELECT * FROM Emp";
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(sql1);

while (result.next())
{
System.out.println (result.getInt(1)+" "+
result.getString(2)+" "+
result.getString(3)+" "+
result.getString(4));
}

//Update the record


String sql2 = "Update Emp set email = ? where empname = ?";
PreparedStatement pstmt = con.prepareStatement(sql2);
pstmt.setString(1, "snehal09@gmail.com");
29
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

pstmt.setString(2, "sneha");
int rowUpdate = pstmt.executeUpdate();
if (rowUpdate > 0)
{
System.out.println("\nRecord updated successfully!!\n");
}

//Delete the record


String sql3 = "DELETE FROM Emp WHERE empname=?";
PreparedStatement statement1 = con.prepareStatement(sql3);
statement1.setString(1, "sneha");

int rowsDeleted = statement1.executeUpdate();


if (rowsDeleted > 0)
{
System.out.println("A Employee was deleted successfully!\n");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

Output:

30
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 7

Aim:
a. Construct a simple calculator using the JAVA Swings with
minimum functionality.
b. Construct a GUI using JAVA Swings to accept details of a
record of a given table and submit it to the database using
JDBC technology on the click of a button.

Source Code:

a. Construct a simple calculator using the JAVA Swings with minimum


functionality.

Source Code:

package calculator;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{


JButton btnPlus ,btnMinus,btnMultiply,btnDivide,btnEquals,btnClear;
JButton b[]=new JButton[10];
int i,r,n1,n2;
JTextField res;
char op;
public Calculator()
{
super("Calulator");
setLayout(new BorderLayout());
JPanel p=new JPanel();

p.setLayout(new GridLayout(4,4));
for(int i=0;i<=9;i++)
{
b[i]=new JButton(i+"");
p.add(b[i]);
31
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

b[i].addActionListener(this);

}
btnPlus=new JButton("+");
p.add(btnPlus);
btnPlus.addActionListener(this);

btnMinus=new JButton("-");
p.add(btnMinus);
btnMinus.addActionListener(this);

btnMultiply=new JButton("*");
p.add(btnMultiply);
btnMultiply.addActionListener(this);

btnDivide=new JButton("/");
p.add(btnDivide);
btnDivide.addActionListener(this);

btnEquals=new JButton("=");
p.add(btnEquals);
btnEquals.addActionListener(this);

btnClear=new JButton("C");
p.add(btnClear);
btnClear.addActionListener(this);

res=new JTextField(20);
res.setEditable(false);

add(p,BorderLayout.CENTER);
add(res,BorderLayout.NORTH);
setVisible(true);
setSize(300,300);
}
public void actionPerformed(ActionEvent ae)
{
JButton pb=(JButton)ae.getSource();
if(pb==btnClear)
{
r=n1=n2=0;
res.setText("");
32
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

}
else
if(pb==btnEquals)
{
n2=Integer.parseInt(res.getText());
eval();
res.setText(""+r);
}

else
{
boolean opf=false;
if(pb==btnPlus)
{ op='+';
opf=true;
}
if(pb==btnMinus)
{ op='-';opf=true;}
if(pb==btnMultiply)
{ op='*';opf=true;}
if(pb==btnDivide)
{ op='/';opf=true;}

if(opf==false)
{
for(i=0;i<10;i++)
{
if(pb==b[i])
{
String t=res.getText();
t+=i;
res.setText(t);
}
}
}
else
{
n1=Integer.parseInt(res.getText());
res.setText("");
}
}
33
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

}
int eval()
{
switch(op)
{
case '+': r=n1+n2; break;
case '-': r=n1-n2; break;
case '*': r=n1*n2; break;
case '/': r=n1/n2; break;
}
return 0;
}
public static void main(String[] args) {
new Calculator();
}
}

Output:

b. Construct a GUI using JAVA Swings to accept details of a record of a given


table and submit it to the database using JDBC technology on the click of a
button.

Source Code:

34
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
package swingjdbcex;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;

public class Swingjdbcex extends JFrame implements ActionListener{


public static final String DBURL = "jdbc:mysql://localhost:3306/mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "12345";

JFrame main_frame;
JLabel lblid,lblname, lblemail, lblcity;
JTextField txtid,txtname, txtemail, txtcity;
JButton btnSubmit, btnreset,btnshow,btnDelete;//btnUpdate;

public static int id;


public static String name,email,city;
public Swingjdbcex()
{
super("Employee Form");
setLayout(new BorderLayout());
JPanel p=new JPanel();
p.setLayout(new GridLayout(4,4));

lblid = new JLabel("ID");


lblname = new JLabel("Name");
lblemail = new JLabel("EMail");
lblcity = new JLabel("City");

lblid.setBounds(50,20,300,30);
lblname.setBounds(50,50,300,30);
lblemail.setBounds(50,80,300,30);
lblcity.setBounds(50,100,300,30);

txtid = new JTextField();


txtname = new JTextField();
txtemail = new JTextField();
txtcity = new JTextField();

txtid.setBounds(175,10,300,30);
txtname.setBounds(175,40,300,30);
txtemail.setBounds(175,70,300,30);
txtcity.setBounds(175,100,300,30);

btnSubmit = new JButton("Submit");


btnreset = new JButton("Reset");
35
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
btnshow = new JButton("show");
btnDelete = new JButton("Delete");
//btnUpdate = new JButton("Update");

btnSubmit.setBounds(50,200,100,30);
btnshow.setBounds(175,200,100,30);
btnDelete.setBounds(300,200,100,30);
btnreset.setBounds(420,200,100,30);
//btnUpdate.setBounds(450,250,100,30);

add(lblid);
add(lblname);
add(lblemail);
add(lblcity);

add(txtid);
add(txtname);
add(txtemail);
add(txtcity);

add(btnSubmit);
add(btnreset);
add(btnshow);
add(btnDelete);
//add(btnUpdate);
btnSubmit.addActionListener(this);
btnreset.addActionListener(this);
btnshow.addActionListener(this);
btnDelete.addActionListener(this);
// btnUpdate.addActionListener(this);
setSize(600,300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent ae)
{
JButton pb=(JButton)ae.getSource();

if(pb==btnSubmit)
{
btnSubmit_ActionPerformed();
}
else if(pb==btnshow)
{
btnshow_ActionPerformed();
}
36
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
else if(pb==btnDelete)
{
btnDelete_ActionPerformed();
}
else
{
txtid.setText("");
txtname.setText("");
txtemail.setText("");
txtcity.setText("");
}

}
private void btnSubmit_ActionPerformed()
{
id=Integer.parseInt(txtid.getText());
name=(txtname.getText());
email=(txtemail.getText());
city=(txtcity.getText());
try
{
//1. Create “Emp” table
//Create table emp (emp_id number, empname varchar2(10), email varchar2(30), city
varchar2(10));
//Loading the driver
Class.forName("com.mysql.jdbc.Driver");
//Create the connection object
Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
//Insert the record
String sql = "INSERT INTO emp (emp_id, empname, email, city) VALUES (?, ?, ?,
?)";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, id);
statement.setString(2, name);
statement.setString(3, email);
statement.setString(4, city);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0)
System.out.println("A new employee was inserted successfully!\n");

txtid.setText("");
txtname.setText("");
txtemail.setText("");
txtcity.setText("");
}
catch(Exception ex)
{
ex.printStackTrace();
37
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
}

}
private void btnshow_ActionPerformed()
{
try
{
//1. Create “Emp” table
//Create table emp (emp_id number, empname varchar2(10), email varchar2(30), city
varchar2(10));
//Loading the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Create the connection object
Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

// Display the record


String sql1 = "SELECT * FROM Emp";
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(sql1);

while (result.next())
{
System.out.println (result.getInt("emp_id")+" "+result.getString("empname")+"
"+
result.getString(3)+" "+
result.getString(4));
}

//con.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}

private void btnDelete_ActionPerformed()


{
id=Integer.parseInt(txtid.getText());
try
{

//Loading the driver


Class.forName("oracle.jdbc.driver.OracleDriver");
//Create the connection object
Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

//Delete the record


String sql3 = "DELETE FROM Emp WHERE emp_id=?";
38
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
PreparedStatement statement1 = con.prepareStatement(sql3);
statement1.setInt(1, id);

int rowsDeleted = statement1.executeUpdate();


if (rowsDeleted > 0)
{
System.out.println("A Employee was deleted successfully!\n");
}
else
System.out.println("No Record found!\n");
//con.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) {
// TODO code application logic here
new Swingjdbcex();
}
}

Output:

39
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

Practical 8
Aim: Write Java application to encoding and decoding JSON in Java.
Source Code:

package jsonexample1;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import java.util.*;

public class JsonExample1 {

public static void main(String[] args) {


// TODO code application logic here

//simple example to encode JSON object in java.


JSONObject jsonObj=new JSONObject();
jsonObj.put("name","Rutuja");
jsonObj.put("age",new Integer(27));
jsonObj.put("is_manager",new Boolean(true));
jsonObj.put("salary",new Integer(60000));
System.out.println("Using JSONObject "+jsonObj);

//simple example to encode JSON array in java.


JSONArray arrObj = new JSONArray();
arrObj.add("kunal");
arrObj.add(new Integer(27));
arrObj.add(new Double(600000));
System.out.println("Using JSONArray "+arrObj);

//Java JSON Array Encode using List


List arrList = new ArrayList();
arrList.add("ravi");
arrList.add(new Integer(27));
arrList.add(new Double(60000));
System.out.println("Using ArrayList "+arrList);
//converts obj to string
String jsonText = JSONValue.toJSONString(arrList);
System.out.println("Converting obj to text "+jsonText);

40
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.

//to decode JSON string in java.


String s="{\"name\":\"ravi\",\"salary\":600000.0,\"age\":27}";
Object obj=JSONValue.parse(s);//converts string to object
JSONObject JsonObj= (JSONObject) obj;
String name = (String) JsonObj.get("name");
double salary = (Double) JsonObj.get("salary");
long age = (Long) JsonObj.get("age");
System.out.println("Decoding JSON string in java ");
System.out.println(name+" "+salary+" "+age);
}
}

Output:

41

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy