169_java_Practical[1]
169_java_Practical[1]
Practical: 1
Aim: Set up Java Programming development environment by using
i. Command Prompt
ii. Any IDE like Eclipse, Notepad++, JCreater etc.
1. And Test Java Programming development environment by implementing a small
program.
Source Code:
public class HelloWord {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output:
Name: Yash Roshan Sonawane Subject: Java Programming Lab
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 2
Aim: Implementing the Operations of stack and queue using package and
interface.
Practical: 3
Aim: Write a program to implement an object oriented system and multithreaded processes
as per needs and specifications.
Source Code:
class Task implements Runnable {
private final String name;
public Task(String name) {
this.name = name;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " is executing " + name);
try {
Thread.sleep(1000); // Simulate task duration
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public class Main {
public static void main(String[] args) {
// Create and start multiple threads (multithreading)
for (int i = 1; i <= 5; i++) {
Thread thread = new Thread(new Task("Task-" + i), "Worker-" + i);
thread.start();
}
}
}
Output:
Name: Yash Roshan Sonawane Subject: Java Programming Lab
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 4
Aim: Write a program to implement student information in a file and perform the operations
on it.
Source Code:
import java.io.*;
import java.util.*;
class Student {
private String rollNo;
private String name;
private int age;
@Override
public String toString() {
return "Roll No: " + rollNo + ", Name: " + name + ", Age: " + age;
}
}
if (!found) {
System.out.println("No valid student records to display.");
}
}
int choice;
try {
choice = Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
continue;
}
switch (choice) {
case 1:
System.out.print("Enter Roll No: ");
String roll = sc.nextLine();
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Age: ");
int age;
try {
age = Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid age input.");
break;
}
addStudent(new Student(roll, name, age));
System.out.println("Student added successfully.");
break;
case 2:
showAll();
break;
case 3:
System.out.print("Enter Roll No to search: ");
String search = sc.nextLine();
searchStudent(search);
break;
case 4:
System.out.println("Exiting the program. Goodbye!");
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}
Practical: 5
Aim: Working with shape motion by Applet programming.
Source Code:
Practical5.java :
import java.applet.Applet;
import java.awt.*;
public class Practical5 extends Applet implements Runnable {
int x = 0;
Thread t;
Image bufferImage;
Graphics bufferGraphics;
bufferGraphics.setColor(Color.BLUE);
bufferGraphics.fillOval(x, 225, 150, 150); // Bigger circle, adjusted Y
g.drawImage(bufferImage, 0, 0, this);
}
public void stop() {
t = null;
}
}
Practical5.html :
<html>
<body>
<applet code="Practical5.class" width="400" height="200">
</applet>
</body>
</html>
Cmd :
appletviewer Practical5.html
Output:
Name: Yash Roshan Sonawane Subject: Java Programming Lab
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 6
Aim: Write a program to design Registration process form using Applet and AWT
components.
Source Code:
Practical6.java :
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Practical6 extends Applet implements ActionListener {
Label nameLabel, genderLabel, courseLabel, outputLabel;
TextField nameField;
CheckboxGroup genderGroup;
Checkbox male, female;
Choice courseChoice;
Button submitButton;
Practical6.html :
<html>
<body>
<applet code="Practical6" width="500" height="500"></applet>
</body>
</html>
Cmd :
appletviewer Practical6.html
OutPut:
Name: Yash Roshan Sonawane Subject: Java Programming
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 7
Aim : Write a program to connect to any database and to execute the SQL query operation on
command prompt.
MySQL :
CREATE DATABASE testdb;
USE testdb;
insert into students value(14, "Rutuja", 22), (66, "Pranali", 21), (69, "Yash", 21);
DBQueryExample.java :
import java.sql.*;
public class DBQueryExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "javauser";
String password = "javapass";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to database!");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
CMD :
E:
cd JavaProgram
javac -cp .;mysql-connector-j-9.2.0.jar DBQueryExample.java
java -cp .;mysql-connector-j-9.2.0.jar DBQueryExample
Name: Yash Roshan Sonawane Subject: Java Programming
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 8
Aim : Write a program to connect to any database and to execute the SQL query operation using
GUI Interface
MySQL :
CREATE DATABASE testdb;
USE testdb;
insert into students value(14, "Rutuja", 22), (66, "Pranali", 21), (69, "Yash", 21);
DatabaseGUI.java :
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class DatabaseGUI extends JFrame {
private JTable table;
private JButton loadButton;
public DatabaseGUI() {
setTitle("Database Query GUI");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(loadButton, BorderLayout.NORTH);
add(new JScrollPane(table), BorderLayout.CENTER);
CMD :
javac -cp .;mysql-connector-j-9.2.0.jar DatabaseGUI.java
java -cp .;mysql-connector-j-9.2.0.jar DatabaseGUI
Name: Yash Roshan Sonawane Subject: Java Programming
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 9
Aim : Write a program to demonstrate socket programming. E.g. send hello world to server from
client.
Server.java :
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
int port = 1234;
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java :
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
String host = "localhost";
int port = 1234;
try (Socket socket = new Socket(host, port)) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hello, World");
System.out.println("Message sent to server.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
CMD :
java Server
New cmd :
java Client
Name: Yash Roshan Sonawane Subject: Java Programming Lab
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 10
Aim: Write a Servlet code to demonstrate GET and POST methods with suitable example.
Source Code:
Practical10 :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Hello from GET method!</h2>");
out.println("<form method='POST'>");
out.println("Name: <input type='text' name='username'/>");
out.println("<input type='submit' value='Send via POST'/>");
out.println("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String name = request.getParameter("username");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Hello, " + name + " (from POST method)!</h2>");
}
}
Folder Structure in Tomcat
apache-tomcat-9.0.102
└── webapps
└── MyApp
├── Practical10.java (compiled separately)
└── WEB-INF
├── web.xml
└── classes
└── Practical10.class
Web.xml :
Located at: webapps/MyApp/WEB-INF/web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Practical10</servlet-name>
<servlet-class>Practical10</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Practical10</servlet-name>
<url-pattern>/prac10</url-pattern>
</servlet-mapping>
</web-app>
Compile Servlet
Open CMD and navigate to where your .java file is saved, then run:
javac -cp "C:\apache-tomcat-9.0.102\lib\servlet-api.jar" Practical10.java
Output :
Name: Yash Roshan Sonawane Subject: Java Programming Lab
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 11
Aim: Write a program to demonstrate the use of JSP.
Source Code:
apache-tomcat-9.0.102
└── webapps
└── MyJspApp
├── index.jsp
└── greet.jsp
Index.jsp :
<html>
<head>
<title>JSP Greeting Form</title>
</head>
<body>
<h2>Enter Your Name</h2>
<form action="greet.jsp" method="post">
Name: <input type="text" name="username" />
<input type="submit" value="Greet Me" />
</form>
</body>
</html>
Greet.jsp :
<%
String name = request.getParameter("username");
%>
<html>
<head>
<title>Greeting Result</title>
</head>
<body>
<h2>Hello, <%= name %>! Welcome to JSP.</h2>
</body>
</html>
Cmd :
http://localhost:8080/MyJspApp/index.jsp
Output :
Name: Yash Roshan Sonawane Subject: Java Programming Lab
Roll No: B-169 Subject-In-Charge: Prof. Monika Shinde
Practical: 12
Aim: Write a java program to print a pyramid number pattern as follows:
0
101
21012
3210123
432101234
54321012345
6543210123456
765432101234567
87654321012345678
9876543210123456789
Source code :
public class Practical12{
System.out.print(" ");
System.out.println();
}
}
Output :