SQL and JAVA Practical
SQL and JAVA Practical
4. Change the datatype of column Dname from VARCHAR(80) to VARCHAR(100) and the
column should not contain any NULL value in the table Department.
Ans: ALTER TABLE Department MODIFY Dname VARCHAR(100) NOT NULL;
6. Insert the following values in the given rows in the table Department:-
Row 1: 101, ‘English’
Row 2: 102, ‘Accounts’
Row 3: 103, ‘History’
Row 4: 104, ‘Information Technology’
10. Make the default address ‘Malbazar’ for all employees in Employee table.
Ans: ALTER TABLE Employee ALTER Empaddr SET DEFAULT ‘Bus-Stand, Malbazar’;
11. Make Empdid of Employee Foreign Key referencing values of Did of table Department
where Empdid should show changed values if values on Did is changed.
Ans: ALTER TABLE Employee ADD FOREIGN KEY(Empdept) REFERENCES Department(Did) ON
UPDATE CASCADE;
Ans: INSERT INTO Employee VALUES(1011, ‘Akshay Jha’, ‘NJP main road, ward-32, Siliguri’,
‘9832512546’, 101, 35000);
INSERT INTO Employee VALUES(1012, ‘Akshay Oraon’, ‘Bidhan road, ward-15,
Jalpaiguri’, ‘9832016622’, 101, 20000);
INSERT INTO Employee VALUES(1013, ‘Dipak Patel, ‘Anandapally, ward-02, Malbazar’,
‘7270123431’, 104, 35000);
INSERT INTO Employee VALUES(1014, ‘Kajal Khatun’, ‘Caltex More, ward-10, Malbazar’,
‘6290876543’, 103, 15000);
INSERT INTO Employee(Empid, Empname, Empcont, Edpt, Esal) VALUES(1015,
‘Bishakha Saha’, ‘9563212345’, 104, 15000);
INSERT INTO Employee(Empid, Empname, Empaddr, Edpt, Esal) VALUES(1016, ‘Ananya
Sarkar’, ‘Anandapally, ward-02, Malbazar’, 102, 35000);
INSERT INTO Employee VALUES(1017, ‘Kamal Sah’, ‘Dabgram, ward-22, Siliguri’,
‘9677854309’, 102, 20000);
INSERT INTO Employee VALUES(1018, ‘Zubair Hussain’, ‘Ukilpara, ward-25, Jalpaiguri’,
‘7004567890, 103, 35000);
18. Show Names and Salary of employees whose Salary is 20000 or more.
Ans: SELECT Empname, Esal FROM Employee WHERE Esal>=20000;
Empname Esal
Akshay Jha 35000
Akshay Oraon 20000
Dipak Patel 35000
Ananya Sarkar 35000
Kamal Sah 20000
Zuabir Hussain 35000
19. Enter Contact as ‘7001525009’ for Ananya Sarkar.
Ans: UPDATE Employee SET Empcont=’ ‘7001525009’ WHERE Empname=’Ananya Sarkar’;
20. Show Id, Name and Bonus of employees which is 10% of their Salary.
Ans: SELECT Empid, Empname, Esal*0.1 AS Bonus FROM Employee;
Empid Empname Bonus
1011 Akshay Jha 3500
1012 Akshay Oraon 2000
1013 Dipak Patel 3500
1014 Kajal Khatun 1500
1015 Bishakha Saha 1500
1016 Ananya Sarkar 3500
1017 Kamal Sah 2000
1018 Zuabir Hussain 3500
4. Write a Java Program to insert a numbers and print the last digit.
Ans:
import java.util.*;
class LastDigit
{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number”);
a= sc.nextInt();
b= a%10;
System.out.println(“Last digit = ”+b);
}
}
5. Write a Java Program to insert 2 numbers and find the bigger number
Ans:
import java.util.*;
class Big
{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number”);
a= sc.nextInt();
b= sc.nextInt();
if(a>b)
{
System.out.println(“Bigger number= “+a);
}
else
{
System.out.println(“Bigger number= “+b);
}
}
}
6. Write a Java Program to insert a number and check if it is a positive or negative number or
not.
Ans:
import java.util.*;
class PosNeg
{
public static void main(String[] args)
{
int a;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number”);
a= sc.nextInt();
if(a>0)
{
System.out.println(“positive number”);
}
else
{
System.out.println(“negative number“);
}
}
}
7. Write a Java Program to insert a number and check if it is an even number or odd.
Ans:
import java.util.*;
class OddEven
{
public static void main(String[] args)
{
int a;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number”);
a= sc.nextInt();
if(a%2==0)
{
System.out.println(“Even number“);
}
else
{
System.out.println(“Odd number”);
}
}
}
8. Write a Java Program to insert 3 random numbers and find the largest number.
Ans:
import java.util.*;
class Largest
{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter numbers”);
a= sc.nextInt();
b= sc.nextInt();
if(a>b&&a>c)
{
System.out.println(“largest number= “+a);
}
else if(b>a&&b>c)
{
System.out.println(“largest number= “+b);
}
else
{
System.out.println(“largest number= “+c);
}
}
}
9. Write a Java Program to print “Hello” 5 times using while loop.
Ans:
import java.util.*;
class Hello
{
public static void main(String[] args)
{
int i=1;
while(i<=5)
{
System.out.println(“Hello”);
i++;
}
}
}
10. Write a Java Program to print the 1st 100 Natural numbers.
Ans:
import java.util.*;
class Natural
{
public static void main(String[] args)
{
int i=1;
while(i<=100)
{
System.out.println(i);
i++;
}
}
}
11. Write a Java Profram to Print the sum and average of the 1st 100 Natural numbers.
Ans:
import java.util.*;
class SumAvg
{
public static void main(String[] args)
{
int i=1, s=0, avg;
while(i<=5)
{
s=s+i;
i++;
}
i- - ;
avg=sum/i;
System.out.println(“Sum of 1st 100 Nutural nos. =”+s);
System.out.println(“Average of 1st 100 Nutural nos. =”+avg);
}
}
12. Write a Java Program to insert 5 numbers and Print their sum.
Ans:
import java.util.*;
class Sum
{
public static void main(String[] args)
{
int i=1, n, s=0;
Scanner sc= newScanner(System.in);
while(i<=5)
{
System.out.println(“Enter number”);
n= sc.nextInt();
s=s+n;
i++;
}
System.out.println(“Sum of 5 nos. =”+s);
}
}
13. Write a Java Program to find the even and odd numbers between 1 and 100.
Ans:
import java.util.*;
class EvenOdd2
{
public static void main(String[] args)
{
for(i=1;i<=100;i++)
{
if(i%2==0)
{
System.out.println(“Even no “+i);
}
else
{
System.out.println(“odd no “+i);
}
}
}
}
14. Write a Java program to check if a number is prime or not.
Ans:
import java.util.*;
class Prime
{
public static void main(String[] args)
{
int i, n, c=0;
Scanner sc= new Scanner(System.in);
System.out.println(“Enter number ”);
n= sc.nextInt();
for(i=2;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==0)
{
System.out.println(n+" is a prime number");
}
else
{
System.out.println(n+" is not a prime number");
}
}
}
15. Write a Java Program to Sort the elements of an array using Bubble Sort.
Ans:
import java.util.*;
class Test2
{
public static void main(String[] args)
{
int i, j, temp, n;
Scanner sc= new Scanner(System.in);
System.out.println("Enter array limit");
n= sc.nextInt();
int a[]= new int[n];
System.out.println("Enter array elements");
for(i=0; i<n; i++)
{
a[i]=sc.nextInt();
}
for (i=0; i< n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("After sorting elements are");
for(i=0; i<n; i++)
{
System.out.println(a[i]);
}
}
}
16. Write a Java Program to print the id, roll number, name, class of 3 students using
Parameterized constructor.
Ans:
import java.util.*;
class Student
{
int id, roll;
String name;
String cls;
Student(int i, int r, String n, String c)
{
id= i;
roll= r
name= n;
cls= c;
}
void display()
{
System.out.println(id+” ”+roll+” ”+name+” ”+cls);
}
public static void main(String[] args)
{
Student s1 = new Student(101, 32, "John", “XII-HUM”);
Student s2 = new Student(102, 42, "Jason", “XII-HUM”);
Student s3 = new Student(103, 22, "June", “XII-COM”);
s1.display();
s2.display();
s3.display();
}
}
17. Write a Java Program to show Multilevel Inheritence.
Ans:
import java.util.*;
class GrandFather
{
public void house()
{
System.out.println("3 BHK House.");
}
}
class Father extends GrandFather
{
public void land()
{
System.out.println("5 Arcs of Land..");
}
}
class Son extends Father
{
public void car()
{
System.out.println("Own Audi Car..");
}
}
public class multilevel
{
public static void main(String args[])
{
Son ob = new Son();
ob.car();
ob.house();
ob.land();
}
}