Java 001
Java 001
EXPERIMENT:-8
1. Title:- Write the query to create the views (1 and multiple tables), replace views and
delete views on college database.
4.2Solution:
Views in sql:-
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present in the
database.
o A view can either have specific rows based on certain condition or all the rows of
a table.
Creating a view:-
o We can create view using CREATE VIEW statement. A view can be created from
a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
4.3 Assumptions:- Not Applicable.
4.4 Dependencies:- Not Applicable.
4.5Code/ Pseudo Code:-
• Query to use college database:-
Mysql> use college;
68 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
69 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
70 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
4.6Results
4.6.1 Test Case
Input:
Output:
Input:
Output:
5 Lab Assignment: In this task you have to implement view on college database.
6 Quiz & Viva Questions
6.1Quiz:
• What is a view in a database management system (DBMS)?
a) A physical table that stores data
b) A virtual table derived from one or more tables or views
c) A query used to retrieve data from the database
71 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
6.2 Viva
a. What is a view in a DBMS?
b. How is a view different from a physical table in a database?
c. What are the advantages of using views in a DBMS?
d. How are views used to enforce data security in a database?
e. What is the role of views in achieving data abstraction and independence?
72 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
EXPERIMENT:-6
1. Title:- Write the query for implementing the following aggregate functions: MAX (), MIN
(), AVG (), COUNT (), SUM () on bank database.
AVG Average
4.2Solution:
• SQL Aggregate Functions:-
o SQL aggregate function is used to perform the calculations on multiple rows
of a single column of a table. It returns a single value.
o It is also used to summarize the data.
SQL Aggregation
Function
MIN
COUNT
SUM AVG MAX
53 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
▪ COUNT Function:-
-COUNT Function is used to Count thee number of rows in a database table. It
can work on both numeric and non-numeric data types.
-COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and null.
▪ SYNTAX:-
COUNT(*)
Or COUNT([ALL|DISTINCT] expression)
▪ Example:-
SELECT COUNT(*) FROM PRODUCT;
▪ SUM Function:-
-SUM function is used to calculate the sum of all selected columns. It works on
numeric fields only.
▪ SYNTAX:-
SUM()
Or
SUM([ALL|DISTINCT EXPRESSION)
▪ Example:-
SELECT SUM(COST) FROM PRODUCT;
▪ AVG Function:-
-the AVG function is used to calculate the average value of the numeric type.
AVG function returns the average of all non-null values.
▪ SYNTAX:-
AVG()
Or
AVG([ALL|DISTINCT] EXPRESSION)
▪ Example:-
SELECT AVG(COST) FROM PRODUCT;
▪ MAX Function:-
-MAX function is used to find the maximum value of a certain
column. This function determines the largest value of all selected
values of a column.
SYNTAX:-
MAX()
Or
MAX([ALL|DISTINCT] expression)
▪ Example:-
54 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
55 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
56 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
4.5Results
4.5.1 Test Case
Input:
Output:
Input:
Output:
5. Lab Assignment:- In this task you have to know how to implement aggregate functions.
6.1Quiz:
• In SQL the function- avg, min, max, sum, count are called as_____.
(a) Aggregate function
(b) Adjunct function
(c) Set operations
(d) Scalar operation
• Which of the following functions returrn the count of all the rows in a specified table?
(a) COUNT(*)
(b) COUNT(UNIQUE expression)
(c) COUNT(DISTINCT expression)
(d) COUNT(+)
6.2 Viva
• Define aggregate functions?
• Define atomicity and aggregation?
• Explain different types of aggregate functions?
• Explain COUNT() function
58 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
EXPERIMENT:-7
1. Title:- Write the query to implement the concept of different Integrity constraints.
2. Outcome:- Must be able to implement integrity constraints.
3. Objectives:- Understand the concept of different types of integrity constraints.
4. Nomenclature, theory with self-assessment questionnaire:-
4.1 Nomenclature:
Not Null Null values
PK Primary key
4.2Solution:
• Integrity constraints:-
➢ Integrity constraints are a set of rules. It is used to maintain the quality of
information.
➢ Integrity constraints ensure that the data insertion, updating and other processes
have to be performed in such a way that data integrity is not affected.
➢ Thus, integrity constraint is used to guard against accidental demage to the
database.
59 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
o Key Constraint:- Keys are the set of entities that are used to identify an entity
within its entity set uniquely. There could be multiple keys in a single entity set,
but out of these multiple keys, only one key will be the primary key. A primary
key can only contain unique and not null values in the relational database table.
60 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
61 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
63 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
Key Constraints:-
64 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
65 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
4.6Results
4.6.1 Test Case
Input: 1
Output:
Input: 2
Output:
5. Lab Assignment: In this task you have to implement different types of integrity constraints.
6. Quiz & Viva Questions
6.1Quiz:
• Information ____ is maintained by integrity constraints.
(a) Quantity
(b) Data
(c) Quality
(d) Flow
• A ____ constraint ensures that insertions, updates and other processes are performed
in a manner that does not compromise____.
(a) Data composition
(b) Data interval
(c) Data integrity
(d) Data insertion
(a) 3
(b) 4
(c) 5
(d) 6
6.2 Viva
• What is constraints?
• What is integrity constraints?
• How many types of integrity constraints?
• Define domain integrity constraints?
67 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
EXPERIMENT:-5
1. Title:- Write the queries for Retrieving Data from a Database Using the WHERE clause,
Using Logical Operators in the WHERE clause, Using IN, BETWEEN, LIKE, ORDER BY,
GROUP BY and HAVING Clause, and Combining Tables Using JOINS.
4.2Solution:
• Sql IN Operator:-
o IN is an operator in SQL, which is generally used with a WHERE clause.
o Using the IN operator, multiple values can be specified.
o It allows us to easily test if an expression matches any value in a list of values.
o IN operator is used to replace many OR conditions.
▪ SYNTAX:-
-SELECT columnName FROM tableNmae WHERE columnName IN (Value 1,
Value 2, -…, Value N);
▪ Example:-
-Mysql> SELECT * FROM STUDENT WHERE Hometown IN (“JAIPUR”,
“SIKAR”, “AJMER”);
• Sql BETWEEN Operator:-
o The BETWEEN keyword is an operator in Structured Query Language. It
allows the database users to access the values within the specified range.
o We can easily use BETWEEN operator in the WHERE clause of INSERT,
UPDATE, SELECT and DELETE SQL queries.
▪ SYNTAX:-
-SELECT column_name(s) FROM table_name WHERE column_name
BETWEEN value1 AND value2;
▪ Example:-
43 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
-SELECT Name FROM emp where Salary BETWEEN 30000 AND 45000;
45 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
46 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
LIKE:-
47 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
NOT LIKE:-
Mysql> select E_Name from employee where E_Add NOT LIKE “In%”;
48 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
49 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
4.6 Results:- We have been successfully implement the all the queries.
4.6.1 Test Case
Input: 4
Output:
Input: 7
Output:
5. Lab Assignment: In this task you have to implement the where clause and logical
operators.
(c) Unused
(d) None
6.2 Viva
• What are joins in SQL?
• What are the different types of joins in SQL?
• State the difference between inner join and left join?
• Explain equi join with example?
52 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
EXPERIMENT:-4
1. Title:- Apply the constraints Primary Key, Foreign key, NOT NULL and Unique key to
the tables of bank /college database.
2. Outcome:- Must be able to apply primary key and foreign key concept on the tables.
3. Objectives:- Understand the relationship between primary key and foreign key.
FK Foreign Key
4.2 Solution:
• NOT NULL:-
A NOT NULL constraints in SQL is used to prevent inserting NULL values into the
specified column, considering it as a not accepted value for that column.
• FOREIGN KEY:-
The FOREIGN KEY constraints is used to prevent actions that would destroy links
between tables.
A foreign key is a field(or collection of fields) in one table, that refers to the primary
key in another table.
The table with the foreign key is called the child table and the table with the primary
key is called the referenced or parent table.
37 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
38 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
We have table 1 that is STUDENT2 having S_ID as the primary key, now we
will make this key as Foreign key in tanle 2 FEES.
STUDENT2 Table:-
4.6 Results:- Successfully create primary key and foreign key in sql.
4.6.1 Test Case
40 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
Input:
Output:
Input:
Output:
5. Lab Assignment:- In this task you have to know how to implement primary key and
foreign key constraints on the tables.
• Which one of the following is a set of one or more attributes taken collectively to
uniquely identify a record?
(a) Candidate key
(b) Super key
(c) Foreign key
(d) None
• Consider attributes ID, CITY and NAME. which one of this can be considered as a
super key?
(a) NAME
(b) ID
(c) CITY
(d) CITY,ID
41 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
• A ______ is a property of the entire relation, rather than of the individual tuples in
which each tuple is unique?
(a) Rows
(b) Key
(c) Attribute
(d) Fields
• A attribute in a relation is a foreign key if the _______ key from one relation is used
as an attribute in that relation?
(a) Candidate
(b) Primary
(c) Super
(d) Sub
• Which one of the following cannot be taken as a primary key?
(a) Id
(b) Register number
(c) Dept_id
(d) Street
6.2 Viva
• What is primary key?
• What is a candidate key?
• What is a foreign key?
• What is a super key?
42 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
EXPERIMENT:-4
1. Title:- Apply the constraints Primary Key, Foreign key, NOT NULL and Unique key to
the tables of bank /college database.
2. Outcome:- Must be able to apply primary key and foreign key concept on the tables.
3. Objectives:- Understand the relationship between primary key and foreign key.
FK Foreign Key
4.2 Solution:
• NOT NULL:-
A NOT NULL constraints in SQL is used to prevent inserting NULL values into the
specified column, considering it as a not accepted value for that column.
• FOREIGN KEY:-
The FOREIGN KEY constraints is used to prevent actions that would destroy links
between tables.
A foreign key is a field(or collection of fields) in one table, that refers to the primary
key in another table.
The table with the foreign key is called the child table and the table with the primary
key is called the referenced or parent table.
37 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
38 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
We have table 1 that is STUDENT2 having S_ID as the primary key, now we
will make this key as Foreign key in tanle 2 FEES.
STUDENT2 Table:-
4.6 Results:- Successfully create primary key and foreign key in sql.
4.6.1 Test Case
40 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
Input:
Output:
Input:
Output:
5. Lab Assignment:- In this task you have to know how to implement primary key and
foreign key constraints on the tables.
• Which one of the following is a set of one or more attributes taken collectively to
uniquely identify a record?
(a) Candidate key
(b) Super key
(c) Foreign key
(d) None
• Consider attributes ID, CITY and NAME. which one of this can be considered as a
super key?
(a) NAME
(b) ID
(c) CITY
(d) CITY,ID
41 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
• A ______ is a property of the entire relation, rather than of the individual tuples in
which each tuple is unique?
(a) Rows
(b) Key
(c) Attribute
(d) Fields
• A attribute in a relation is a foreign key if the _______ key from one relation is used
as an attribute in that relation?
(a) Candidate
(b) Primary
(c) Super
(d) Sub
• Which one of the following cannot be taken as a primary key?
(a) Id
(b) Register number
(c) Dept_id
(d) Street
6.2 Viva
• What is primary key?
• What is a candidate key?
• What is a foreign key?
• What is a super key?
42 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
EXPERIMENT:-2
1. Title:- Design a Database and create required tables. For Bank Database like CUSTOMER,
LOAN, ACCOUNT, BRANCH & TRANSACTION.
3. Objectives:- Understand the relationship between Bank activities and its variable.
4.2Solution:
➢ A database is a collection of organized data that can be easily retrieved, managed,
and updated. The data can be anything that conveys some information.
➢ Tables are created inside a database. You need to first select a database to create a
table in it.
To use a database,
USE<database_name>;
The column parameters specify the names of the columns of the table. The
datatype parameter sets the type of data the column can hold (e.g., varchar,
integer, etc.).
14 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
15 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
16 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
17 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
18 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
19 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
20 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
21 | P a g e
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology
4.6 Results:- The database Bank and table related to it have been created.
4.6.1 Test Case
Input:
Output:
Input:
Output:
5. Lab Assignment: In this task you have to implement Bank Database in MySQL and
create related tables using SQL queries.
(a) Network
(b) Tree
(c) Chain
(d) All of these
• A database management system(DBMS) is a?
(a) Hardware system used to create, maintain and provide controlled access to a
database
(b) Hardware system used to create, maintain and provide uncontrolled access to a
database.
(c) Software system used to create, maintain and provide uncontrolled access to a
database.
(d) Software system used to create, maintain and provide controlled access to a
database.
• For performing tasks like creating the structure of the relations, deleting relation,
which of the following is used?
(a) Data definition language
(b) Data derivation language
(c) Dynamic data language
(d) Detailed data language
6.2 Viva
• What are the types of databases?
• What is the use of DBMS?
• What is a relation schema?
• What is an entity
23 | P a g e
SHRI VAISHNAV VIDHYAPEETH VISHWAVIDYALAYA
SHRI VAISHNAV INSTITUTE OF INFORMATION TECHNOLOGY
EXPERIMENT 6
AIM :
To create a Java Servlet program for session tracking.
THEORY :
Session Tracking is a mechanism used to maintain the state of a user across multiple
requests.
HTTP is a stateless protocol, which means it does not remember previous interactions.
Session tracking helps to identify a user and store user-specific data across multiple pages.
HttpSession is an interface provided by Java Servlet API that:
>Creates a session for each user >Stores user data as attributes
>Tracks user activity
SOURCE CODE :
SHRI VAISHNAV VIDHYAPEETH VISHWAVIDYALAYA
SHRI VAISHNAV INSTITUTE OF INFORMATION TECHNOLOGY
OUTPUT :
VIVA QUESTIONS :
Q1. What is session tracking?
Session tracking is a technique used to maintain user data across multiple requests.
Q2. Why is session tracking needed?
Because HTTP is stateless and cannot remember previous user interactions.
Q3. What is HttpSession?
HttpSession is an interface used to create and manage user sessions in servlets.
Q4. What are different session tracking techniques?
Cookies, URL Rewriting, Hidden Fields, and HttpSession.
Q5. What is session ID?
A unique identifier assigned to each user session.
SHRI VAISHNAV VIDHYAPEETH VISHWAVIDYALAYA
SHRI VAISHNAV INSTITUTE OF INFORMATION TECHNOLOGY
EXPERIMENT 5
AIM :
WAP to create a simple Java Servlet that generates plain text
THEORY :
A Servlet is a Java program that runs on a web server and is used to handle client requests
and generate dynamic web content.
Servlets are mainly used to develop web applications in Java. They run inside a Servlet
Container such as Apache Tomcat.
The main features of Servlets include:
SOURCE CODE :
SHRI VAISHNAV VIDHYAPEETH VISHWAVIDYALAYA
SHRI VAISHNAV INSTITUTE OF INFORMATION TECHNOLOGY
OUTPUT :
VIVA QUESTIONS :
Q1. What is a Servlet?
A Servlet is a Java program that runs on a web server to handle client requests and generate
dynamic web content.
EXPERIMENT 2
AIM : WAP to execute SELECT query using JDBC.
THEORY :
JDBC (Java Database Connectivity) is an API that allows Java programs to interact with
databases. Using JDBC, a Java application can connect to a database, execute SQL queries,
and retrieve results.
To execute a SELECT query, the following steps are required:
1. Load the database driver
2. Establish connection with the database
3. Create a statement
4. Execute the SELECT query
5. Process the result
6. Close the connection
In this program, a SELECT query is used to fetch records from the student table and display
them on the console. And try with resource feature is used to automatically close resources
like database connections and statements after use. even if an exception occurs. It reduces
code complexity and prevents resource leaks.
SOURCE CODE :
OUTPUT :
VIVA QUESTIONS :
1. What is JDBC?
JDBC is an API that allows Java programs to connect and interact with databases.
2. Which method is used to establish a database connection?
[Link]() is used to establish a connection.
3. What is the use of Statement interface?
Statement is used to execute SQL queries in Java.
4. What is ResultSet?
ResultSet stores the data returned by a SELECT query.
5. Why is [Link]() used?
[Link]() moves the cursor to the next row of the ResultSet.
EXPERIMENT 4
AIM : WAP to create a simple student login page using HTML
SOURCE CODE
OUTPUT :
EXPERIMENT 3
AIM : WAP to update student information.
THEORY :
JDBC is an API used to connect Java applications with databases. It allows execution of SQL
queries such as SELECT, INSERT, UPDATE, and DELETE.
• It improves performance
• It prevents SQL injection
• It allows passing values dynamically
In this program, the student’s marks are updated based on student ID using a prepared
statement.
SOURCE CODE :
OUTPUT :
VIVA QUESTIONS :
1. What is PreparedStatement?
PreparedStatement is an interface used to execute parameterized SQL queries.
2. Which SQL command is used to update records?
EXPERIMENT 1
AIM :
To WAP for creating text box ,check box, radio button ,list, submit button.
THEORY :
A Graphical User Interface (GUI) allows users to interact with a program using visual
components instead of typing commands. Java supports GUI programming mainly through
AWT (Abstract Window Toolkit) and Swing.
AWT provides basic GUI components like buttons, text fields, check boxes, labels, lists, and
frames. These components help in building interactive window-based applications.
In this program, GUI components are placed inside a main window.
Each component performs a specific task:
• Text Box is used to enter text input.
• Check Box allows multiple selections.
OUTPUT :
VIVA QUESTIONS :
1. What is GUI?
A GUI (Graphical User Interface) allows users to interact with applications using graphical
components like buttons, text boxes, and lists.
2. What is the use of TextField / JTextField?
It is used to accept single-line text input from the user.
3. Difference between Check Box and Radio Button?
Event handling is the mechanism that handles user actions like button clicks using listeners
such as ActionListener.