Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Slip 1
- ==========
- Question 1: Write a Java program using Multithreading to display all the alphabets between 'A' to 'Z' after every 2 seconds.
- --------------------------------------------------------------------------------
- public class AlphabetThread extends Thread {
- public void run() {
- for (char ch = 'A'; ch <= 'Z'; ch++) {
- System.out.println(ch);
- try {
- Thread.sleep(2000); // Sleep for 2 seconds
- } catch (InterruptedException e) {
- System.out.println("Thread interrupted.");
- }
- }
- }
- public static void main(String[] args) {
- AlphabetThread thread = new AlphabetThread();
- thread.start();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a Java program to accept the details of Employee (Eno, EName, Designation, Salary) from a user and store it into the database. (Use Swing)
- --------------------------------------------------------------------------------
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- public class EmployeeForm extends JFrame {
- private JTextField enoField, enameField, desgField, salaryField;
- private JButton submitButton;
- public EmployeeForm() {
- setTitle("Employee Details");
- setLayout(new GridLayout(5, 2));
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- add(new JLabel("Employee No:"));
- enoField = new JTextField();
- add(enoField);
- add(new JLabel("Employee Name:"));
- enameField = new JTextField();
- add(enameField);
- add(new JLabel("Designation:"));
- desgField = new JTextField();
- add(desgField);
- add(new JLabel("Salary:"));
- salaryField = new JTextField();
- add(salaryField);
- submitButton = new JButton("Submit");
- add(submitButton);
- submitButton.addActionListener(e -> saveToDatabase());
- pack();
- setVisible(true);
- }
- private void saveToDatabase() {
- String eno = enoField.getText();
- String ename = enameField.getText();
- String desg = desgField.getText();
- String salary = salaryField.getText();
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- String query = "INSERT INTO Employee (Eno, EName, Designation, Salary) VALUES (?, ?, ?, ?)";
- PreparedStatement ps = con.prepareStatement(query);
- ps.setString(1, eno);
- ps.setString(2, ename);
- ps.setString(3, desg);
- ps.setString(4, salary);
- ps.executeUpdate();
- JOptionPane.showMessageDialog(this, "Employee details saved successfully!");
- con.close();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
- }
- }
- public static void main(String[] args) {
- new EmployeeForm();
- }
- }
- --------------------------------------------------------------------------------
- Slip 2
- ==========
- Question 1: Write a Java program to accept a string from the user and count the number of vowels in it.
- --------------------------------------------------------------------------------
- import java.util.Scanner;
- public class VowelCounter {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter a string: ");
- String str = sc.nextLine().toLowerCase();
- int vowelCount = 0;
- for (char ch : str.toCharArray()) {
- if ("aeiou".indexOf(ch) != -1) {
- vowelCount++;
- }
- }
- System.out.println("Number of vowels: " + vowelCount);
- sc.close();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a JSP program to accept two numbers from the user and display their sum.
- --------------------------------------------------------------------------------
- <html>
- <body>
- <h2>Sum of Two Numbers</h2>
- <form method="post">
- Enter First Number: <input type="text" name="num1"><br>
- Enter Second Number: <input type="text" name="num2"><br>
- <input type="submit" value="Calculate">
- </form>
- <%
- String num1Str = request.getParameter("num1");
- String num2Str = request.getParameter("num2");
- if (num1Str != null && num2Str != null) {
- int num1 = Integer.parseInt(num1Str);
- int num2 = Integer.parseInt(num2Str);
- int sum = num1 + num2;
- %>
- <p>Sum: <%= sum %></p>
- <% } %>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Slip 3
- ==========
- Question 1: Write a JSP program to display the details of Patient (PNo, PName, Address, Age, Disease) in tabular form on browser.
- --------------------------------------------------------------------------------
- <%@ page import="java.sql.*" %>
- <html>
- <body>
- <h2>Patient Details</h2>
- <table border="1">
- <tr>
- <th>PNo</th>
- <th>PName</th>
- <th>Address</th>
- <th>Age</th>
- <th>Disease</th>
- </tr>
- <%
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM Patient");
- while (rs.next()) {
- %>
- <tr>
- <td><%= rs.getString("PNo") %></td>
- <td><%= rs.getString("PName") %></td>
- <td><%= rs.getString("Address") %></td>
- <td><%= rs.getInt("Age") %></td>
- <td><%= rs.getString("Disease") %></td>
- </tr>
- <%
- }
- con.close();
- } catch (Exception e) {
- out.println("Error: " + e.getMessage());
- }
- %>
- </table>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Question 2: Write a Java program to create LinkedList of String objects and perform the following...
- --------------------------------------------------------------------------------
- import java.util.LinkedList;
- import java.util.Collections;
- public class LinkedListDemo {
- public static void main(String[] args) {
- LinkedList<String> list = new LinkedList<>();
- // Adding elements
- list.add("Apple");
- list.add("Banana");
- list.add("Cherry");
- // i. Add element at the end
- list.addLast("Dragonfruit");
- System.out.println("After adding at end: " + list);
- // ii. Delete first element
- list.removeFirst();
- System.out.println("After deleting first element: " + list);
- // iii. Display in reverse order
- Collections.reverse(list);
- System.out.println("In reverse order: " + list);
- }
- }
- --------------------------------------------------------------------------------
- Slip 8
- ==========
- Question 1: Write a Java program to define a thread for printing text on output screen...
- --------------------------------------------------------------------------------
- public class TextThread extends Thread {
- private String text;
- private int times;
- public TextThread(String text, int times) {
- this.text = text;
- this.times = times;
- }
- public void run() {
- for (int i = 0; i < times; i++) {
- System.out.println(text);
- try {
- Thread.sleep(500); // Small delay for visibility
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- TextThread t1 = new TextThread("COVID19", 10);
- TextThread t2 = new TextThread("LOCKDOWN2020", 20);
- TextThread t3 = new TextThread("VACCINATED2021", 30);
- t1.start();
- t2.start();
- t3.start();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a JSP program to check whether a given number is prime or not...
- --------------------------------------------------------------------------------
- <html>
- <body>
- <h2>Prime Number Check</h2>
- <form method="post">
- Enter Number: <input type="text" name="num">
- <input type="submit" value="Check">
- </form>
- <%
- String numStr = request.getParameter("num");
- if (numStr != null) {
- int num = Integer.parseInt(numStr);
- boolean isPrime = true;
- if (num <= 1) isPrime = false;
- for (int i = 2; i <= Math.sqrt(num); i++) {
- if (num % i == 0) {
- isPrime = false;
- break;
- }
- }
- %>
- <p style="color:red;">
- <%
- if (isPrime) {
- out.println(num + " is a Prime Number");
- } else {
- out.println(num + " is not a Prime Number");
- }
- %>
- </p>
- <% } %>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Slip 12
- ===========
- Question 1: Write a Java program to create an ArrayList of integers and find the maximum and minimum values.
- --------------------------------------------------------------------------------
- import java.util.ArrayList;
- import java.util.Collections;
- public class ArrayListMinMax {
- public static void main(String[] args) {
- ArrayList<Integer> numbers = new ArrayList<>();
- numbers.add(45);
- numbers.add(12);
- numbers.add(78);
- numbers.add(23);
- numbers.add(56);
- int max = Collections.max(numbers);
- int min = Collections.min(numbers);
- System.out.println("Maximum value: " + max);
- System.out.println("Minimum value: " + min);
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a Servlet program to accept a number from the user and check if it’s even or odd.
- --------------------------------------------------------------------------------
- <!-- evenodd.html -->
- <html>
- <body>
- <h2>Check Even or Odd</h2>
- <form action="EvenOddServlet" method="post">
- Enter Number: <input type="text" name="number"><br>
- <input type="submit" value="Check">
- </form>
- </body>
- </html>
- <!-- EvenOddServlet.java -->
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class EvenOddServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String numStr = request.getParameter("number");
- int num = Integer.parseInt(numStr);
- out.println("<html><body>");
- out.println("<h2>Number Check</h2>");
- if (num % 2 == 0) {
- out.println("<p>" + num + " is Even</p>");
- } else {
- out.println("<p>" + num + " is Odd</p>");
- }
- out.println("</body></html>");
- }
- }
- --------------------------------------------------------------------------------
- Slip 15
- ===========
- Question 1: Write a Java program to display name and priority of a Thread.
- --------------------------------------------------------------------------------
- public class ThreadInfo {
- public static void main(String[] args) {
- Thread t1 = new Thread("Thread-1");
- Thread t2 = new Thread("Thread-2");
- t1.setPriority(Thread.MIN_PRIORITY); // 1
- t2.setPriority(Thread.MAX_PRIORITY); // 10
- System.out.println("Thread Name: " + t1.getName() + ", Priority: " + t1.getPriority());
- System.out.println("Thread Name: " + t2.getName() + ", Priority: " + t2.getPriority());
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a SERVLET program which counts how many times a user has visited a web page...
- --------------------------------------------------------------------------------
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class VisitCounterServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- Cookie[] cookies = request.getCookies();
- int visitCount = 0;
- if (cookies != null) {
- for (Cookie cookie : cookies) {
- if (cookie.getName().equals("visitCount")) {
- visitCount = Integer.parseInt(cookie.getValue());
- }
- }
- }
- visitCount++;
- Cookie visitCookie = new Cookie("visitCount", String.valueOf(visitCount));
- response.addCookie(visitCookie);
- out.println("<html><body>");
- if (visitCount == 1) {
- out.println("<h2>Welcome! This is your first visit.</h2>");
- } else {
- out.println("<h2>You have visited this page " + visitCount + " times.</h2>");
- }
- out.println("</body></html>");
- }
- }
- --------------------------------------------------------------------------------
- Slip 16
- ===========
- Question 1: Write a Java program to create a TreeSet, add some colors...
- --------------------------------------------------------------------------------
- import java.util.TreeSet;
- public class TreeSetDemo {
- public static void main(String[] args) {
- TreeSet<String> colors = new TreeSet<>();
- colors.add("Red");
- colors.add("Blue");
- colors.add("Green");
- colors.add("Yellow");
- System.out.println("Colors in ascending order: " + colors);
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a Java program to accept the details of Teacher...
- --------------------------------------------------------------------------------
- import java.sql.*;
- public class TeacherDetails {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- Statement stmt = con.createStatement();
- stmt.executeUpdate("CREATE TABLE Teacher (TNo INT, TName VARCHAR(50), Subject VARCHAR(50))");
- PreparedStatement ps = con.prepareStatement("INSERT INTO Teacher VALUES (?, ?, ?)");
- ps.setInt(1, 1); ps.setString(2, "John"); ps.setString(3, "JAVA"); ps.executeUpdate();
- ps.setInt(1, 2); ps.setString(2, "Alice"); ps.setString(3, "Python"); ps.executeUpdate();
- ps.setInt(1, 3); ps.setString(2, "Bob"); ps.setString(3, "JAVA"); ps.executeUpdate();
- ps.setInt(1, 4); ps.setString(2, "Emma"); ps.setString(3, "C++"); ps.executeUpdate();
- ps.setInt(1, 5); ps.setString(2, "Mike"); ps.setString(3, "JAVA"); ps.executeUpdate();
- ps = con.prepareStatement("SELECT * FROM Teacher WHERE Subject = ?");
- ps.setString(1, "JAVA");
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- System.out.println(rs.getInt("TNo") + " " + rs.getString("TName") + " " + rs.getString("Subject"));
- }
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- --------------------------------------------------------------------------------
- Slip 18
- ===========
- Question 1: Write a Java program using Multithreading to display all the vowels from a given String...
- --------------------------------------------------------------------------------
- import java.util.Scanner;
- public class VowelThread extends Thread {
- private String input;
- public VowelThread(String input) {
- this.input = input;
- }
- public void run() {
- for (char ch : input.toLowerCase().toCharArray()) {
- if ("aeiou".indexOf(ch) != -1) {
- System.out.println(ch);
- try {
- Thread.sleep(3000); // 3 seconds delay
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter a string: ");
- String str = sc.nextLine();
- VowelThread thread = new VowelThread(str);
- thread.start();
- sc.close();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a SERVLET program in Java to accept details of student...
- --------------------------------------------------------------------------------
- <!-- student.html -->
- <html>
- <body>
- <h2>Enter Student Details</h2>
- <form action="StudentServlet" method="post">
- Seat No: <input type="text" name="seatNo"><br>
- Name: <input type="text" name="studName"><br>
- Class: <input type="text" name="className"><br>
- Total Marks: <input type="text" name="totalMarks"><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- <!-- StudentServlet.java -->
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class StudentServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String seatNo = request.getParameter("seatNo");
- String studName = request.getParameter("studName");
- String className = request.getParameter("className");
- double totalMarks = Double.parseDouble(request.getParameter("totalMarks"));
- double percentage = (totalMarks / 500.0) * 100; // Assuming 500 is max marks
- String grade = (percentage >= 80) ? "A" : (percentage >= 60) ? "B" : (percentage >= 40) ? "C" : "D";
- out.println("<html><body>");
- out.println("<h2>Student Details</h2>");
- out.println("Seat No: " + seatNo + "<br>");
- out.println("Name: " + studName + "<br>");
- out.println("Class: " + className + "<br>");
- out.println("Total Marks: " + totalMarks + "<br>");
- out.println("Percentage: " + percentage + "%<br>");
- out.println("Grade: " + grade);
- out.println("</body></html>");
- }
- }
- --------------------------------------------------------------------------------
- Slip 19
- ===========
- Question 1: Write a Java program to accept ‘N’ Integers from a user store them into LinkedList...
- --------------------------------------------------------------------------------
- import java.util.LinkedList;
- import java.util.Scanner;
- public class NegativeIntegers {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- LinkedList<Integer> numbers = new LinkedList<>();
- System.out.print("Enter the number of integers (N): ");
- int n = sc.nextInt();
- System.out.println("Enter " + n + " integers:");
- for (int i = 0; i < n; i++) {
- numbers.add(sc.nextInt());
- }
- System.out.println("Negative integers:");
- for (int num : numbers) {
- if (num < 0) {
- System.out.println(num);
- }
- }
- sc.close();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a Java program using SERVLET to accept username and password...
- --------------------------------------------------------------------------------
- <!-- login.html -->
- <html>
- <body>
- <h2>Login</h2>
- <form action="LoginServlet" method="post">
- Username: <input type="text" name="username"><br>
- Password: <input type="password" name="password"><br>
- <input type="submit" value="Login">
- </form>
- </body>
- </html>
- <!-- LoginServlet.java -->
- import java.io.*;
- import java.sql.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class LoginServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
- ps.setString(1, username);
- ps.setString(2, password);
- ResultSet rs = ps.executeQuery();
- out.println("<html><body>");
- if (rs.next()) {
- out.println("<h2>Login Successful!</h2>");
- } else {
- out.println("<h2>Error: Invalid username or password</h2>");
- }
- out.println("</body></html>");
- con.close();
- } catch (Exception e) {
- out.println("Error: " + e.getMessage());
- }
- }
- }
- --------------------------------------------------------------------------------
- Slip 22
- ===========
- Question 1: Write a Menu Driven program in Java for the following...
- --------------------------------------------------------------------------------
- import java.sql.*;
- import java.util.Scanner;
- public class EmployeeMenu {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- Statement stmt = con.createStatement();
- stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Employee (ENo INT, EName VARCHAR(50), Salary DOUBLE)");
- while (true) {
- System.out.println("\n1. Insert\n2. Update\n3. Display\n4. Exit");
- System.out.print("Enter choice: ");
- int choice = sc.nextInt();
- switch (choice) {
- case 1: // Insert
- System.out.print("Enter ENo: ");
- int eno = sc.nextInt();
- System.out.print("Enter EName: ");
- String ename = sc.next();
- System.out.print("Enter Salary: ");
- double salary = sc.nextDouble();
- PreparedStatement ps = con.prepareStatement("INSERT INTO Employee VALUES (?, ?, ?)");
- ps.setInt(1, eno);
- ps.setString(2, ename);
- ps.setDouble(3, salary);
- ps.executeUpdate();
- System.out.println("Record inserted.");
- break;
- case 2: // Update
- System.out.print("Enter ENo to update: ");
- eno = sc.nextInt();
- System.out.print("Enter new Salary: ");
- salary = sc.nextDouble();
- ps = con.prepareStatement("UPDATE Employee SET Salary = ? WHERE ENo = ?");
- ps.setDouble(1, salary);
- ps.setInt(2, eno);
- ps.executeUpdate();
- System.out.println("Record updated.");
- break;
- case 3: // Display
- ResultSet rs = stmt.executeQuery("SELECT * FROM Employee");
- while (rs.next()) {
- System.out.println(rs.getInt("ENo") + " " + rs.getString("EName") + " " + rs.getDouble("Salary"));
- }
- break;
- case 4: // Exit
- con.close();
- sc.close();
- System.exit(0);
- default:
- System.out.println("Invalid choice!");
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a JSP program which accepts UserName in a TextField and greets the user...
- --------------------------------------------------------------------------------
- <%@ page import="java.util.Calendar" %>
- <html>
- <body>
- <h2>Greeting</h2>
- <form method="post">
- Enter Username: <input type="text" name="username">
- <input type="submit" value="Greet">
- </form>
- <%
- String username = request.getParameter("username");
- if (username != null) {
- Calendar cal = Calendar.getInstance();
- int hour = cal.get(Calendar.HOUR_OF_DAY);
- String greeting;
- if (hour < 12) {
- greeting = "Good Morning";
- } else if (hour < 17) {
- greeting = "Good Afternoon";
- } else {
- greeting = "Good Evening";
- }
- %>
- <p><%= greeting %>, <%= username %>!</p>
- <% } %>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Slip 23
- ===========
- Question 1: Write a Java program using Multithreading to accept a String from a user...
- --------------------------------------------------------------------------------
- import java.util.Scanner;
- public class VowelDisplayThread extends Thread {
- private String input;
- public VowelDisplayThread(String input) {
- this.input = input;
- }
- public void run() {
- for (char ch : input.toLowerCase().toCharArray()) {
- if ("aeiou".indexOf(ch) != -1) {
- System.out.println(ch);
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter a string: ");
- String str = sc.nextLine();
- VowelDisplayThread thread = new VowelDisplayThread(str);
- thread.start();
- sc.close();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a Java program to accept 'N' student names through command line...
- --------------------------------------------------------------------------------
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.ListIterator;
- public class StudentNames {
- public static void main(String[] args) {
- if (args.length == 0) {
- System.out.println("Please provide student names as command-line arguments.");
- return;
- }
- ArrayList<String> students = new ArrayList<>();
- for (String name : args) {
- students.add(name);
- }
- System.out.println("Using Iterator:");
- Iterator<String> iterator = students.iterator();
- while (iterator.hasNext()) {
- System.out.println(iterator.next());
- }
- System.out.println("\nUsing ListIterator (reverse order):");
- ListIterator<String> listIterator = students.listIterator(students.size());
- while (listIterator.hasPrevious()) {
- System.out.println(listIterator.previous());
- }
- }
- }
- --------------------------------------------------------------------------------
- Slip 25
- ===========
- Question 1: Write a Java program using multithreading to print even numbers from 1 to 20 with a 1-second delay.
- --------------------------------------------------------------------------------
- public class EvenNumberThread extends Thread {
- public void run() {
- for (int i = 1; i <= 20; i++) {
- if (i % 2 == 0) {
- System.out.println(i);
- try {
- Thread.sleep(1000); // 1-second delay
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- EvenNumberThread thread = new EvenNumberThread();
- thread.start();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a JSP program to display the current date and time.
- --------------------------------------------------------------------------------
- <%@ page import="java.util.Date" %>
- <html>
- <body>
- <h2>Current Date and Time</h2>
- <p>Current Date and Time: <%= new Date() %></p>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Slip 26
- ===========
- Question 1: Write a Java program to delete the details of given employee...
- --------------------------------------------------------------------------------
- import java.sql.*;
- import java.util.Scanner;
- public class DeleteEmployee {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter Employee ID to delete: ");
- int eno = sc.nextInt();
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- PreparedStatement ps = con.prepareStatement("DELETE FROM Employee WHERE ENo = ?");
- ps.setInt(1, eno);
- int rows = ps.executeUpdate();
- if (rows > 0) {
- System.out.println("Employee with ENo " + eno + " deleted successfully.");
- } else {
- System.out.println("No employee found with ENo " + eno);
- }
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- sc.close();
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a JSP program to calculate sum of first and last digit of a given number...
- --------------------------------------------------------------------------------
- <html>
- <body>
- <h2>Sum of First and Last Digit</h2>
- <form method="post">
- Enter Number: <input type="text" name="num">
- <input type="submit" value="Calculate">
- </form>
- <%
- String numStr = request.getParameter("num");
- if (numStr != null) {
- int num = Integer.parseInt(numStr);
- int firstDigit = num;
- while (firstDigit >= 10) {
- firstDigit /= 10;
- }
- int lastDigit = num % 10;
- int sum = firstDigit + lastDigit;
- %>
- <p style="color:red; font-size:18px;">Sum of first and last digit: <%= sum %></p>
- <% } %>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Slip 28
- ===========
- Question 1: Write a JSP script to accept a String from a user and display it in reverse order.
- --------------------------------------------------------------------------------
- <html>
- <body>
- <h2>Reverse String</h2>
- <form method="post">
- Enter String: <input type="text" name="str">
- <input type="submit" value="Reverse">
- </form>
- <%
- String str = request.getParameter("str");
- if (str != null) {
- StringBuilder reversed = new StringBuilder(str).reverse();
- %>
- <p>Reversed String: <%= reversed %></p>
- <% } %>
- </body>
- </html>
- --------------------------------------------------------------------------------
- Question 2: Write a Java program to display name of currently executing Thread in multithreading.
- --------------------------------------------------------------------------------
- public class CurrentThreadDemo extends Thread {
- public CurrentThreadDemo(String name) {
- super(name);
- }
- public void run() {
- System.out.println("Currently executing thread: " + Thread.currentThread().getName());
- }
- public static void main(String[] args) {
- CurrentThreadDemo t1 = new CurrentThreadDemo("Thread-1");
- CurrentThreadDemo t2 = new CurrentThreadDemo("Thread-2");
- t1.start();
- t2.start();
- }
- }
- --------------------------------------------------------------------------------
- Slip 29
- ===========
- Question 1: Write a Java program to display information about all columns in the DONAR table...
- --------------------------------------------------------------------------------
- import java.sql.*;
- public class DonarMetaData2 {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
- PreparedStatement ps = con.prepareStatement("SELECT * FROM DONAR");
- ResultSet rs = ps.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- System.out.println("Total Columns: " + columnCount);
- for (int i = 1; i <= columnCount; i++) {
- System.out.println("Column " + i + ":");
- System.out.println("Name: " + rsmd.getColumnName(i));
- System.out.println("Type: " + rsmd.getColumnTypeName(i));
- System.out.println("Size: " + rsmd.getColumnDisplaySize(i));
- System.out.println();
- }
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- --------------------------------------------------------------------------------
- Question 2: Write a Java program to create LinkedList of integer objects and perform the following...
- --------------------------------------------------------------------------------
- import java.util.LinkedList;
- public class LinkedListOperations {
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<>();
- // Adding some initial elements
- list.add(10);
- list.add(20);
- list.add(30);
- // i. Add element at first position
- list.addFirst(5);
- System.out.println("After adding at first position: " + list);
- // ii. Delete last element
- list.removeLast();
- System.out.println("After deleting last element: " + list);
- // iii. Display the size of the list
- System.out.println("Size of the list: " + list.size());
- }
- }
- --------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement