0% found this document useful (0 votes)
41 views2 pages

Java Slip 5

The document contains two Java code snippets. The first snippet demonstrates creating a hash table to store student details, displaying them using Enumeration. The second snippet is a JSP page that connects to a MySQL database to fetch a random quiz question and display it with multiple-choice options in an HTML form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Java Slip 5

The document contains two Java code snippets. The first snippet demonstrates creating a hash table to store student details, displaying them using Enumeration. The second snippet is a JSP page that connects to a MySQL database to fetch a random quiz question and display it with multiple-choice options in an HTML form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1

import java.util.*;

public class StudentHashTable {


public static void main(String[] args) {
// Creating a hash table
Hashtable<String, String> studentTable = new Hashtable<>();

// Adding student details (Mobile Number -> Student Name)


studentTable.put("9876543210", "Alice");
studentTable.put("8765432109", "Bob");
studentTable.put("7654321098", "Charlie");
studentTable.put("6543210987", "David");

// Displaying student details using Enumeration


Enumeration<String> keys = studentTable.keys();
System.out.println("Student Details:");

while (keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.println("Mobile: " + key + ", Name: " +
studentTable.get(key));
}
}
}

Q2

<%@ page import="java.sql.*, java.util.*" %>


<%
// Database connection details
String dbURL = "jdbc:mysql://localhost:3306/quizdb";
String dbUser = "root";
String dbPassword = "password";

Connection conn = null;


Statement stmt = null;
ResultSet rs = null;

// Session tracking for question index and score


Integer questionIndex = (Integer) session.getAttribute("questionIndex");
Integer score = (Integer) session.getAttribute("score");
if (questionIndex == null) {
questionIndex = 0;
score = 0;
}

// Fetching a random question from database


try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dbURL, dbUser, dbPassword);
stmt = conn.createStatement();

rs = stmt.executeQuery("SELECT * FROM questions ORDER BY RAND() LIMIT 1");


String question = "";
String optionA = "", optionB = "", optionC = "", optionD = "",
correctAnswer = "";
int questionId = 0;

if (rs.next()) {
questionId = rs.getInt("id");
question = rs.getString("question_text");
optionA = rs.getString("option_a");
optionB = rs.getString("option_b");
optionC = rs.getString("option_c");
optionD = rs.getString("option_d");
correctAnswer = rs.getString("correct_answer");
}

// Storing correct answer in session


session.setAttribute("correctAnswer", correctAnswer);
session.setAttribute("questionIndex", questionIndex + 1);
} catch (Exception e) {
out.println("Error: " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
%>

<html>
<head>
<title>Online MCQ Test</title>
</head>
<body>
<h2>Question <%= questionIndex %></h2>
<form action="process.jsp" method="post">
<p><%= question %></p>
<input type="radio" name="answer" value="A"> <%= optionA %><br>
<input type="radio" name="answer" value="B"> <%= optionB %><br>
<input type="radio" name="answer" value="C"> <%= optionC %><br>
<input type="radio" name="answer" value="D"> <%= optionD %><br>
<input type="hidden" name="questionId" value="<%= questionIndex %>">
<input type="submit" name="action" value="Next">
<input type="submit" name="action" value="Submit">
</form>
</body>
</html>

You might also like