You are on page 1of 43

ACADEMIC YEAR 2020-2021

Roll Number : 335


Name : Rishikesh Jagannath Jangam.
MCA- I Semester
Course – Java Programming
Practical Journals

1
Dr. D. Y. Patil Unitech Society’s
Dr. D.Y. PATIL INSTITUTE OF MANAGEMENT & RESEARCH,
Sant Tukaram Nagar, Pimpri, Pune-411018, Maharashtra, India.
(Approved by All India Council for Technical Education & Recognized by the
Savitribai Phule Pune University)

Date: 18-05-2021

CERTIFICATE

This is to certify that Ms./Mr. ____Rishikesh Jagannath Jangam______

_____________________________________________ has successfully


completed the Practicals on IT 11 L : Java Programming as a partial
fulfilment of their Master of Computer Application (Sem-I ) under the
curriculum of Savitribai Phule Pune University(SPPU), Pune for the
academic year 2020-21.

Mr. Keshav Thite Ms.Swati Narkhede Dr. Shikha Dubey

Subject Incharge Course Coordinator MCA,HOD

2
INDEX

Sr. No. Program Name Page


No.
1 Design an application by using array. 1
2 Write a program to differentiate static and non-static members of class. 3
3 Design java application to implement constructor overloading. 4
4 Implementation of package, Interface and abstract class 6
5 Write java applications to implement inheritance. 7
6 Write a java application to implement runtime polymorphism. 10
7 Design application using String, StringBuilder, StringTokenizer. 12
8 Write java programs to perform read/write operations on file. 14
9 Test any five of standard exception and user Defined Custom Exceptions in java. 16
10 Threads creation and design applications by using Extending the Thread class/ 21
implementing the Runnable Interface.
11 Write application for thread synchronization. 22
12 Write application for Inter thread collection. 24
13 Design java application using Collection in java such as Array List, Link List 25
14 Design java application using Collection in java such as Queue. 27
15 Design java application using Collection in java such as Map 28
16 Design GUI based java application using AWT, Event Handling. 30
17 Design a JDBC application to retrieve the data from SQL table. 32
18 Design a JDBC application to execute non select queries. 34
19 Design and implement servlet applications. 36
20 Design and implement JSP applications 39

3
_____________________________________________________________________________________

Program 01. Design an application by using array

_____________________________________________________________________________________

import java.util.*;
public class Prog1 {

public static void main(String[] args)


{

System.out.println("---------------------------------------------------");
System.out.println("\t\t Program 2");
System.out.println("---------------------------------------------------");
int soa=0;
System.out.print("How many numbers you want in an array : ");
Scanner obj = new Scanner(System.in);
soa=obj.nextInt();
int arr[]=new int[soa];
boolean valPresent=false;
System.out.println("---------------------------------------------------");
System.out.println("Enter Numbers");
for (int i=0;i<arr.length;i++)
{
System.out.print("Num "+(i+1)+" : ");
arr[i]=obj.nextInt();

}
System.out.println("---------------------------------------------------");
System.out.println("Entered Numbers are");
for (int i=0;i<arr.length;i++)
{
System.out.println("Num "+(i+1)+": "+arr[i]);
}
System.out.println("---------------------------------------------------");
int searchItem=0;
System.out.print("Enter Number to search in array :");
searchItem=obj.nextInt();
System.out.println("---------------------------------------------------");
System.out.println("---------------Search Result--------------------");
for (int i=0;i<arr.length;i++)
{
if(arr[i]==searchItem)
{
valPresent=true;

1
System.out.println("searchItem is present in an array");
break;
}
}
if(!valPresent)
{
System.out.println("searchItem is not present in an array");
}
System.out.println("---------------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

2
_____________________________________________________________________________________

Program 02. Write a program to differentiate static and non-static members of class.

_____________________________________________________________________________________

public class Prog2


{
public int counter = 0;
public static int staticCounter = 0;

public Prog2()
{
counter++;
staticCounter++;
}

public static void main(String args[])


{
Prog2 tester = new Prog2();
Prog2 tester1 = new Prog2();
Prog2 tester2 = new Prog2();
System.out.println("Counter: " + tester2.counter);
System.out.println("Static Counter: " + tester2.staticCounter);
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

3
_____________________________________________________________________________________

Program 03. Design java application to implement constructor overloading.

_____________________________________________________________________________________

import java.util.*;
public class Prog3
{
public static void main(String[] args)
{
System.out.println("----------------------------------------------");
System.out.println("------------------Program3--------------------");
System.out.println("----------------------------------------------\n");
Person p1=new Person();
System.out.println("***Default Constructor Object");
System.out.println("P1 object");
p1.printPersonDetails();
Person p2=new Person(1);
System.out.println("***One Parameter Constructor Object");
System.out.println("P2 object");
p2.printPersonDetails();
Person p3=new Person(2,"Rishikesh");
System.out.println("***Two Parameterized Constructor Object");
System.out.println("P3 object");
p3.printPersonDetails();
System.out.println("----------------------------------------------");
}
}
public class Person
{
int number;
String name;

Person()
{
this.number=00;
this.name="Default";
}

Person(int number)
{
this.number=number;
this.name="Default";
}

4
Person(int number,String name)
{
this.number=number;
this.name=name;
}

public void printPersonDetails()


{
System.out.println("Person Number :"+this.number);
System.out.println("Person Name :"+this.name);
System.out.println("---------------------------------------------");
}
}

------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

5
_____________________________________________________________________________________

Program 04. Implementation of package, Interface and abstract class

_____________________________________________________________________________________
package Vehicle;

public interface Vehicles


{
public void run();
public void speed();
}

package Vehicle;

public class Bike implements Vehicles


{
public void run()
{
System.out.println("Bike is running.");
}
public void speed()
{
System.out.println("Speed of Bike: 50 Km/h");
}
public static void main(String args[])
{
Bike bike = new Bike();
bike.run();
bike.speed();
}
}

------------------------------------------------------------------------------------------------------------------------------------------

Output

Bike is running.
Speed of Bike: 50 Km/h

------------------------------------------------------------------------------------------------------------------------------------------

6
_____________________________________________________________________________________

Program 05. Write java applications to implement inheritance.

_____________________________________________________________________________________

public class Prog5 extends Student


{
public int p5val;

Prog5()
{
super();
this.p5val=00;
}
public void printProg5Vals()
{
super.printStudentDetails();
System.out.println("P5val variable Value ="+1);
}
public static void main(String[] args)
{

System.out.println("----------------------------------------------");
System.out.println("------------------Program5--------------------");
System.out.println("----------------------------------------------\n");
Person p1=new Person();
System.out.println("****No Inheritance");
System.out.println("P1 object Of Class Person");
p1.printPersonDetails();
System.out.println("----------------------------------------------\n");
Student p2=new Student();
System.out.println("****Single Inheritance");
System.out.println("P2 object Of Class Student");
p2.printStudentDetails();
System.out.println("----------------------------------------------\n");
Prog5 p3=new Prog5();
System.out.println("****Single Inheritance");
System.out.println("P3 object Of Class Prog5");
p3.printProg5Vals();
System.out.println("----------------------------------------------");
}
}
public class Person
{
public String gender;

7
public String name;
Person()
{
this.name="Default";
this.gender="Default";
}
Person(String name, String gender)
{
this.name=name;
this.gender=gender;
}

public void printPersonDetails()


{
System.out.println("Person Name :"+this.name);
System.out.println("Person Gender :"+this.gender);
}
}
public class Student extends Person
{
public int Roll_Number;

Student()
{
super();
this.Roll_Number=111;
}

Student(int Roll_Number)
{
super();
this.Roll_Number=Roll_Number;
}

public void printStudentDetails()


{
System.out.println("Student Roll_Number :"+this.Roll_Number);
super.printPersonDetails();

}
}
------------------------------------------------------------------------------------------------------------------------------------------

8
Output

------------------------------------------------------------------------------------------------------------------------------------------

9
_____________________________________________________________________________________

Program 06. Write a java application to implement runtime polymorphism.

_____________________________________________________________________________________

public class Prog6


{
public static void main (String[] args)
{
System.out.println("----------------------------------------------");
System.out.println("------------------Program 6-------------------");
System.out.println("----------------------------------------------\n");

System.out.println("*** Object of Sqare class implementing area abstract method");


System.out.println("*** Area Of Square");
Square s1=new Square(8);
s1.area();
System.out.println("----------------------------------------------\n");
System.out.println("*** Object of Rectangle class implementing area abstract method");
System.out.println("*** Area Of Rectangle");
Rectangle r1=new Rectangle(8,9);
r1.area();
System.out.println("----------------------------------------------\n");
}
}

public class Rectangle extends Shape


{
float length, breadth;

Rectangle(float l, float b)
{
this.length=l;
this.breadth=b;
}

void area()
{
System.out.println("length is :"+length+" cm");
System.out.println("breadth is :"+breadth+" cm");
System.out.println("Area of Rectangle is :"+(length*breadth)+" cm");
}
}

public class Square extends Shape

10
{
float side;

Square(float s)
{
this.side=s;
}

void area()
{
System.out.println("side is :"+side+" cm");
System.out.println("Area of Square is :"+(side*4)+" cm");
}
}
public abstract class Shape
{
abstract void area();
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

11
_____________________________________________________________________________________

Program 07. Design application using String, StringBuilder, StringTokenizer.


_____________________________________________________________________________________

import java.util.*;
public class Prog7
{
public static void main(String[] args)
{

System.out.println("----------------------------------------------");
System.out.println("------------------Program7--------------------");
System.out.println("----------------------------------------------");
System.out.println("--------Application Of String Tokenizer--------");
System.out.println("----------------------------------------------");
System.out.println("String Tokenizer For WHitespaces");
StringTokenizer st = new StringTokenizer("Rishikesh Jagannath Jangam From Pune"," ");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
System.out.println("----------------------------------------------");
System.out.println("String Tokenizer For Comma");
StringTokenizer st2 = new StringTokenizer("Rishikesh,Jagannath,Jangam,From,Pune");
System.out.println("Next token is : " + st2.nextToken(","));
System.out.println("----------------------------------------------\n");
System.out.println("----------------------------------------------");
System.out.println("--------Application Of String Builder--------");
System.out.println("----------------------------------------------");
StringBuilder s=new StringBuilder("Rishikesh");
s.reverse();
System.out.println("Reverse of string using String builder");
System.out.println("Reverse String s is : "+s);
System.out.println("----------------------------------------------\n\n");

}
}
------------------------------------------------------------------------------------------------------------------------------------------

12
Output

------------------------------------------------------------------------------------------------------------------------------------------

13
_____________________________________________________________________________________

Program 08. Write java programs to perform read/write operations on file

_____________________________________________________________________________________

import java.io.*;
public class Prog8 {
public static void main(String[] args)
{
try
{
System.out.println("--------------------------------------------------------------");
System.out.println("--------------------------Program 8------------------------");
System.out.println("--------------------------------------------------------------");

FileReader fr=new FileReader("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\MyFile.txt");


BufferedReader br=new BufferedReader(fr);
System.out.println("---------------------Initial File Content---------------------");

int i;
while((i=br.read())!=-1)
{
System.out.print((char)i);
}
br.close();
fr.close();
System.out.println("\n--------------------------------------------------------------");

FileWriter writer = new FileWriter("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\MyFile.txt",true);


BufferedWriter buffer = new BufferedWriter(writer);
buffer.write(" Hi Welcome to the Program 8 and We have just changed the content of the file.");
buffer.close();
System.out.println("Write Operation Done");
System.out.println("--------------------------------------------------------------");
System.out.println("---------------------After Modification---------------------");

System.out.println("After Modification File Content Is");


FileReader fr2=new FileReader("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\MyFile.txt");
BufferedReader br2=new BufferedReader(fr2);
int j;
while((j=br2.read())!=-1)
{
System.out.print((char)j);
}
br2.close();

14
fr2.close();
System.out.println("\n--------------------------------------------------------------");
System.out.println("--------------------------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

15
_____________________________________________________________________________________

Program 09. Test any five of standard exception and user Defined Custom Exceptions in java.

_____________________________________________________________________________________

import java.io.*;

class Prog9
{
void validate(int age)throws InvalidAgeException
{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
void productCheck(int weight) throws InvalidProductException
{
if(weight<100)
{
throw new InvalidProductException("Product Invalid");
}
}
void InputCheck(int Input) throws InvalidInputException
{
if(Input<100)
{
throw new InvalidInputException("Input Invalid");
}
}
void ChoiceCheck(int Choice) throws InvalidChoiceException
{
if(Choice<100)
{
throw new InvalidChoiceException("Choice Invalid");
}
}
void Withdrawan(int Ammount) throws InvalidAmmountException
{
int balance =5000;
if(Ammount>balance)
{
throw new InvalidAmmountException("Withdrawan cannot happen!!");
}
}

16
public static void main(String args[])
{
System.out.println("--------------------------------------------");
System.out.println("------------------Program 9 ----------------");
System.out.println("--------------------------------------------");
System.out.println("*** Built-in Exceptions ***");
//1
try {
int a = 30, b = 0;
int c = a/b;
System.out.println ("Result = " + c);
}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by 0");
}

//2
try {
String a = null;
System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException..");
}

//3
try {
String a = "This is like chipping "; // length is 22
char c = a.charAt(24); // accessing 25th element
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}

//4
try {
File file = new File("E://file.txt");

FileReader fr = new FileReader(file);


} catch (FileNotFoundException e) {
System.out.println("File does not exist");
}

//5

17
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;

System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format exception");
}

//6
try{
int a[] = new int[5];
a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
System.out.println("--------------------------------------------");
System.out.println("--------------------------------------------");
System.out.println("*** User Defined Exceptions ***");

//u1
Prog9 p=new Prog9();
try{
p.validate(13);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}

//u2
try{
p.productCheck(1);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}

//u3
try{
p.InputCheck(1);
}
catch(Exception m){
System.out.println("Exception occured: "+m);

18
}

//u4
try{
p.ChoiceCheck(1);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}

//u5
try{
p.Withdrawan(6000);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}
System.out.println("--------------------------------------------");
System.out.println("--------------------------------------------");
}
}

class InvalidAgeException extends Exception


{
public InvalidAgeException(String s)
{
super(s);
}
}
class InvalidProductException extends Exception
{
public InvalidProductException(String s)
{
super(s);
}
}
class InvalidInputException extends Exception
{
public InvalidInputException(String s)
{
super(s);
}
}
class InvalidChoiceException extends Exception

19
{
public InvalidChoiceException(String s)
{
super(s);
}
}
class InvalidAmmountException extends Exception
{
public InvalidAmmountException(String s)
{
super(s);
}
}

------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

20
_____________________________________________________________________________________
Program 10. Threads creation and design applications by using Extending the Thread class/
implementing the Runnable Interface.
_____________________________________________________________________________________
public class Prog10 implements Runnable {
public void run() {
System.out.println("Thread has ended");
System.out.println("--------------------------------------------");
}
public static void main(String[] args) {
System.out.println("--------------------------------------------");
System.out.println("------------------Program 10 ---------------");
System.out.println("--------------------------------------------");
System.out.println("*** Thread Application Implementing Runable Interface ***");
Prog10 ex = new Prog10();
Thread t1= new Thread(ex);
t1.start();
System.out.println("* Multiplication Table Of 6 *");
for(int i=1;i<11;i++)
{
System.out.println(i*7);
}
System.out.println("--------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

----------------------------------------------------------------------------------------------------------------------------------------

21
_____________________________________________________________________________________

Program 11. Write application for thread synchronization.

_____________________________________________________________________________________

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

System.out.println("--------------------------------------------------------------");
System.out.println("--------------------------Program 11------------------------");
System.out.println("--------------------------------------------------------------");

Table obj = new Table();

MyThread1 t1=new MyThread1(obj);


MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();

}
}
class Table
{
synchronized void printTable(int n)
{
System.out.println("Multiplication Table of "+n);

for(int i=1;i<=10;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread
{

22
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

-----------------------------------------------------------------------------------------------------------------------------------------

23
_____________________________________________________________________________________

Program 12. Write application for Inter thread collection.

_____________________________________________________________________________________

public class ThreadA {


public static void main(String[] args) throws InterruptedException {
ThreadB b = new ThreadB();
b.start();
synchronized (b) {
System.out.println("main thread calling wait() method"); // step 1
b.wait();
System.out.println("main thread got notification call"); // step 4
System.out.println("totol balance " + b.totalBalance);
}
}
}
class ThreadB extends Thread {

int totalBalance = 0;

public void run() {


synchronized (this) {
System.out.println("child Thread starts calculation for total balance"); // step 2
for (int i = 0; i <= 50; i++) {
totalBalance = totalBalance + i;
}
System.out.println("child thread gives notification call"); // step 3
this.notify();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

----------------------------------------------------------------------------------------------------------------------------------------

24
_____________________________________________________________________________________

Program 13. Design java application using Collection in java such as Array List, Link List

_____________________________________________________________________________________

import java.util.*;
public class Prog13
{
public static void main(String[] args)
{
System.out.println("---------------------------------------------");
System.out.println("------------------Program 13-----------------");
System.out.println("---------------------------------------------");
Scanner sc = new Scanner(System.in);
ArrayList<String> arrList = new ArrayList<String>();
LinkedList<String> lList = new LinkedList<String>();
System.out.println("---------------------------------------------");
System.out.println("How many record you want to add : ");
int n = sc.nextInt();
System.out.println("---------------------------------------------");
//System.out.println("Enter "+n+" items");
for(int i=1;i<=n;i++)
{
System.out.println("Enter "+i+"th Item : ");
String item = sc.next();
arrList.add(item);
lList.add(item);
}
System.out.println("---------------------------------------------");
System.out.println("ArrayList Elements :"+arrList+ "\n ArrayList size :"+arrList.size());
System.out.println("---------------------------------------------");
System.out.println("LinkedList Elements :"+lList+ "\n Linked List Size:"+lList.size());
System.out.println("---------------------------------------------");
System.out.println("Enter Item to remove From ArrayList: ");
String removeString=sc.next();
if(arrList.contains(removeString))
{
arrList.remove(removeString);
}
else
{
System.out.println("String not available");
}
System.out.println("---------------------------------------------");
System.out.println("Enter Item to remove From LinkedList: ");

25
String removeS=sc.next();
if(lList.contains(removeS))
{
lList.remove(removeS);
}
else
{
System.out.println("Item not available");
}
System.out.println("---------------------------------------------");
System.out.println("---------------------------------------------");
System.out.println("***After Remove Operation");
System.out.println("---------------------------------------------");
System.out.println("ArrayList Elements :"+arrList);
System.out.println("---------------------------------------------");
System.out.println("LinkedList Elements :"+lList);

}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

-----------------------------------------------------------------------------------------------------------------------------------------

26
_____________________________________________________________________________________

Program 14. Design java application using Collection in java such as Queue.

_____________________________________________________________________________________

import java.util.*;
public class Prog14
{
public static void main(String[] args)
{
System.out.println("---------------------------------------------");
System.out.println("------------------Program 14-----------------");
System.out.println("---------------------------------------------");

PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();


pQueue.add(10);
pQueue.add(20);
pQueue.add(15);

System.out.print("Queue Elements Are : "+pQueue+"\n");

System.out.println("Peek Element Of Queue : "+pQueue.peek());

System.out.println("Poll Element Of Queue : "+pQueue.poll());

System.out.println("Peek Element Of Queue : "+pQueue.peek());

System.out.println("---------------------------------------------");
System.out.println("---------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

27
_____________________________________________________________________________________

Program 15. Design java application using Collection in java such as Map

_____________________________________________________________________________________

import java.util.*;

public class Prog15 {


public static void main(String[] args)
{
System.out.println("--------------------------------------------------------------");
System.out.println("---------------------------Program 15-------------------------");
System.out.println("--------------------------------------------------------------\n");

int checker=0;
Map map=new HashMap();

map.put(1,"Amit");
map.put(2,"Jai");
map.put(3,"Rahul");
map.put(4,"Sai");
map.put(5,"Taman");
map.put(6,"Rishikesh");
map.put(7,"Mahendra");

Set set=map.entrySet();
Iterator itr=set.iterator();
System.out.println("Elements of Map");
while(itr.hasNext())
{
Map.Entry entry=(Map.Entry)itr.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
System.out.println("--------------------------------------------------------------");
Scanner sc=new Scanner(System.in);
System.out.println("Enter name which you want to search in Map : ");
String nm=sc.next();
System.out.println("--------------------------------------------------------------");
Set set1=map.entrySet();
Iterator itr1=set1.iterator();
while(itr1.hasNext())
{
Map.Entry entry1=(Map.Entry)itr1.next();
if(!nm.equals(entry1.getValue()))
{

28
}
else
{
checker=1;
System.out.println("Entered Name is present in map \n Details");
System.out.println(entry1.getKey()+" "+entry1.getValue());
}
}
if(checker==0)
{
System.out.println("Entered Name is not present in map");
}
System.out.println("--------------------------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

29
_____________________________________________________________________________________

Program 16. Design GUI based java application using AWT, Event Handling.

_____________________________________________________________________________________
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
class AEvent extends Frame implements ActionListener{

TextField tf;
Date d = new Date();
AEvent(){

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});

tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("Show Date");
b.setBounds(100,120,80,30);

//register listener
b.addActionListener(this);//passing current instance

//add components and set size, layout and visibility


add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
tf.setText(d.toGMTString());
}
public static void main(String args[]){
new AEvent();
}
}
------------------------------------------------------------------------------------------------------------------------------------------

30
Output

------------------------------------------------------------------------------------------------------------------------------------------

31
_____________________________________________________________________________________

Program 17. Design a JDBC application to retrieve the data from SQL table

_____________________________________________________________________________________

import java.sql.*;
public class Jdbc {
public static void main(String[] args){
System.out.println("My JDBC Program");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");
System.out.println("Connected");
Statement st=con.createStatement();
ResultSet rs= st.executeQuery("select * from login");
while(rs.next()){

System.out.println(rs.getString(1)+"------"+rs.getString(2)+"------"+rs.getString(3));
}
if(rs.next())
{
}
else
{
System.out.println("Rs is empty!");
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------

32
Output

------------------------------------------------------------------------------------------------------------------------------------------

33
_____________________________________________________________________________________

Program 18. Design a JDBC application to execute non select queries

_____________________________________________________________________________________

public class Prog18 {


public static void main(String[] args){
System.out.println("My JDBC Program");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");
System.out.println("-------------------------------------------------");
System.out.println("------------------Program 18---------------------");
System.out.println("-------------------------------------------------");
System.out.println("Connected");
Statement st=con.createStatement();
System.out.println("-------------------------------------------------");
System.out.println("---------------Initial Table Content------------");
ResultSet rs= st.executeQuery("select * from Prog18");
System.out.println("RS");
if(!rs.next()){
System.out.println("Rs is empty!");
}
while(rs.next()){
System.out.println(rs.getInt(1)+"------"+rs.getString(2));
}

st.execute("update Prog18 set name='RAMAN' where id=1");


System.out.println("-------------------------------------------------");
System.out.println("Update Complete");
System.out.println("-------------------------------------------------");
System.out.println("-------------Updated table Content---------------");
ResultSet rs1= st.executeQuery("select * from Prog18");
if(!rs1.next()){
System.out.println("Rs is empty!");
}
while(rs1.next()){
System.out.println(rs1.getInt(1)+"------"+rs1.getString(2));
}
System.out.println("-------------------------------------------------");
con.close();
}
catch(Exception e)
{

34
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------

Output

------------------------------------------------------------------------------------------------------------------------------------------

35
_____________________________________________________________________________________

Program 19. Design and implement servlet applications.

_____________________________________________________________________________________

package test;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
@SuppressWarnings("serial")
public class InsertServlet extends GenericServlet
{
public Connection con;
@Override
public void init() throws ServletException
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ADITYA12","system","ma
nager");
}
catch(Exception e) {e.printStackTrace();}
}
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String eId=req.getParameter("eid");
String eName=req.getParameter("ename");
long ephno=Long.parseLong(req.getParameter("ephno"));

try
{
PreparedStatement ps=con.prepareStatement("insert into studentdata values(?,?,?)");
ps.setString(1,eId);
ps.setString(2,eName);
ps.setLong(3,ephno);

int k=ps.executeUpdate();
if(k>0)
{
pw.println("Employee Data Inserted Succesfully");

36
}
}
catch(Exception e) {e.printStackTrace();}
}
@Override
public void destroy()
{
try {con.close();}
catch(Exception e) {e.printStackTrace();}
}
}

Input.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="insert" method="post">
Emp Id: <input type="number" name="eid"><br>
Emp Name: <input type="text" name="ename"><br>
Emp Phno: <input type="tel" name="ephno"><br>
<input type="submit" value="Insert">
</form>
</body>
</html>

------------------------------------------------------------------------------------------------------------------------------------------

37
Output

------------------------------------------------------------------------------------------------------------------------------------------

38
_____________________________________________________________________________________

Program 20. Design and implement JSP applications

_____________________________________________________________________________________

Display.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Program2</title>
</head>
<body>
<% String name=request.getParameter("name");
long phno=Long.parseLong(request.getParameter("phno"));
String mid=request.getParameter("mid");
out.println("Name :"+name);
out.println("Phone no :"+phno);
out.println("Email ID :"+mid);
%>
</body>
</html>
Input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Display.jsp" method="post">
Name:
<input type="text" name="name"><br>
Phone no:
<input type="text" name="phno"><br>
Email ID:
<input type="text" name="mid"><br>
<input type="submit" name="Display">
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------

39
Output

------------------------------------------------------------------------------------------------------------------------------------------

40

You might also like