JAVA practical
JAVA practical
[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
1
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
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);
}
// int prod1 = a * b * c;
// return prod1;
return (a*b*c);
}
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)
{
}
}
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");
}
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.
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
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.
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();
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
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);
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.*;
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
/**
* @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');
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","
12345");
//here mysql is database name, root is username and password is 12345
while (result.next())
{
System.out.println (result.getInt(1)+" "+ result.getString(2)+" "+
result.getString(3)+" "+ result.getString(4));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
Output:
28
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
while (result.next())
{
System.out.println (result.getInt(1)+" "+
result.getString(2)+" "+
result.getString(3)+" "+
result.getString(4));
}
pstmt.setString(2, "sneha");
int rowUpdate = pstmt.executeUpdate();
if (rowUpdate > 0)
{
System.out.println("\nRecord updated successfully!!\n");
}
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:
Source Code:
package calculator;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
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:
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.*;
JFrame main_frame;
JLabel lblid,lblname, lblemail, lblcity;
JTextField txtid,txtname, txtemail, txtcity;
JButton btnSubmit, btnreset,btnshow,btnDelete;//btnUpdate;
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.setBounds(175,10,300,30);
txtname.setBounds(175,40,300,30);
txtemail.setBounds(175,70,300,30);
txtcity.setBounds(175,100,300,30);
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);
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();
}
}
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.*;
40
Name: SAHIL RAMJAN SAIYAD Roll No.: 52 Div.:=A Batch: B
Subject: Java Based Application Development Practical Std: S.Y.Bsc.C.S.
Output:
41