JAVA PYQ Programs PDF
JAVA PYQ Programs PDF
1)Write a Java program to accept 'N' student names from the user, store them in a LinkedList collection, and
display in reverse order
import java.util.*;
Collections.reverse(students);
sc.close();
}
2)Write a Java program to accept details of a student (rollno, name, percentage), store them into a database &
display it. (Note: Assume JDBC setup is already done)
try {
Class.forName("org.postgresql.Driver");
"ty90", // Username
);
String query = "INSERT INTO students (rollno, name, percentage) VALUES (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.executeUpdate();
ResultSet rs = stmt.executeQuery(selectQuery);
// Print header
while (rs.next()) {
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
sc.close();
}
}
3)Write a JSP program to accept username & password, if username & password are the same then display "Login
successful" message on the browser otherwise display "Login failed" message.
<html>
<head>
<title>Login Page</title>
</head>
<body>
<!-- Simple form to take username and password input from the user -->
<form method="POST">
</form>
<%
// Check if both username and password are not null (i.e., form is submitted)
} else {
%>
</body>
</html>
4)Write a Java program to delete the details of a given teacher & display remaining records from Teacher Table.
Assume teacher table (tid, tname, subject) is already created.
import java.sql.*;
import java.util.Scanner;
try {
Class.forName("org.postgresql.Driver");
"ty90", // username
);
PreparedStatement ps = conn.prepareStatement(deleteQuery);
ps.executeUpdate();
while (rs.next()) {
rs.close();
stmt.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
5)Write a jdbc program to accept details of student (RN, Name, percentage) from user. Display that details.
import java.sql.*;
import java.util.Scanner;
try {
Class.forName("org.postgresql.Driver");
"jdbc:postgresql://192.168.1.21:5432/ty90", // DB URL
"ty90", // username
);
String query = "INSERT INTO student (rollNo, name, percentage) VALUES (?, ?, ?)";
pstmt.setString(1, rollNo);
pstmt.setString(2, name);
pstmt.setFloat(3, percentage);
if (rowsAffected > 0) {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
6)Write a java program in multithreading to display all the numbers between 1 to 10. Each number should display
after 2 seconds.
// Override the run() method which contains the code to be executed by the thread
try {
Thread.sleep(2000);
System.out.println(i);
} catch (InterruptedException e) {
e.printStackTrace();
// Start the thread - this will call the run() method in a separate thread
t.start();
7)Write a JSP script to check the given number is prime or not. Display the result in blue color.
<!-- This directive tells the server that the page uses Java language and sets character encoding -->
<!DOCTYPE html>
<html>
<head>
<!-- Sets the title of the web page shown in the browser tab -->
</head>
<body>
<form method="post">
<!-- This form sends data to the same JSP page using POST method -->
</form>
<%
// Get the value entered in the input field "num" from the request and store it in numStr
// Check if user has entered something (not null and not empty)
// Loop from 2 to num/2 to check if the number is divisible by any number in between
if (num % i == 0) {
isPrime = false;
// Set the flag to false
break;
} else {
%>
</body>
</html>
8)Write a Servlet program to get information about the server such as name, port number, and version of the
server
response.setContentType("text/html");
// Set the response content type to HTML so the browser understands the output
out.println("<html><body>");
out.println("<h1>Server Information</h1>");
out.println("</body></html>");
9)Write a Java program to accept 'n' names from the user, store them into ArrayList, sort them in ascending order,
and display them.
import java.util.*; // Import utility package for Scanner, ArrayList, and Collections
// Main class
// Ask the user for the number of names they want to enter
Collections.sort(names);
10)Write a Java program to accept N integers from the user, store them in a suitable collection, and display only
even integers.
import java.util.*;
// Main class
numbers.add(scanner.nextInt());
System.out.println("Even integers:");
System.out.println(num);
11)Write a Java program to accept details of a teacher (Tid, Tname, Tsubject), store it in the database, and display
it.
import java.sql.*;
import java.util.Scanner;
try {
Class.forName("org.postgresql.Driver");
"jdbc:postgresql://192.168.1.21:5432/ty90", // DB URL
"ty90", // Username
);
String query = "INSERT INTO teacher (Tid, Tname, Tsubject) VALUES (?, ?, ?)";
stmt.executeUpdate();
ResultSet rs = selectStmt.executeQuery(selectQuery);
// Loop through the result set and print each teacher's details
while (rs.next()) {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
12)Write a JSP program to accept the user's name and greet the user according to the time of day
pageEncoding="ISO-8859-1"%> <%-- JSP directive to define page language and encoding --%>
<html>
<head>
</head>
<body>
<h2>Enter your name:</h2> <%-- Heading prompting user to enter their name --%>
<form method="post"> <%-- Form using POST method to send data --%>
<input type="text" name="username" /> <%-- Text field to enter username --%>
</form>
<%
String username = request.getParameter("username"); // Get the value of the username entered by user
if (username != null) { // Check if the form was submitted (i.e., username is not null)
%>
</body>
</html>
13)Write a Java program to update the salary of a given employee (use prepared statement interface).
import java.sql.*; // Import all classes from java.sql package for database operations
try {
Class.forName("org.postgresql.Driver");
"ty90", // Username
);
// Execute the update query and get the number of affected rows
if (rowsUpdated > 0) {
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}