PRACTICAL – 1
Write a Java program to create an Applet
import java.applet.Applet;
import java.awt.Graphics;
public class applet extends Applet{
public void paint(Graphics g) {
g.drawString("Hello, this is my first Applet!", 50, 50);
}
}
Kaustubh Mishra
PRACTICAL – 2
Write a Java program to create an Applet that reads Employeeinformation using
parameters and displays name of employee, designation, salary and tax
Import java.applet.Applet;
import java.awt.*;
public class EmployeeApplet extends Applet {
String name, designation;
double salary, tax;
public void init() {
name = getParameter("name");
designation = getParameter("designation");
try {
salary = Double.parseDouble(getParameter("salary"));
} catch (NumberFormatException e) {
salary = 0.0; // Default salary if parsing fails
}
tax = salary * 0.10;
}
public void paint(Graphics g) {
g.drawString("Employee Details:", 20, 20);
g.drawString("Name: " + name, 20, 40);
g.drawString("Designation: " + designation, 20, 60);
g.drawString("Salary: " + salary, 20, 80);
g.drawString("Tax (10%): " + tax, 20, 100);
}
}
Kaustubh Mishra
PRACTICAL – 3
Write a Java program that displays 4 buttons using AWT.
import java.awt.*;
import java.awt.event.*;
public class button extends Frame {
button() {
setLayout(new FlowLayout());
Button b1 = new Button("Button 1");
Button b2 = new Button("Button 2");
Button b3 = new Button("Button 3");
Button b4 = new Button("Button 4");
add(b1);
add(b2);
add(b3);
add(b4);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
setSize(300, 200);
setTitle("AWT Button Example");
setVisible(true);
}
public static void main(String[] args) {
new button();
}
}
Kaustubh Mishra
PRACTICAL – 4
Write a Java program that displays text Field, Check Box and Radio Button using
Swing.
import javax.swing.*;
import java.awt.*;
public class display {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
frame.setLayout(new FlowLayout());
frame.setSize(300, 200);
JTextField textField = new JTextField(15);
JCheckBox checkBox = new JCheckBox("Accept Terms");
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(male); genderGroup.add(female);
frame.add(new JLabel("Enter Name:"));
frame.add(textField);
frame.add(checkBox);
frame.add(new JLabel("Select Gender:"));
frame.add(male);
frame.add(female);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Kaustubh Mishra
PRACTICAL – 5
Write a Java Program to create multiple frames, which create a Frame2 with a
back button, such that when a user clicks back button, Frame 2 is closed, and we
see the Frame l only?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class frame {
public static void main(String[] args) {
new Frame1();
}
}
class Frame1 extends JFrame {
public Frame1() {
setTitle("Frame 1");
setSize(300,200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton nextButton = new JButton("Next");
nextButton.addActionListener(e -> {
new Frame2();
setVisible(false);
});
add(nextButton);
setVisible(true);
}
}
class Frame2 extends JFrame {
public Frame2() {
setTitle("Frame 2");
setSize(300, 200);
setLayout(new FlowLayout());
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> {
new Frame1();
dispose();
});
add(backButton);
setVisible(true);
}
}
Kaustubh Mishra
PRACTICAL – 6
Write a Java Program to create a frame using swing in which create a push
button with a label and image. When the button is clicked an image is
displayed in the Frame?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class image extends JFrame {
private JLabel imageLabel;
public image() {
setTitle("Image Display Frame");
setSize(400, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Show Image");
imageLabel = new JLabel();
button.addActionListener(e -> {
ImageIcon icon = new ImageIcon("A:\\Admin codes\\Java\\javagui\\src\\OIP.JPG");
// Change "image.jpg" to your image
fileimageLabel.setIcon(icon);
});
add(button);
add(imageLabel);
setVisible(true);
}
public static void main(String[] args) {
new image();
}
}
Kaustubh Mishra
PRACTICAL – 7
Write a Java Program to execute select Query using JDBC
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String user = "root";
String password = "hellomysql@123";
String query = "SELECT id, name, designation, salary FROM employees";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String designation = rs.getString("designation");
double salary = rs.getDouble("salary");
System.out.println("ID: " + id + ", Name: " + name + ", Designation: " +designation + ",
Salary: " + salary);
}
rs.close();
stmt.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Kaustubh Mishra
PRACTICAL – 8
Write a Java Program for basic Arithmetic Function Such as Addition,
Subtraction, Multiplication and Division using JSP.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operations</title>
</head>
<body>
<h2>Basic Arithmetic Operations</h2>
<form method="post">
Number 1: <input type="text" name="num1" required><br><br>
Number 2: <input type="text" name="num2" required><br><br>
<input type="submit" name="operation" value="Addition">
<input type="submit" name="operation" value="Subtraction">
<input type="submit" name="operation" value="Multiplication">
<input type="submit" name="operation" value="Division">
</form>
<%
String num1Str = request.getParameter("num1");
String num2Str = request.getParameter("num2");
String operation = request.getParameter("operation");
if (num1Str != null && num2Str != null && operation != null) {
try {
double num1 = Double.parseDouble(num1Str);
double num2 = Double.parseDouble(num2Str);
double result = 0;
Kaustubh Mishra
switch (operation) {
case "Addition":
result = num1 + num2;
break;
case "Subtraction":
result = num1 - num2;
break;
case "Multiplication":
result = num1 * num2;
break;
case "Division":
if (num2 != 0) {
result = num1 / num2;
} else {
out.println("<p style='color:red;'>Error: Division by zero!</p>");
return;
}
break;
}
out.println("<h3>Result: " + result + "</h3>");
} catch (NumberFormatException e) {
out.println("<p style='color:red;'>Please enter valid numbers!</p>");
}
}
%>
</body>
</html>
Kaustubh Mishra
PRACTICAL – 9
Write a servlet program to create a simple servlet and test it
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println(" Hello this is my Servlet!");
}
}
Kaustubh Mishra
PRACTICAL – 10
Write a Java program to create a bean that display employee name, salary,
designation and company?
Employee.java (bean file):-
package com.example;
import java.io.Serializable;
public class Employee implements Serializable {
private String name;
private double salary;
private String designation;
private String company;
public Employee() {
public Employee(String name, double salary, String designation, String company) {
this.name = name;
this.salary = salary;
this.designation = designation;
this.company = company;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
Kaustubh Mishra
Myservlet.java (servlet file):-
package com.example;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Employee emp = new Employee("John Doe", 50000, "Software Engineer", "TechCorp");
request.setAttribute("employee",emp);
request.getRequestDispatcher("employee.jsp").forward(request, response);
}
}
employee.jsp (Jsp file):-
<!-- Location: src/main/webapp/employee.jsp -->
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="com.example.Employee" %>
<jsp:useBean id="employee" class="com.example.Employee" scope="request" />
<!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<h2>Employee Information</h2>
<p><b>Name:</b> <%= employee.getName() %></p>
<p><b>Salary:</b> <%= employee.getSalary() %></p>
<p><b>Designation:</b> <%= employee.getDesignation() %></p>
<p><b>Company:</b> <%= employee.getCompany() %></p>
</body>
</html>
Kaustubh Mishra