Advanced Java Lab Manual for CSE Students
Advanced Java Lab Manual for CSE Students
Department of CSE\IT\AI&DS
Advance Java Lab
Student Name=Chirag jain
Roll no =23EGJCS059
LAB MANUAL
GLOBAL INSTITUTE OF TECHNOLOGY
ADVANCED JAVA
PARTICLE RECORD
Jaipur (Rajasthan)-302020
Session: 2025-2026
INDEX
1 18-08-25
Write a Java program to demonstrate various Swing
components.
.2
Write a Java program to implement event handling on
Swing components. 18-08-25
3. 12-09-25
Develop a Swing-based Calculator application using
buttons, text fields, and event handling.
4 12-09-25
Write a Java program that establishes a connection
with a database using JDBC and displays all the
records from a specified table.
5
19-09-25
Write a Java program to insert a new record into a
table using a static SQL query (Statement).
6
19-09-25
Write a Java program to update an existing record in a
table using a dynamic query (PreparedStatement).
7
31-10-25
Write a Java program to demonstrate a Distributed
Application
8
04-11-25
Implementation of CRUD Operations using JPA with
Hibernate
9
05-11-25
Write a simple Spring Boot application that returns
“Hello World” as a web response.
10
07-11-25
Write a RESTful web service that performs
CRUD(Create, Read, Update, Delete) operations on
Employee data.
Experiment – 1
Title: Write a Java program to demonstrate various Swing components.
Program / Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Viva Questions
1. What is Java?
Simple: Java is easy to learn. The syntax of Java is based on C++ which makes easier to
write the program in it.
Object-Oriented: Java follows the object-oriented paradigm which allows us to
maintain our code as the combination of different type of objects that incorporates both
data and behavior.
Secured: Java is secured because it doesn't use explicit pointers. Java also provides the
concept of Byte Code and Exception handling which makes it more secured.
Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for the
program execution.
Multithreaded: We can write Java programs that deal with many tasks at once by
defining multiple threads. The main advantage of multi-threading is that it doesn't
occupy memory for each thread. It shares a common memory area. Threads are
important for multi-media, Web applications, etc.
4. What are the main differences between the Java platform and other
platforms?
There are the following differences between the Java platform and other platforms.
·Java is the software-based platform whereas other platforms may be the hardware
platforms or software-based platforms.
· Java is executed on the top of other hardware platforms whereas other platforms can
only have the hardware components.
· We can also have the hidden classes that are not visible outside and used by the
package.
Program / Code:
import [Link].*;
import [Link].*;
import [Link].*;
// Create frame
// Create components
// Set layout
[Link](new FlowLayout());
// Add components
[Link](label);
[Link](textField);
[Link](button);
[Link](new ActionListener() {
if (![Link]()) {
} else {
});
// Frame settings
[Link](300, 150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
Output:
Viva Question
It stands for It stands for Java It stands for Java Virtual Machine.
Java Runtime
Development Environment.
Kit.
Set. Event. Provides interfaces and classes for dealing with different types of events
fired by AWT components. This package defines events and event listeners, as well as
event listener adapters, which are convenience classes to make easier the process of
writing event listeners easier.
Experiment – 3
Title: Develop a Swing-based Calculator application using buttons, text
fields, and event handling.
Program / Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
class Calculator{
[Link]("Calculator");
[Link](400,100);
[Link](400,350);
[Link](null);
[Link](150,50,100,20);
[Link](label);
[Link](50,100,150,20);
[Link](label1);
[Link](200,100,120,20);
[Link](textField1);
[Link](50,150,150,20);
[Link](label2);
[Link](200,150,120,20);
[Link](textField2);
[Link](50,200,150,20);
[Link](label3);
[Link](200,200,120,20);
[Link](textField3);
[Link](70,250,50,30);
[Link](new ActionListener(){
[Link]([Link]());
}
});
[Link](140,250,50,30);
[Link](new ActionListener(){
[Link]([Link]());
}
});
[Link](210,250,50,30);
[Link](new ActionListener(){
[Link]([Link]());
}
});
[Link](280,250,50,30);
[Link](new ActionListener(){
}
});
[Link](button1);
[Link](button2);
[Link](button3);
[Link](button4);
[Link](true);
Output:
Viva Questions
Ø Swing supports a pluggable look and feel. The three PLAFs available to all users
are Metal (default), Windows, and Motif.
Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation
Classes (JFC) – an API for providing a graphical user interface (GUI) for Java
programs. Swing was developed to provide a more sophisticated set of GUI
components than the earlier Abstract Window Toolkit (AWT).
· Image Icon. The Image Icon component creates an icon sized-image from an
image residing at the source URL. ...
·J Button. J Button class is used to create a push-button on the UI. ...
·J Label. ...
· J Text Field. ...
· J Text Area. ...
· J Password Field. ...
· J Check Box.
· J Radio Button
Program / Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
try {
[Link](driverName);
Connection connection =
[Link](url,userName,userPass);
Statement statement =
[Link]();
ResultSet resultSet =
[Link](sql);
while([Link]()) {
[Link]("EmpNo is:
"+[Link](1));
[Link]("EmpName is:
"+[Link](2));
[Link]("EmpSal is:
"+[Link](3));
[Link]("---------------------------");
}
[Link]();
}catch(ClassNotFoundException e) {
}catch(SQLException e) {
[Link](e);
}
}
Output:
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
Viva Questions
1. What are JDBC and ODBC?
JDBC Driver Manager: It is the class in JDBC API. It loads the JDBC driver in a
Java application for establishing a connection with the database.
JDBC Driver is a software component that enables Java application to interact with the
database.
There are 4 types of JDBC drivers:
· JDBC-ODBC bridge driver: The JDBC-ODBC bridge driver uses the ODBC driver to
connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls
into the ODBC function calls. This is now discouraged because of the thin driver. It is
easy to use and can be easily connected to any database.
· Native-API driver (partially java driver): The Native API driver uses the client-side
libraries of the database. The driver converts JDBC method calls into native calls of the
database API. It is not written entirely in Java. Its performance is better than
JDBC-ODBC bridge driver. However, the native driver must be installed on each client
machine.
·Network Protocol driver (fully java driver): The Network Protocol driver uses
middleware (application server) that converts JDBC calls directly or indirectly into the
vendor-specific database protocol. It is entirely written in Java. There is no requirement
of the client-side library because of the application server that can perform many tasks
like auditing, load balancing, logging, etc.
·Thin driver (fully java driver): The thin driver converts JDBC calls directly into the
vendor-specific database protocol. That is why it is known as the thin driver. It is
entirely written in Java language. Its performance is better than all other drivers
however these drivers depend upon the database.
The [Link] package contains the following interfaces and classes for JDBC API.
Prepared Statement: The Prepare Statement object is created by using the prepare
Statement () method of Connection class. It is used to execute the parameterized query.
Result Set: The object of Result Set maintains a cursor pointing to a row of a table.
Initially, cursor points before the first row. The execute Query () method of Statement
interface returns the Result Set object.
Result Set Meta Data: The object of Result Set Meta Data interface contains the
information about the data (table) such as number of columns, column name, column
type, etc. The get Meta Data () method of Result Set returns the object of Result Set
Meta Data.
In JDBC, Statements are used to send SQL commands to the database and receive data
from the database. There are various methods provided by JDBC statements such as
execute (), execute Update (), execute Query, etc. which helps you to interact with the
database.
There are three types of JDBC statements given in the following table.
Experiment – 5
Title: Write a Java program to insert a new record into a table using a
static SQL query (Statement).
Program / Code:
import [Link];
import [Link];
import [Link];
import [Link];
try {
[Link]("[Link]");
Connection connection =
[Link](url,uname,upass);
Statement statement =
[Link]();
int x = [Link](sql);
[Link]();
}catch(ClassNotFoundException e)
{
}
catch(SQLException e)
{
[Link](e);
}
}
Output:
1 Record Added.
Viva Questions
1. What is DNS?
● DNS was introduced by Paul Mock aperies and Jon Postal in 1983.
● It is a naming system for all the resources over the internet which includes
physical nodes and applications. It is used to locate to resource easily over
a network.
● DNS is an internet which maps the domain names to their associated IP
addresses.
● Without DNS, users must know the IP address of the web page that you
wanted to access.
2. What is NIC?
NIC stands for Network Interface Card. It is a peripheral card attached to the
PC to connect to a network. Every NIC has its own MAC address that
identifies the PC on the network. It provides a wireless connection to a local
area network. NICs were mainly used in desktop computers.
· Domain: Users that use the same database will be grouped into a single
domain.
OSI reference model: OSI reference model is an ISO standard which defines a
networking framework for implementing the protocols in seven layers. These
seven layers can be grouped into three categories:
· Network layer: Layer 1, Layer 2 and layer 3 are the network layers.
· Application layer. Layer 5, Layer 6 and Layer 7 are the application layers.
Program / Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
try {
[Link](driverName);
Connection connection =
[Link](url,uName,uPass);
PreparedStatement preparedStatement =
[Link](sql);
while(true) {
[Link]("Enter EmpNo.: ");
[Link]("Enter new
EmpName.: ");
Double empSal =
[Link]();
[Link](3,empNo);
[Link](1,empName);
[Link](2,empSal);
int x =
[Link]();
[Link](x+" Entry
updated.");
Character ch =
[Link]().charAt(0);
if (ch=='n') {
break;
}
}
[Link]();
[Link]();
}catch(ClassNotFoundException e) {
}catch(SQLException e) {
[Link](e);
}
}
Output:
1 Entry updated.
1. What is the difference between a web server web container and application server?
The main difference between the web containers and application server is that most web
containers such as Apache Tomcat implements only basic JSR like Servlet, JSP, JSTL
where Application servers implement the entire Java EE Specification. Every
application server contains a web container.
Container Types
·HTTP servlets. Extending Java. Servlet. Http Servlet. Have built-in HTTP protocol
support and are more useful in a Sun Java System Web Server environment.
Experiment – 7
Title: Write a Java program to demonstrate a Distributed Application.
Program / Code:
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("Sent text..");
PrintWriter pw = new
PrintWriter([Link](),true);
while(true) {
[Link](" ");
[Link](sc.
[Link](msg);
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Socket s = [Link]();
[Link]("Connected");
Scanner sc = new Scanner([Link]);
PrintWriter pw = new
PrintWriter([Link](),true);
while(true) {
[Link](msg);
[Link](" ");
[Link]([Link]());
Viva Questions
1. What is the use of JSP in Java?
Java Server Pages (JSP) is a programming tool on the application server side that
supports platform-independent and dynamic methods to construct Web-based
applications. Much as Servlet technology does, the JSP method provides a web
application.
A JSP life cycle is defined as the process from its creation till the destruction. This is
similar to a servlet life cycle with an additional step which is required to compile a JSP
into servlet.
Servlets are Java-based codes. JSP are HTML-based codes. Servlets are harder to code,
as here, the HTML codes are written in Java. JSPs are easier to code, as here Java is
coded in HTML. In an MVC architecture, Servlets act as the controllers.
A JSP file extension is given to a server-generated web page containing Java code. The
code is parsed by the web server, which generates HTML. Since the Java code is parsed
on the server, the end user never sees the JSP code, but only the HTML generated by
the Java code in the page.
The advantages of JSP are twofold. First, the dynamic part is written in Java, not
Visual Basic or other MS specific language, so it is more powerful and easier to use.
Second, it is portable to other operating systems and non-Microsoft Web servers.
Experiment – 8
Title: Implementation of CRUD Operations using JPA with Hibernate
Program / Code:
[Link]
</project>
src/main/resources/META-INF/[Link]
[Link]
package [Link];
import [Link].*;
@Entity
@Table(name = "students")
@Id
@GeneratedValue(strategy = [Link])
public Student() {}
[Link] = name;
[Link] = city;
[Link] = marks;
}
@Override
}
[Link]
package [Link];
import [Link].*;
EntityManagerFactory emf =
[Link]("student_unit");
EntityManager em = [Link]();
// CREATE
[Link]().begin();
[Link](s1);
[Link]().commit();
// READ
// UPDATE
[Link]().begin();
[Link](90.0);
[Link](found);
[Link]().commit();
[Link]("Updated Record: " + found);
// DELETE
[Link]().begin();
[Link](found);
[Link]().commit();
[Link]("Deleted Record");
[Link]();
[Link]();
}
OUTPUT :
Hibernate:
insert
into
students
values
(?, ?, ?)
Hibernate:
update
students
set
city=?,
marks=?,
name=?
where
id=?
Hibernate:
delete
from
students
where
id=?
Deleted Record
VIVA QUESTION
2. How do you perform the Create (Insert) operation in JPA using Hibernate?
To insert a new record, you use the persist() method of
EntityManager.
Example:
EntityManagerFactory emf =
[Link]("my-pu");
EntityManager em = [Link]();
[Link]().begin();
[Link]("John");
[Link](22);
[Link]().commit();
[Link]();
[Link]();
Answer:
There are two common ways:
[Link]().commit();
[Link]().commit();
Program / Code:
[Link]
package [Link];
import [Link];
import [Link];
@SpringBootApplication
[Link]([Link], args);
}
[Link]
package [Link];
import [Link];
import [Link];
@RestController
@GetMapping("/")
}
Ans: There are mainly four scope values available for <[Link]> tag.
● page
● request
● application
● session
● Null
● Boolean
● String
● Integer Float
4. List down the major differences between the JSP Custom Tags and Java Beans.
Ans: The Major Differences between JSP Custom Tags and Java Beans are as follows:
5. Mention the Implicit Objects in a JSP.
Ans: JSP implicit objects are created during the translation phase of JSP to the servlet.
These objects can be directly used in scriptlets that go in the service method. They are
created by the container automatically, and they can be accessed using objects.
1. Out
2. Request
3. Response
4. Config
5. Application
6. Session
7. PageContext
8. Page
9. Exception
● jsp Init()
● disservice()
● Jsp Destroy
Jsp Init (): Container calls jsp Init () method to initialize servlet instance. It is called
once for the servlet instance and preceded every other method.
Jsp Service (): Container calls _jsp Service () method for each request and passes it on
to the objects.
Jsp Destroy (): Container calls the jsp Destroy () just before destruction of the instance.
Experiment – 10
Title: Write a RESTful web service that performs CRUD (Create, Read, Update,
Delete) operations on Employee data.
Program / Code:
[Link]
package [Link];
import [Link];
import [Link];
@SpringBootApplication
[Link]([Link], args);
}
[Link]
package [Link];
public Employee() {}
[Link] = id;
[Link] = name;
[Link] = salary;
}
[Link]
package [Link];
import [Link];
import [Link].*;
import [Link].*;
@RestController
@RequestMapping("/employees")
@PostMapping
[Link]([Link](), emp);
}
@GetMapping
return [Link]();
}
@GetMapping("/{id}")
return [Link](id);
}
@PutMapping("/{id}")
[Link](id, emp);
}
@DeleteMapping("/{id}")
[Link](id);
}
Viva Questions
Example:
1 <html>
2 <body>
5 </body>
6 </html>
JSP Script let Tag can only declare Declaration Tag declares Methods and
Variables Variables
Script let Tag is placed within _jsp Declaration Tag is placed outside _jsp
Service() Service()
3. How are JSP (Java Server Pages) better than ASP (Active Server Pages)?
● The dynamic part of the code is written in Java, not in Visual Basic or the
Microsoft-specific language. Hence, it is powerful and easier to use.
● It is portable to other operating systems and Non-Microsoft Web servers.
4. Mention the advantages of JSP over Pure Servlets?
Ans: Some of the Major Advantages of JSP over Pure Servlets are as discussed below:
· It is more convenient to write and modify normal HTML than to have plenty of print
ln statements that generate the HTML.
· Platform independence.
5. What are the steps involved in reading data from a form using JSP?
Ans: The data parsing is a JSP is Automatic. It is done through the following steps
depending on the situation.
1. Get Parameter (): request. Get Parameter () method is called to get the value of
the form parameter.
2. Get Parameter Values (): This method is called if the parameter appears more
than once and returns multiple values.
3. Get Parameter Names (): This method is called if the user wants a complete list
of all parameters in the current request.
Get Input Stream (): This method is used to read binary data stream coming from the
client.