OneCompiler

ja26

106
  1. Write a Java program to delete the details of given employee (ENo EName Salary).
    Accept employee ID through command line. (Use PreparedStatement Interface)
    [15 M]
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

public class DeleteEmployee {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java DeleteEmployee <employeeId>");
return;
}

    int employeeId = Integer.parseInt(args[0]);
    
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    
    try {
        // Establish connection to the database
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
        
        // Create a prepared statement for deleting employee details
        String sql = "DELETE FROM employee WHERE ENo = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, employeeId);
        
        // Execute the delete operation
        int rowsAffected = preparedStatement.executeUpdate();
        if (rowsAffected > 0) {
            System.out.println("Employee details deleted successfully.");
        } else {
            System.out.println("Employee not found with ID: " + employeeId);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // Close the resources
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

  1. Write a JSP program to calculate sum of first and last digit of a given number. Display
    sum in Red Color with font size 18.
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Sum of First and Last Digits</title> </head> <body> <h2>Sum of First and Last Digits</h2> <% int number = Integer.parseInt(request.getParameter("number")); int firstDigit = String.valueOf(number).charAt(0) - '0'; int lastDigit = number % 10; int sum = firstDigit + lastDigit; %> <p style="color:red; font-size:18px;">Sum of first and last digits of <%= number %> is <%= sum %>.</p> </body> </html>