TRIBHUVAN UNIVERSITY
Faculty of Humanities and Social Sciences
JANAMAITRI MULTIPLE CAMPUS
Kuleshwar, Kathmandu
A LAB REPORT ON Network Programming
In the partial fulfillment of the requirements for the Bachelors of
Computer Applications
Submitted by:
Bibek Dhakal
(TU Reg. No: 6-2-263-08-2021)
Under the supervision of
Mr. Prakash Adhakari
Department of Computer Applications
Janamaitri Multiple Campus
Letter of Recommendation
This report entitled “A lab report on Network Programming” by Bibek Dhakal(TU Reg. No:
6-2-263-08-2021) has been prepared under our supervision for partial fulfillment of the
requirement of Bachelors of Computer Applications (BCA) 6th semester. I, therefore recommend it
for evaluation by the thesis committee.
------------------------------------
- Mr. Prakash Adhakari
i
Certificate of Acceptance
This report "A lab report on Network Programming" written by Bibek Dhakal(TU Reg.
No: 6-2-263-08-2021) has been accepted as partial fulfillment of the requirement for a
Bachelors of Computer Applications (BCA) 6th Semester.
Evaluation Committee:
….……………….. ………………….
External Signature Supervisor
ii
Declaration Letter
I declare that I completed this work on my own and that information which has been directly or
indirectly taken from other sources has been noted as such. Neither this, nor a similar work, has
been published or presented to an examination committee.
Bibek Dhakal
TU Reg. No: 6-2-263-08-2021
BCA 6th Semester
Janamaitri Multiple Campus
iii
Acknowledgement
This research report is prepared for the partial fulfillment of the requirement of BCA 6th
Semester with the prescribed rules and regulations of the TU board. Firstly, I would like to
acknowledge special thanks to Mr. Prakash Adhakari, our Network Programming teacher, for
giving me an opportunity to prepare a report on "A lab report on Network Programming". His
guidance, comments and recommendations helped me to conduct this research. Secondly, I
would like to thank all my friends who inspired me during this lab report. I would also like to
thank my family members who shared their knowledge in this report topic. Thank you!
iv
Self-Declaration Letter
I, Bibek Dhakal, candidate of BCA 6th Semester, hereby declare that the contents included on this
Practical Lab Book is entirely done by myself as a part of study. None of the portion on the Lab
Book is duplicated or copied from any other source. I am liable and obliged to all the corrections
and suggestions given by the faculty members, Internal Examiner as well as External Examiner
during the evaluation.
Name: --------------------------------------------------------------------
T.U. Regd. No.: - -------------------------------------------------------
Level: - -------------------------------- Semester: - -------------------
v
Contents
A LAB REPORT ON Network Programming...................................................................................................i
Letter of Recommendation......................................................................................................................i
Certificate of Acceptance........................................................................................................................ii
Declaration Letter..................................................................................................................................iii
Acknowledgement.................................................................................................................................iv
Self-Declaration Letter............................................................................................................................v
LAB NO-1................................................................................................................................................1
Lab NO-2.................................................................................................................................................4
Lab No -3.................................................................................................................................................6
Lab No – 4...............................................................................................................................................7
Lab No- 5.................................................................................................................................................8
Lab No -6.................................................................................................................................................9
Lab No-7...............................................................................................................................................10
Lab No-8...............................................................................................................................................11
Lab No-9...............................................................................................................................................12
Lab No -10.............................................................................................................................................13
Lab No-11.............................................................................................................................................14
Lab No-12.............................................................................................................................................15
Lab No-13.............................................................................................................................................17
Lab No-14.............................................................................................................................................19
Lab No-15.............................................................................................................................................20
Lab No-16.............................................................................................................................................21
Lab No-17.............................................................................................................................................23
vi
LAB NO-1
Create a GUI application to demonstrate FlowLayout by adding buttons in a
flow.
import [Link].*;
import [Link].*;
public class flowlayout {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout");
[Link](new FlowLayout());
for (int i = 1; i <= 5; i++) {
[Link](new JButton("Button " + i));
}
[Link](400, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
1
LAB NO-2
Create a GUI application to demonstrate FlowLayout by adding buttons in a
flow.
import [Link].*;
public class HelloApplet {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello");
JLabel label = new JLabel("Hello, World!", [Link]);
[Link](label);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
2
3
LAB NO-3
Create a Swing application with components like JButton, JLabel, and
JTextField added to a JPanel, which is then added to a JFrame.
import [Link].*;
public class q3 {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
JPanel panel = new JPanel();
JLabel label = new JLabel("Name:");
JTextField textField = new JTextField(15);
JButton button = new JButton("Submit");
[Link](label);
[Link](textField);
[Link](button);
[Link](panel);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
4
LAB NO-4
Design a calculator-like GUI using BorderLayout with buttons at different
positions (NORTH, SOUTH, EAST, WEST, and CENTER).
import [Link].*;
import [Link].*;
public class q4 {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout");
[Link](new BorderLayout());
[Link](new JButton("North"), [Link]);
[Link](new JButton("South"), [Link]);
[Link](new JButton("East"), [Link]);
[Link](new JButton("West"), [Link]);
[Link](new JButton("Center"), [Link]);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
5
6
LAB NO-5
Create a GUI with a JCheckBox and JRadioButton to select favorite
programming languages. Show the selected options on a JLabel.
import [Link].*;
public class q5 {
public static void main(String[] args) {
JFrame frame = new JFrame("Selection");
JPanel panel = new JPanel();
JCheckBox java = new JCheckBox("Java");
JCheckBox python = new JCheckBox("Python");
JRadioButton beginner = new JRadioButton("Beginner");
JRadioButton expert = new JRadioButton("Expert");
ButtonGroup group = new ButtonGroup();
[Link](beginner);
[Link](expert);
JLabel label = new JLabel("Selected:");
JButton show = new JButton("Show");
[Link](e -> {
String text = "Languages: ";
if ([Link]()) text += "Java ";
if ([Link]()) text += "Python ";
text += "| Level: ";
if ([Link]()) text += "Beginner";
if ([Link]()) text += "Expert";
7
[Link](text);
});
[Link](java);
[Link](python);
[Link](beginner);
[Link](expert);
[Link](show);
[Link](label);
[Link](panel);
[Link](400, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
8
9
LAB NO-6
Create a GUI with a GridLayout that displays a 3x3 grid of buttons labeled 1
to 9.
import [Link].*;
import [Link].*;
public class q6 {
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout 3x3");
[Link](new GridLayout(3, 3));
for (int i = 1; i <= 9; i++) {
[Link](new JButton([Link](i)));
[Link](300, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
10
LAB NO-7
Create a GUI with a JComboBox to select a country and a JSlider to set age,
and display the selections on a JLabel.
import [Link].*;
public class q7 {
public static void main(String[] args) {
JFrame frame = new JFrame("Combo and Slider");
JPanel panel = new JPanel();
JComboBox<String> countries = new JComboBox<>(new String[]{"India", "USA", "UK"});
JSlider ageSlider = new JSlider(0, 100, 25);
JLabel label = new JLabel("Selected:");
JButton show = new JButton("Show");
[Link](e -> {
String country = (String) [Link]();
int age = [Link]();
[Link]("Country: " + country + ", Age: " + age);
});
[Link](countries);
[Link](ageSlider);
[Link](show);
[Link](label);
[Link](panel);
[Link](400, 150);
[Link](JFrame.EXIT_ON_CLOSE);
11
[Link](true);
12
LAB NO-8
Create a GUI with a button. When the button is clicked, display "Button
Clicked!" in a JLabel.
import [Link].*;
public class q8 {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Click");
JButton button = new JButton("Click Me");
JLabel label = new JLabel("Not clicked");
[Link](e -> [Link]("Button Clicked!"));
JPanel panel = new JPanel();
[Link](button);
[Link](label);
[Link](panel);
[Link](250, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
13
14
LAB NO-9
Implement a mouse listener using an adapter class to handle mouse events like
mouseClicked() and mouseEntered().
import [Link].*;
import [Link].*;
public class q9 {
public static void main(String[] args) {
JFrame frame = new JFrame("Mouse Adapter");
JLabel label = new JLabel("Hover or Click");
[Link](new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at " + [Link]());
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered");
});
[Link](label);
[Link](300, 150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
15
}
16
LAB NO-10
Write a program that shows a confirmation dialog when the user attempts to
close the window.
import [Link].*;
import [Link].*;
public class q10 {
public static void main(String[] args) {
JFrame frame = new JFrame("Close Confirmation");
[Link](JFrame.DO_NOTHING_ON_CLOSE);
[Link](300, 150);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int confirmed = [Link](frame,
"Are you sure you want to exit?", "Confirm Exit",
JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
[Link]();
});
[Link](true);
17
18
LAB NO-11
Write a program to connect to a MySQL/PostgreSQL database and display the
database metadata.
import [Link].*;
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/school?useSSL=false";
String user = "root";
String password = ""; //
try (Connection conn = [Link](url, user, password)) {
DatabaseMetaData metaData = [Link]();
[Link]("Database Product Name: " + [Link]());
[Link]("Database Product Version: " + [Link]());
[Link]("Driver Name: " + [Link]());
[Link]("Driver Version: " + [Link]());
[Link]("URL: " + [Link]());
[Link]("Username: " + [Link]());
ResultSet tables = [Link](null, null, "%", new String[]{"TABLE"});
[Link]("\nTables in the database:");
while ([Link]()) {
[Link]([Link]("TABLE_NAME"));
}
} catch (SQLException e) {
[Link]();
}
}
}
19
LAB NO-12
Create a table Students with columns id, name, and grade using a Java
program.
import [Link].*;
import [Link];
public class Main {
private static final String URL = "jdbc:mysql://localhost:3306/student_db?
useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "";
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection conn = [Link](URL, USER, PASSWORD);
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("\nStudent Management System");
[Link]("1. Add Student");
[Link]("2. View Students");
[Link]("3. Update Student");
[Link]("4. Delete Student");
[Link]("5. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
[Link](); // Consume newline
switch (choice) {
case 1:
addStudent(conn, scanner);
break;
case 2:
viewStudents(conn);
break;
case 3:
updateStudent(conn, scanner);
break;
case 4:
deleteStudent(conn, scanner);
break;
case 5:
[Link]("Exiting...");
20
[Link]();
return;
default:
[Link]("Invalid choice! Try again.");
}
}
} catch (ClassNotFoundException e) {
[Link]("MySQL JDBC Driver not found! Add MySQL Connector JAR.");
[Link]();
} catch (SQLException e) {
[Link]("Database connection failed!");
[Link]();
}
}
private static void addStudent(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Student Name: ");
String name = [Link]();
[Link]("Enter Age: ");
int age = [Link]();
[Link]();
[Link]("Enter Grade: ");
String grade = [Link]();
String sql = "INSERT INTO students (name, age, grade) VALUES (?, ?, ?)";
PreparedStatement stmt = [Link](sql);
[Link](1, name);
[Link](2, age);
[Link](3, grade);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("Student added successfully!");
}
[Link]();
}
private static void viewStudents(Connection conn) throws SQLException {
String sql = "SELECT * FROM students";
Statement stmt = [Link]();
ResultSet rs = [Link](sql);
[Link]("\nStudent List:");
[Link]("------------------------------------------------");
[Link]("%-5s %-20s %-5s %-20s%n", "ID", "Name", "Age", "Grade");
21
[Link]("------------------------------------------------");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String grade = [Link]("grade");
[Link]("%-5d %-20s %-5d %-20s%n", id, name, age, grade);
}
[Link]();
}
private static void updateStudent(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Student ID to update: ");
int id = [Link]();
[Link]();
[Link]("Enter New Name: ");
String name = [Link]();
[Link]("Enter New Age: ");
int age = [Link]();
[Link]();
[Link]("Enter New Grade: ");
String grade = [Link]();
String sql = "UPDATE students SET name = ?, age = ?, grade = ? WHERE id = ?";
PreparedStatement stmt = [Link](sql);
[Link](1, name);
[Link](2, age);
[Link](3, grade);
[Link](4, id);
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Student updated successfully!");
} else {
[Link]("Student ID not found.");
}
[Link]();
}
private static void deleteStudent(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Student ID to delete: ");
int id = [Link]();
String sql = "DELETE FROM students WHERE id = ?";
22
PreparedStatement stmt = [Link](sql);
[Link](1, id);
int rowsDeleted = [Link]();
if (rowsDeleted > 0) {
[Link]("Student deleted successfully!");
} else {
[Link]("Student ID not found.");
}
[Link]();
}
}
23
LAB NO-13
Write a program to perform a transaction with multiple SQL statements.
Rollback the transaction in case of an error.
import [Link].*;
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/rollback?useSSL=false";
String user = "root";
String password = "";
try (Connection conn = [Link](url, user, password)) {
[Link](false);
try {
String insert1 = "INSERT INTO students (name, age, grade) VALUES (?, ?, ?)";
PreparedStatement stmt1 = [Link](insert1);
[Link](1, "Abishek");
[Link](2, 21);
[Link](3, "A");
[Link]();
String insert2 = "INSERT INTO students (name, age, grade) VALUES (?, ?, ?)";
PreparedStatement stmt2 = [Link](insert2);
[Link](1, "Suresh");
[Link](2, 22);
[Link](3, "B");
[Link]();
[Link]();
[Link]("Transaction committed successfully!");
} catch (SQLException | ArithmeticException e) {
[Link]("Error occurred. Rolling back transaction...");
[Link]();
[Link]();
}
} catch (SQLException e) {
[Link]();
24
}
}
}
25
LAB NO-14
Create a program that fetches student records in both forward and reverse
directions using scrollable result sets.
import [Link].*;
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false";
String user = "root";
String password = "";
try (Connection conn = [Link](url, user, password)) {
String sql = "SELECT id, name, age, grade FROM students";
Statement stmt = [Link](ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = [Link](sql);
[Link]("Fetching records in forward direction:");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String grade = [Link]("grade");
[Link]("ID: %d, Name: %s, Age: %d, Grade: %s%n", id, name, age, grade);
}
[Link]("\nFetching records in reverse direction:");
[Link]();
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String grade = [Link]("grade");
[Link]("ID: %d, Name: %s, Age: %d, Grade: %s%n", id, name, age, grade);
}
[Link]();
26
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
27
LAB NO-15
Demonstrate the use of CachedRowSet to fetch data from a database and
update it offline.
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false";
String user = "root";
String password = "";
try {
CachedRowSet crs = [Link]().createCachedRowSet();
[Link](url);
[Link](user);
[Link](password);
[Link]("SELECT id, name, age, grade FROM students");
[Link]();
while ([Link]()) {
[Link]("ID: %d, Name: %s, Age: %d, Grade: %s%n",
[Link]("id"), [Link]("name"),
[Link]("age"), [Link]("grade"));
}
[Link]();
if ([Link]()) {
[Link]("name", "Updated Name");
[Link]("age", [Link]("age") + 1);
[Link]();
}
[Link]();
while ([Link]()) {
[Link]("ID: %d, Name: %s, Age: %d, Grade: %s%n",
[Link]("id"), [Link]("name"),
[Link]("age"), [Link]("grade"));
}
28
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
29
LAB NO-16
Creating a Simple JavaBean
Write a JavaBean class called StudentBean with the following properties:
name (String)
age (int)
email (String)
Implement getters and setters for each property.
Demonstrate how to create an object of StudentBean and set its
properties.
[Link]
import [Link];
public class StudentBean implements Serializable {
private String name;
private int age;
private String email;
public StudentBean() {}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
[Link] = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
}
30
}
[Link] (Demonstration)
public class Main {
public static void main(String[] args) {
StudentBean student = new StudentBean();
[Link]("Chandu Rawal");
[Link](21);
[Link]("chandu@[Link]");
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
[Link]("Email: " + [Link]());
}
}
31
LAB NO-17
Implementing PropertyChangeListener
Modify the StudentBean class to support bound properties using
PropertyChangeListener.
Fire an event when the age property is updated.
Write a main program to test property change notifications.
[Link] (with PropertyChangeSupport)
import [Link];
import [Link];
import [Link];
public class StudentBean implements Serializable {
private String name;
private int age;
private String email;
private PropertyChangeSupport support;
public StudentBean() {
support = new PropertyChangeSupport(this);
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getAge() {
return age;
}
public void setAge(int newAge) {
int oldAge = [Link];
[Link] = newAge;
[Link]("age", oldAge, newAge);
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
}
32
public void addPropertyChangeListener(PropertyChangeListener listener) {
[Link](listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
[Link](listener);
}
}
[Link] (Testing Property Change)
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
StudentBean student = new StudentBean();
[Link](21);
[Link](new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
[Link]("Property '" + [Link]() +
"' changed from " + [Link]() +
" to " + [Link]());
}
});
[Link](22);
}
33
LAB NO-18
Using the BeanInfo Interface
Create a custom BeanInfo class for StudentBean that specifies which
properties should be exposed.
Modify the introspection behavior to hide one property.
[Link]
import [Link];
public class StudentBean implements Serializable {
private String name;
private int age;
private String email;
public StudentBean() {}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
[Link] = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
}}
[Link]
import [Link].*;
public class StudentBeanBeanInfo extends SimpleBeanInfo {
@Override
34
public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor name = new PropertyDescriptor("name", [Link]);
PropertyDescriptor age = new PropertyDescriptor("age", [Link]);
return new PropertyDescriptor[]{name, age};
} catch (IntrospectionException e) {
[Link]();
return null;
}
}
}
[Link]
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) throws Exception {
BeanInfo info = [Link]([Link]);
for (PropertyDescriptor pd : [Link]()) {
[Link]("Property: " + [Link]());
}
}
}
35
LAB NO-19
Write a servlet that displays "Hello, World!" in the browser.
[Link]
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
36
LAB NO-
Create an RMI application where the client sends a number, and the server responds
with its factorial.
37