It - II B.tech Sem - II Java Lab Manual (20-21)
It - II B.tech Sem - II Java Lab Manual (20-21)
B.TECH
(II YEAR – II SEM)
(2020-21)
VISION
➢ To achieve high quality in technical education that provides the skills and
MISSION
➢ To equip the students with the cognizance for problem solving and to
research experience.
➢ To promote the necessary moral and ethical values among the engineers, for
After the completion of the course, B. Tech Information Technology, the graduates will have the
following Program Specific Outcomes:
1. Fundamentals and critical knowledge of the Computer System:- Able to Understand the
working principles of the computer System and its components , Apply the knowledge to
build, asses, and analyze the software and hardware aspects of it .
1. Students are advised to come to the laboratory at least 5 minutes before (to the starting time),
those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to the lab with the
synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.
b. Laboratory Record updated up to the last session experiments and other utensils (if any) needed
in the lab.
c. Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation note
book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain the
discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which should
be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out; if anybody
found loitering outside the lab / class without permission during working hours will be treated
seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the lab
after completing the task (experiment) in all aspects. He/she must ensure the system / seat is
kept properly.
WEEK-1: DATE:
A) Write a java program to find the Fibonacci series using recursive and non recursive functions
/* Recursive Solution*/
import java.io.*;
import java.lang.*;
class Demo {
int fib(int n) {
if(n==1)
return (1);
else if(n==2)
return (1);
else
return (fib(n-1)+fib(n-2));
}
}
class RecFibDemo {
public static void main(String args[])throws IOException {
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("enter last number");
int n=Integer.parseInt(br.readLine());
1|Page
JAVA PROGRAMMING LAB 2020-2021
2|Page
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
3|Page
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
4|Page
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
5|Page
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
6|Page
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
7|Page
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
8|Page
JAVA PROGRAMMING LAB 2020-2021
import java.util.Scanner;
class Matrixmul
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, i, j, k;
if (n != p)
System.out.println("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
multiply[i][j] = sum;
sum = 0;
9|Page
}
}
System.out.println("Product of the matrices:");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++)
System.out.print(multiply[i][j]+"\t");
System.out.print("\n");
}
}
}
}
10 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
11 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
12 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
13 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
14 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
15 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
16 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 2:
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
class StudentData
{
private int stuID;
private String stuName;
private int stuAge;
StudentData()
{
//Default constructor
stuID = 100;
stuName = "New Student";
stuAge = 18;
17 | P a g e
}
JAVA PROGRAMMING LAB 2020-2021
/*This object creation would call the parameterized constructor StudentData(int, String, int)*/
18 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
19 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
20 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
21 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
22 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
23 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
24 | P a g e
JAVA PROGRAMMING LAB 2020-2021
B) Write a java program to display the employee details using Scanner class.
import java.util.Scanner;
class Employee
{
int Id;
String Name;
int Age;
long Salary;
for(i=0;i<3;i++)
Emp[i] = new Employee(); // Allocating memory to each object
for(i=0;i<3;i++)
{
System.out.print("\nEnter details of "+ (i+1) +" Employee\n");
Emp[i].GetData();
}
System.out.print("\nDetails of Employees\n");
for(i=0;i<3;i++)
25 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Emp[i].PutData();
}
}
26 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
27 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
28 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
29 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
30 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
31 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
32 | P a g e
JAVA PROGRAMMING LAB 2020-2021
C) Write a java program that checks whether a given string is palindrome or not.
import java.util.Scanner;
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
33 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
34 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
35 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
36 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
37 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
38 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
39 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
40 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 3:
class Triangle
{
void numberOfSides()
{
System.out.println("The no. of side's in triangle are:3 ");
}
}
class Hexagon
{
void numberOfSides()
{ System.out.println("The no. of side's in hexagon are:6 ");
}
}
// Class that create objects and call the method.
class ShapeDemo
{
public static void main(String args[])
{
Trapezoid obj1 = new Trapezoid();
Triangle obj2 = new Triangle();
Hexagon obj3 = new Hexagon();
obj1.numberOfSides();
obj2.numberOfSides();
obj3.numberOfSides(); }
}
41 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
42 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
43 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
44 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
45 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
46 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
47 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
48 | P a g e
JAVA PROGRAMMING LAB 2020-2021
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1 {
public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the interface Inf2, it has to implement all
the methods of Inf1 as well because the interface Inf2 extends Inf1 */
public void method1(){
System.out.println("method1");
}
public void method2(){
System.out.println("method2");
}
public static void main(String args[]){
Inf2 obj = new Demo();
obj.method2();
}
}
49 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
50 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
51 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
52 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
53 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
54 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
55 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
56 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 4:
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1 {
public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the interface Inf2, it has to implement all
the methods of Inf1 as well because the interface Inf2 extends Inf1 */
public void method1(){
System.out.println("method1");
}
public void method2(){
System.out.println("method2");
}
public static void main(String args[]){
Inf2 obj = new Demo();
obj.method2();
}
}
57 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
58 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
59 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
60 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
61 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
62 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
63 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
64 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 5:
class Outer_Demo {
int num;
// inner class
private class Inner_Demo {
public void print() {
System.out.println("This is an inner class");
}
}
outer.display_Inner();
}
}
65 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
66 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
67 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
68 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
69 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
70 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
71 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
72 | P a g e
JAVA PROGRAMMING LAB 2020-2021
class MultipleExceptionHandling{
public static void main(String args[]){
try{
System.out.println("Begin: try block");
String name="abc";
int nameLength = name.length();
int res= 10/0;
73 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
74 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
75 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
76 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
77 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
78 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
79 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
80 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 6:
A) Write a java program for producer and consumer problem using Threads.
import java.util.LinkedList;
t1.start();
t2.start();
81 | P a g e
JAVA PROGRAMMING LAB 2020-2021
// t1 finishes before t2
t1.join();
t2.join();
}
// This class has a list, producer (adds items to list and consumer (removes items).
public static class PC {
82 | P a g e
JAVA PROGRAMMING LAB 2020-2021
{
// consumer thread waits while list
// is empty
while (list.size() == 0)
wait();
// and sleep
Thread.sleep(1000);
}
}
}
}
}
83 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
84 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
85 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
86 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
87 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
88 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
89 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
90 | P a g e
JAVA PROGRAMMING LAB 2020-2021
B) Write a Java program that implements a multi-thread application that has three threads.
/* The first thread displays "Good Morning" for every one second, the second thread displays "Hello"
for every two seconds and third thread displays "Welcome" for every three seconds */
class MultithreadDemo {
public static void main(String args[]) {
GoodMorning t1 = new GoodMorning();
Hello t2 = new Hello();
91 | P a g e
JAVA PROGRAMMING LAB 2020-2021
t1.start();
t2.start();
t3.start();
}
}
92 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
93 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
94 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
95 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
96 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
97 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
98 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 7:
/* Accept a file or directory name from command line arguments. Then the program will check if
that file or directory physically exist or not and it displays the property of that file or directory.*/
*import java.io.File;
class fileProperty
if(f.exists())
{
System.out.println("Is writeable:"+f.canWrite());
System.out.println("Is readable"+f.canRead());
System.out.println("Is a directory:"+f.isDirectory());
System.out.println("File Size in bytes "+f.length());
}
99 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
100 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
101 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
102 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
103 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
104 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
105 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
106 | P a g e
JAVA PROGRAMMING LAB 2020-2021
import java.io.*;
import java.util.*;
class arrayli
{
public static void main(String[] args) throws IOException
{
// size of ArrayList
int n = 5;
// Printing elements
System.out.println(arrli);
arrli.remove(3);
System.out.println(arrli);
107 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
108 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
109 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
110 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
111 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
112 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
113 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
114 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 8:
Write a Java program loads phone no, name from a text file using hash table.
/*Create a text file hashtab.txt with name followed by a tab and number
akhil 123
srujan 345
edrf 567
Then create a java file Phonebook.java */
import java.io.*;
import java.util.*;
public class Phonebook
{
public static void main(String args[])
{
try
{
FileInputStream fis=new FileInputStream("hashtab.txt");
Scanner sc=new Scanner(fis).useDelimiter("\t");
Hashtable<String,String> ht=new Hashtable<String,String> ();
String[] strarray;
String a,str;
while(sc.hasNext())
{
a=sc.nextLine();
strarray=a.split("\t");
ht.put(strarray[0],strarray[1]);
System.out.println("hash table values are"+strarray[0]+":"+strarray[1]);
}
Scanner s=new Scanner(System.in);
System.out.println("enter the name as given in the phone book");
str=s.next();
if(ht.containsKey(str))
{
System.out.println("phone no is"+ht.get(str));
}
else
{
System.out.println("name is not matched");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
115 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
116 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
117 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
118 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
119 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
120 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
121 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
122 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 9:
Applet1.java:
// Import the packages to access the classes and methods in awt and applet classes.
import java.awt.*;
import java.applet.*;
{
g.drawString("HELLO WORLD",20,20);
}
Applet1.html:
/* <applet code="Applet1" width=200 height=300> </applet>*/
123 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
124 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
125 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
126 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
127 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
128 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
129 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
130 | P a g e
JAVA PROGRAMMING LAB 2020-2021
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
else
{
131 | P a g e
JAVA PROGRAMMING LAB 2020-2021
for(int i=1;i<=n;i++)
fact=fact*i;
}
t2.setText(String.valueOf(fact));
}
else if(ae.getSource()==b2)
{
t1.setText("");
t2.setText("");
}
}
}
132 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
133 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
134 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
135 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
136 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
137 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
138 | P a g e
JAVA PROGRAMMING LAB 2020-2021
import java.awt.*;
import java.applet.*;
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
*/
139 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
140 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
141 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
142 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
143 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
144 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
145 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
146 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 10:
Write a java program for handling Mouse events and Key events
147 | P a g e
JAVA PROGRAMMING LAB 2020-2021
mousey=me.getY();
msg="down";
repaint();
}
148 | P a g e
JAVA PROGRAMMING LAB 2020-2021
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
149 | P a g e
JAVA PROGRAMMING LAB 2020-2021
150 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
151 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
152 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
153 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
154 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
155 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
156 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Week 11:
A) Write a java program that connects to a database using JDBC program:
import java.sql.Connection;
import java.sql.DriverManager;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb",
"postgres", "123");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
157 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
158 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
159 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
160 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
161 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
162 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
163 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
164 | P a g e
JAVA PROGRAMMING LAB 2020-2021
B) Write a java program to connect to database using JDBC & insert values into
table
import java.sql.*;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
165 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
166 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
167 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
168 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
169 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
170 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
171 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
172 | P a g e
JAVA
JAVA PROGRAMMING
PROGRAMMING LAB
LAB 2020-2021
2020-2021
C) Write a java program to connect to a database using JDBC and delete values from
table.
import java.sql.*;
public class delete
{
public static void main(String args[])
{
String id = "id2";
String pwd = "pwd2";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();
int x = stmt.executeUpdate(q1);
if (x > 0)
System.out.println("One User Successfully Deleted");
else
System.out.println("ERROR OCCURED :(");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
173 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
174 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
175 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
176 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
177 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
178 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
179 | P a g e
JAVA PROGRAMMING LAB 2020-2021
Record Notes:
180 | P a g e
JAVA PROGRAMMING LAB 2020-2021
12.Write a java program that works as a simple calculator. Use a Grid Layout to arrange Buttons for
digits and for the + - * % operations. Add a text field to display the result.
Program:
import javax.swing.*;
import javax.swing.JOptionPane; import java.awt.*;
import java.awt.event.*;
181 | P a g e
JAVA PROGRAMMING LAB 2018-2019
182 | P a g e
equal=new JButton("=");
JAVA PROGRAMMING LAB 2018-2019
panel.add(equal);
equal.addActionListener(this); plus=new JButton("+"); panel.add(plus);
plus.addActionListener(this); add(panel);
}
// Implementing method in ActionListener.
public void actionPerformed(ActionEvent
ae)
{
if(ae.getSource()==n1)
assign("1");
else if(ae.getSource()==n2)
assign("2");
else if(ae.getSource()==n3)
assign("3");
else if(ae.getSource()==n4)
assign("4");
else if(ae.getSource()==n5)
assign("5");
else if(ae.getSource()==n6)
assign("6");
else if(ae.getSource()==n7)
assign("7");
else if(ae.getSource()==n8)
assign("8");
else if(ae.getSource()==n9)
assign("9");
else if(ae.getSource()==n0)
assign("0");
else if(ae.getSource()==dot)
{
if(((result.getText()).indexOf("."))==-1)
result.setText(result.getText()+"."); } else if(ae.getSource()==minus)
{
preRes=Double.parseDouble(result.getText()); lastCommand="-";
result.setText("0");
}
else if(ae.getSource()==div)
{
preRes=Double.parseDouble(result.getText());
lastCommand="/";
result.setText("0");
}
183 | P a g e
JAVA PROGRAMMING LAB 2018-2019
else if(ae.getSource()==equal)
{
secVal=Double.parseDouble(result.getText());
if(lastCommand.equals("/"))
res=preRes/secVal;
else if(lastCommand.equals("*"))
res=preRes*secVal;
else if(lastCommand.equals("-
")) res=preRes-secVal;
else if(lastCommand.equals("+"))
res=preRes+secVal;
result.setText(" "+res); lastCommand="=";
}
else if(ae.getSource()==mul)
{
preRes=Double.parseDouble(result.getText());
lastCommand="*";
result.setText("0");
}
else if(ae.getSource()==plus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="+";
result.setText("0");
}
}
}
Calculator.html:
<applet code="Calculator" width=200 height=300> </applet>
P 103
a g |eP |a106
g
e
JAVA PROGRAMMING LAB 2018-2019
Record Notes:
P 103
a g |eP |a107
g
e
JAVA PROGRAMMING LAB 2018-2019
Record Notes
P 103
a g |eP |a108
g
e
JAVA PROGRAMMING LAB 2018-2019
Record Notes:
P 103
a g |eP |a109
g
e
JAVA PROGRAMMING LAB 2018-2019
Record Notes:
P 103
a g |eP |a110
g
e
JAVA PROGRAMMING LAB 2018-2019
Record Notes:
P 103
a g |eP |a111
g
e
JAVA PROGRAMMING LAB 2018-2019
Record Notes:
P 103
a g |eP |a112
g
e