ja25
- 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"%>
- Write a Java Program for the following: Assume database is already created.
- 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();
}
}
}
}