Note that there are some explanatory texts on larger screens.

plurals
  1. POJSP Quiz application with database access
    primarykey
    data
    text
    <p>I'm working on a java quiz application. The application will consist of 20 questions of one of the following forms:</p> <blockquote> <p>What is the english word for the german word xxxx? </p> <p>What is the german word of the english word xxxx?</p> </blockquote> <p>I have created a database with a word table and a page to add words to that table. the table consists of the following columns: germanWord, gender, englishWord</p> <p>My problem is that upon each question, I would like to randomly pick a word from that database to form the question so for example:</p> <blockquote> <p>What is the german word of the english word <em>wordTable.getEnglishName</em>?</p> </blockquote> <p>Lastly, the question is a multiple choice question so is it possible that in one of the 4 possible answers I could insert a command to take the relevant answer from that same entry that is chosen for the question? </p> <pre><code>&lt;input type="radio" name="q1Answer" value="A"/&gt;&lt;label for="A"&gt;A) fixed answer&lt;/label&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="B"/&gt;&lt;label for="B"&gt;B) fixed answer&lt;/label&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="C"/&gt;&lt;label for="C"&gt;C) *get name from the database*&lt;/label&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="D"/&gt;&lt;label for="D"&gt;D) fixed answer&lt;/label&gt;&lt;br/&gt;&lt;br/&gt; package org.me.jsp.beans; import java.sql.*; import java.util.*; public class WordDataBean { private Connection connection; private PreparedStatement addWord, getWords, removeWord, getRandEnglishWord; public WordDataBean() throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/university2", "root", ""); getWords = connection.prepareStatement("SELECT * FROM word"); addWord = connection.prepareStatement("INSERT INTO university2.word ( " + "germanName, gender, englishName ) " + "VALUES ( ?, ?, ?);"); removeWord = connection.prepareStatement("DELETE FROM university2.student " + "WHERE germanName='?';"); getRandEnglishWord= connection.prepareStatement("SELECT englishName FROM word" + "ORDER BY RAND() LIMIT 1"); } catch (SQLException sqle) { sqle.printStackTrace(); } } public List&lt;WordBean&gt; getWordList() throws SQLException { List&lt;WordBean&gt; wordList = new ArrayList&lt;WordBean&gt;(); // obtain list of titles ResultSet results = getWords.executeQuery(); // get row data while (results.next()) { WordBean word = new WordBean(); word.setGermanName(results.getString(1)); word.setGender(results.getString(2)); word.setEnglishName(results.getString(3)); wordList.add(word); } return wordList; } public void addWord(WordBean word) throws SQLException { addWord.setString(1, word.getGermanName()); addWord.setString(2, word.getGender()); addWord.setString(3, word.getEnglishName()); addWord.executeUpdate(); } public void removeWord(WordBean word) throws SQLException{ removeWord.setString(1,word.getGermanName()); removeWord.executeUpdate(); } public void randomEnglishWord(WordBean word) throws SQLException{ getRandEnglishWord.setString(1, word.getEnglishName()); getRandEnglishWord.executeQuery(); } protected void finalize() { try { getWords.close(); addWord.close(); connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } } } </code></pre> <p>Here is a sample of one of the question jsp pages, q1.jsp:</p> <pre><code>&lt;%-- Document : q1 Created on : 06-May-2012, 18:54:24 Author : encore --%&gt; &lt;!--This JSP acts as the first question in a multiple choice quiz, with the user asked to submit their answer to the question--&gt; &lt;?xml version = "1.0"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Big Java Quiz, question 1&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;/head&gt; &lt;body&gt; &lt;%if(request.getParameter("choice").equals("N")) out.print("&lt;meta http-equiv='refresh' content='0;url=options.jsp'/&gt;"); //Redirects user back to index if they did not want to take quiz%&gt; &lt;form action="q2.jsp" method="POST"&gt; &lt;b&gt;Question 1.&lt;/b&gt; What is the German noun for the English word ______?&lt;br/&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="A"/&gt;&lt;label for="A"&gt;A) Exception generator&lt;/label&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="B"/&gt;&lt;label for="B"&gt;B) Exception manipulator&lt;/label&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="C"/&gt;&lt;label for="C"&gt;C) Exception handler&lt;/label&gt;&lt;br/&gt; &lt;input type="radio" name="q1Answer" value="D"/&gt;&lt;label for="D"&gt;D) Exception monitor&lt;/label&gt;&lt;br/&gt;&lt;br/&gt; &lt;input type="submit" value="Submit"/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload