Practical No 1
Aim: Implements and create five button and put on different direction and centre by using
border layout manager.
[Link]
import [Link].*;
public class AWTBorderLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT BorderLayout Example");
Button northButton = new Button("North");
Button southButton = new Button("South");
Button eastButton = new Button("East");
Button westButton = new Button("West");
Button centerButton = new Button("Center");
[Link](new BorderLayout());
[Link](northButton, [Link]);
[Link](southButton, [Link]);
[Link](eastButton, [Link]);
[Link](westButton, [Link]);
[Link](centerButton, [Link]);
[Link](400, 300);
[Link](true);
}
}
Output:
Practical No 02
Aim: Implementation of awt to create a dialog box
[Link]
import [Link].*;
public class DialogExample {
private Frame mainFrame;
private Button showButton;
private Dialog dialog;
public DialogExample() {
prepareGUI();
showFileDialog();
}
private void prepareGUI() {
mainFrame = new Frame("Dialog Example");
[Link](400, 400);
[Link](new FlowLayout());
showButton = new Button("Show Dialog");
[Link](showButton);
[Link](true);
}
private void showFileDialog() {
dialog = new Dialog(mainFrame, "Dialog", true);
[Link](300, 200);
[Link](new FlowLayout());
Label label = new Label("This is a dialog box.");
[Link](label);
Button closeButton = new Button("Close");
[Link](closeButton);
[Link](true);
}
public static void main(String[] args) {
new DialogExample();
}
}
Output
Practical No 03
Aim: Implementation of AWT to create menubar.
[Link]
import [Link].*;
public class MenuBarExample {
private Frame mainFrame;
private MenuBar menuBar;
private Menu fileMenu;
private MenuItem openMenuItem;
private MenuItem saveMenuItem;
private MenuItem exitMenuItem;
public MenuBarExample() {
prepareGUI();
createMenuBar();
}
private void prepareGUI() {
mainFrame = new Frame("Menu Bar Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](true);
}
private void createMenuBar() {
menuBar = new MenuBar();
fileMenu = new Menu("File");
openMenuItem = new MenuItem("Open");
saveMenuItem = new MenuItem("Save");
exitMenuItem = new MenuItem("Exit");
[Link](openMenuItem);
[Link](saveMenuItem);
[Link]();
[Link](exitMenuItem);
[Link](fileMenu);
[Link](menuBar);
}
public static void main(String[] args) {
new MenuBarExample();
}
}
Output:
Practical No 04
Aim: Implementation of AWT to create list
[Link]
import [Link].*;
import [Link].*;
public class ListExample {
private Frame mainFrame;
private List list;
public ListExample() {
prepareGUI();
createList();
}
private void prepareGUI() {
mainFrame = new Frame("List Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
[Link](0);
}
});
[Link](true);
}
private void createList() {
list = new List(4, true); // Parameters: visible rows, multiple selection allowed
[Link]("Item 1");
[Link]("Item 2");
[Link]("Item 3");
[Link]("Item 4");
[Link]("Item 5");
[Link](list);
}
public static void main(String[] args) {
new ListExample();
}
}
Output:
Practical No 05
Aim: Implementation of AWT to create choice
[Link]
import [Link].*;
import [Link].*;
public class ChoiceExample {
private Frame mainFrame;
private Choice choice;
public ChoiceExample() {
prepareGUI();
createChoice();
}
private void prepareGUI() {
mainFrame = new Frame("Choice Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
[Link](0);
}
});
[Link](true);
}
private void createChoice() {
choice = new Choice();
[Link]("Option 1");
[Link]("Option 2");
[Link]("Option 3");
[Link]("Option 4");
[Link]("Option 5");
[Link](choice);
}
public static void main(String[] args) {
new ChoiceExample();
}
}
Output:
Practical No 06
Aim: Implementation of AWT to create checkbox
[Link]
import [Link].*;
import [Link].*;
public class CheckboxExample {
private Frame mainFrame;
private Checkbox checkbox1, checkbox2, checkbox3;
public CheckboxExample() {
prepareGUI();
createCheckboxes();
}
private void prepareGUI() {
mainFrame = new Frame("Checkbox Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
[Link](0);
}
});
[Link](true);
}
private void createCheckboxes() {
checkbox1 = new Checkbox("Checkbox 1");
checkbox2 = new Checkbox("Checkbox 2");
checkbox3 = new Checkbox("Checkbox 3");
[Link](checkbox1);
[Link](checkbox2);
[Link](checkbox3);
}
public static void main(String[] args) {
new CheckboxExample();
}
}
Output:
Practical No 07
Aim: Implementation of AWT to create scrollbar
[Link]
import [Link].*;
public class ScrollbarExample {
private Frame mainFrame;
private Scrollbar scrollbar;
public ScrollbarExample() {
prepareGUI();
createScrollbar();
}
private void prepareGUI() {
mainFrame = new Frame("Scrollbar Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](true);
}
private void createScrollbar() {
scrollbar = new Scrollbar();
[Link]([Link]);
[Link](0);
[Link](100);
[Link](10);
[Link](1);
[Link](10);
[Link](scrollbar);
}
public static void main(String[] args) {
new ScrollbarExample();
}
}
Output:
Practical No 08
Aim: Implementation of swing to demonstrate of Radiobutton
import [Link].*;
import [Link].*;
public class RadioButtonExample {
private JFrame mainFrame;
private JPanel panel;
private JRadioButton radioButton1, radioButton2, radioButton3;
public RadioButtonExample() {
prepareGUI();
createRadioButtons();
}
private void prepareGUI() {
mainFrame = new JFrame("RadioButton Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
[Link](new GridLayout(3, 1));
[Link](panel);
[Link](true);
}
private void createRadioButtons() {
radioButton1 = new JRadioButton("Option 1");
radioButton2 = new JRadioButton("Option 2");
radioButton3 = new JRadioButton("Option 3");
ButtonGroup group = new ButtonGroup();
[Link](radioButton1);
[Link](radioButton2);
[Link](radioButton3);
[Link](radioButton1);
[Link](radioButton2);
[Link](radioButton3);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
Output:
Practical No 09
Aim: Implementation of swing to demonstrate of JToggleButton
[Link]
import [Link].*;
import [Link].*;
public class JToggleButtonExample {
private JFrame mainFrame;
private JPanel panel;
private JToggleButton toggleButton1, toggleButton2, toggleButton3;
public JToggleButtonExample() {
prepareGUI();
createToggleButtons();
}
private void prepareGUI() {
mainFrame = new JFrame("JToggleButton Example");
[Link](400, 400);
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
[Link](new GridLayout(3, 1));
[Link](panel);
[Link](true);
}
private void createToggleButtons() {
toggleButton1 = new JToggleButton("Option 1");
toggleButton2 = new JToggleButton("Option 2");
toggleButton3 = new JToggleButton("Option 3");
[Link](toggleButton1);
[Link](toggleButton2);
[Link](toggleButton3);
}
public static void main(String[] args) {
new JToggleButtonExample();
}
}
Output:
Practical No 10
Aim: Implementation of swing to demonstrate of Tabbed panes
[Link]
import [Link].*;
import [Link].*;
public class TabbedPaneExample {
private JFrame mainFrame;
private JTabbedPane tabbedPane;
public TabbedPaneExample() {
prepareGUI();
createTabbedPane();
}
private void prepareGUI() {
mainFrame = new JFrame("TabbedPane Example");
[Link](400, 400);
[Link](new GridLayout(1, 1));
[Link](JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
[Link](tabbedPane);
[Link](true);
}
private void createTabbedPane() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
[Link]("Tab 1", panel1);
[Link]("Tab 2", panel2);
[Link]("Tab 3", panel3);
}
public static void main(String[] args) {
new TabbedPaneExample();
}
}
Output:
Practical No 11
Aim: Implementation of swing to demonstrate of tree.
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
public class TreeDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Tree Demo");
[Link](JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Node 1");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Node 2");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Node 3");
[Link](node1);
[Link](node2);
[Link](node3);
[Link](new DefaultMutableTreeNode("Subnode 1"));
[Link](new DefaultMutableTreeNode("Subnode 2"));
[Link](new DefaultMutableTreeNode("Subnode 3"));
JTree tree = new JTree(root);
JScrollPane treeScrollPane = new JScrollPane(tree);
[Link](treeScrollPane);
[Link](300, 300);
[Link](true);
}
}
Output:
Practical No 12
Aim: Implementation of JDBC to insert record in database.
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
public class InsertRecordExample {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USERNAME = "root";
private static final String PASSWORD = "tiger";
public static void main(String[] args) {
try {
Connection connection = [Link](JDBC_URL, USERNAME,
PASSWORD);
String sql = "INSERT INTO my_table (id, name) VALUES (?, ?)";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, 1); // Assuming id is an integer column and you want to insert 1
[Link](2, "John"); // Assuming name is a string column and you want to
insert "John"
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("A new record was inserted successfully!");
} else {
[Link]("Failed to insert the record!");
}
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
Output:
A new record was inserted successfully!
Process finished with exit code 0
Practical No 13
Aim: Implementation of JDBC to update record of database.
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
public class UpdateRecordExample {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password";
public static void main(String[] args) {
try {
Connection connection = [Link](JDBC_URL, USERNAME,
PASSWORD);
String sql = "UPDATE my_table SET name = ? WHERE id = ?";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, "Sagar"); // Assuming you want to update the name to "New
Name"
[Link](2, 1); // Assuming id is an integer column and you want to update the
record with id 1
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Record updated successfully!");
} else {
[Link]("No records were updated!");
}
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
Output:
Record updated successfully!
Process finished with exit code 0
Practical No 14
Aim: Implementation of JDBC to select record from table and display it.
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SelectRecordExample {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USERNAME = "root";
private static final String PASSWORD = "tiger";
public static void main(String[] args) {
try {
Connection connection = [Link](JDBC_URL, USERNAME,
PASSWORD);
String sql = "SELECT id, name FROM mytable";
PreparedStatement preparedStatement = [Link](sql);
ResultSet resultSet = [Link]();
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
[Link]("ID: " + id + ", Name: " + name);
}
[Link]();
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
Output:
ID: 1, Name: Sagar
ID: 2, Name: Isha
Process finished with exit code 0
Practical No 15
Aim: Implementation of RMI application
[Link]
import [Link];
import [Link];
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
[Link]
import [Link];
import [Link];
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello() throws RemoteException {
return "Hello, world!";
}
}
[Link]
import [Link];
import [Link];
public class Server {
public static void main(String[] args) {
try {
HelloImpl obj = new HelloImpl();
Registry registry = [Link](1099);
[Link]("Hello", obj);
[Link]("Server is running...");
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}
[Link]
import [Link];
import [Link];
public class Client {
public static void main(String[] args) {
try {
Registry registry = [Link]("localhost", 1099);
Hello stub = (Hello) [Link]("Hello");
String response = [Link]();
[Link]("Response from server: " + response);
} catch (Exception e) {
[Link]("Client exception: " + [Link]());
[Link]();
}
}
}
Output:
Server is running...
Response from server: Hello, world!
Practical No 16
Aim: Design a Program to Implement a Socket programming where client will send the request
and server will then respond.
[Link]
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
[Link]("Server is running. Waiting for client...");
Socket clientSocket = [Link]();
[Link]("Client connected.");
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
String request = [Link]();
[Link]("Client request: " + request);
[Link]("Hello, client! This is the server responding to your request: " + request);
[Link]();
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
[Link]
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345);
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
[Link]("Hello, server! How are you?");
String response = [Link]();
[Link]("Server response: " + response);
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
Client side output:
Server response: Hello, client! This is the server responding to your request: Hello, server! How are
you?
Server side output:
Server is running. Waiting for client...
Client connected.
Client request: Hello, server! How are you?
Practical No 17
Aim: Implementation of servlets for Hello World
[Link]
package myPackage;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Hello, World! </h1>");
}
}
Output:
Practical No 18
Aim: Implementation of servlets for doGet method
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Enter Your Name</h2>
<form action="welcome" method="get">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
[Link]:
package myPackage;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WelcomeServlet() {
super();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
// Retrieve the 'name' parameter from the request
String name = [Link]("name");
if (name == null || [Link]().isEmpty()) {
name = "Guest"; // Default if no name is provided
}
[Link]("<html><head><title>Welcome</title></head><body>");
[Link]("<h1>Welcome, " + name + "!</h1>");
[Link]("</body></html>");
}
Output:
[Link]:
[Link]
Practical No. 19
Aim: Implementation of servlets for doPost method
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Enter Your Date of Birth</h2>
<form action="calculateAge" method="post">
<label for="dob">Date of Birth:</label>
<input type="datetime-local" id="dob" name="dob" required>
<button type="submit">Calculate Age</button>
</form>
</body>
</html>
[Link]:
package myPackage;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/calculateAge")
public class AgeCalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
try {
// Get DOB from request
String dobInput = [Link]("dob");
if (dobInput == null || [Link]()) {
[Link]("<h1>Error: Please provide a valid date of birth.</h1>");
return;
}
// Parse input into LocalDateTime
DateTimeFormatter formatter = [Link]("yyyy-MM-dd'T'HH:mm");
LocalDateTime dob = [Link](dobInput, formatter);
// Get current time
LocalDateTime now = [Link]([Link]());
// Calculate differences
Duration duration = [Link](dob, now);
long totalSeconds = [Link]();
long years = totalSeconds / (365 * 24 * 3600);
long remainingSeconds = totalSeconds % (365 * 24 * 3600);
long months = remainingSeconds / (30 * 24 * 3600);
remainingSeconds %= (30 * 24 * 3600);
long days = remainingSeconds / (24 * 3600);
remainingSeconds %= (24 * 3600);
long hours = remainingSeconds / 3600;
remainingSeconds %= 3600;
long minutes = remainingSeconds / 60;
long seconds = remainingSeconds % 60;
long microseconds = [Link]() / 1000; // Convert nanoseconds to microseconds
// Display result
[Link]("<html><head><title>Age Result</title></head><body>");
[Link]("<h1>Age Calculation Result</h1>");
[Link]("<p><strong>Total Years:</strong> " + years + "</p>");
[Link]("<p><strong>Total Months:</strong> " + months + "</p>");
[Link]("<p><strong>Total Days:</strong> " + days + "</p>");
[Link]("<p><strong>Total Hours:</strong> " + hours + "</p>");
[Link]("<p><strong>Total Minutes:</strong> " + minutes + "</p>");
[Link]("<p><strong>Total Seconds:</strong> " + seconds + "</p>");
[Link]("<p><strong>Total Microseconds:</strong> " + microseconds + "</p>");
[Link]("</body></html>");
} catch (Exception e) {
[Link]("<h1>Error processing request: " + [Link]() + "</h1>");
}
}
}
Output:
[Link]:
[Link]:
Practical No. 20
Aim: Implementation of JSP to create User ID and Password
[Link]:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Create User ID & Password</h2>
<form action="[Link]" method="post">
<label for="userId">User ID:</label>
<input type="text" id="userId" name="userId" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
[Link]:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Registration Successful!</h2>
<%
String userId = [Link]("userId");
String password = [Link]("password");
if (userId == null || password == null || [Link]() || [Link]()) {
[Link]("<p>Error: User ID and Password are required!</p>");
} else {
[Link]("userId", userId);
[Link]("password", password);
[Link]("<p><strong>User ID:</strong> " + userId + "</p>");
[Link]("<p>Your password is stored securely (not displayed here).</p>");
}
%>
</body>
</html>
Output:
[Link]
[Link]