OneCompiler

JAVA 25

195

25 Age of Voter &&

Q1]

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Voter Eligibility Check</title> </head> <body> <h2>Voter Eligibility Check</h2> <form action="VoterEligibilityCheck.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> <input type="submit" value="Check Eligibility"> </form> <%-- JSP scriptlet to check eligibility and display result --%> <% // Retrieve name and age parameters from request String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age"));
    // Check eligibility
    String eligibility;
    if (age >= 18) {
        eligibility = "eligible for voting";
    } else {
        eligibility = "not eligible for voting";
    }
%>
<h3><%= name %> is <%= eligibility %>.</h3>
</body> </html>

Q2. Write a Java Program for the following: Assume database is already created.

import java.sql.;
import java.awt.
;
import java.awt.event.;
import javax.swing.
;

class ExecuteDDL extends JFrame implements ActionListener
{
JLabel jl;
JTextField jtf;
JButton jbl,jb2,jb3;
Connection con; Statement stmt;
ExecuteDDL( )
{
try
{
Class.forName("org.postgresql.Driver");
con= DriverManager.getConnection("jdbc:postgresql://localhost/postgres","postgres", "Pass@word");
stmt = con.createStatement();
setSize(700,300); setTitle("ABCD");
setLocationRelativeTo(null);
setlayout(new Flowlayout());
jl = new Jlabel("Type Your DDL Query Here");
jtf = new JTextField(20);
jbl = new JButton("Create Table");
jb2 = new JButton("Alter Table");
jb3 = add(jl);
add(jtf);
add(jbl);
add(jb2);
add(jb3);
jbl.addActionlistener(this);
jb2.addActionListener(this);
jb3.addActionlistener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
new JButton("Drop Table");

  }
  catch(ClassNotFoundException cnfe)
  {
    JOptionPane.showMessageDialog(this,"Driver Not Found" +cnfe,"Error", JOptionPane.ERROR_MESSAGE);
    dispose();
  }catch(SQLException sqle)
  {
    JOptionPane.showMessageDialog(this,"SQLException " +sqle,"Error", JOptionPane.ERROR_MESSAGE);
    dispose();
  }catch(Exception e)
  {
    JOptionPane.showMessageDialog(this,"Exception "+e, "Error",JOptionPane.ERROR_MESSAGE);
    dispose();
  }

}
public void actionPerformed(ActionEvent ae)
{
int ans;
String sql = jtf.getText();
try
{
if(sql.equals(""))
JOptionPane.showMessageDialog(this,"TextField cannot be empty", "Error", JOptionPane.ERROR_MESSAGE);
else
{
if(ae.getsource()==jbl && sql.startsWith("create"))
{
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"Table is created
Successfully", "Output",JOptionPane.INFORMATION_MESSAGE);
}
else if(ae.getSource()==jb2 && sql.startsWith("alter"))
{
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"Table is altered
Successfully", "Output",JOptionPane.INFORMATION_MESSAGE);
}
else if(ae.getSource()==jb3 && sql.startsWith("drop"))
{
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"Table is dropped
Successfully", "Output",JOptionPane.INFORMATION_MESSAGE);
}else
throw new SQLException();
}
}catch(SQLException sqle)
{
JOptionPane.showMessageDialog(this,"Error Executing DDL Query "+sqle, "Error",JOptionPane.ERROR_MESSAGE);
}finally
{
jtf.setText(""); jtf.requestFocus();
}
}

public static void main(String args[])

{
new ExecuteDDL();