OneCompiler

ja25

151
  1. Write a JSP program to accept Name and Age of Voter and check whether he is
    eligible for voting or not. [15 M]
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Voter Eligibility Check</title> </head> <body> <h2>Voter Eligibility Check</h2> <form action="CheckEligibility.jsp" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required><br><br> <label for="age">Age:</label> <input type="number" id="age" name="age" required><br><br> <button type="submit">Check Eligibility</button> </form> </body> </html>
  1. Write a Java Program for the following: Assume database is already created.
  2. import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

public class DatabaseInteraction {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;

    try {
        // Load the MySQL JDBC driver
        Class.forName("com.mysql.jdbc.Driver");
        
        // Establish connection to the database
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
        
        // Create a statement object
        statement = connection.createStatement();
        
        // Execute a SQL query
        resultSet = statement.executeQuery("SELECT * FROM tablename");
        
        // Process the result set
        while (resultSet.next()) {
            // Retrieve data from the result set
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            
            // Display the data
            System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // Close the resources
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}