# Need Help in creating online quiz application using JSP



## sganesh (Nov 16, 2008)

Hi,
i am creating online Quiz application using JSP and MySQl ,Apache 6 in Netbeans IDE.
i am able to successfully  display first question from database,and can say the answer is correct or not,,but i can't implement the program further ,in such way that when clicking next button ,new question gets displayed and score calculated there after!,
index.jsp
----------------------------------------------------------------------------------------------------
<%-- 
    Document   : start
    Created on : Nov 16, 2008, 2:58:18 PM
    Author     : Ganesh
--%>
<%@ page import="java.sql.*" %> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "*www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
     <% String ans=" ";
Connection conn = null; 
Statement st = null; 
ResultSet rs = null; 
 int i=1;
 String s,g;
 int count=0;
try { 

    //Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    conn = 
      DriverManager.getConnection("jdbc:mysql://localhost:3306/MyNewDatabase","root",""); 

    st = conn.createStatement(); 
    //for(i=1;i<=2;i++)
  //  {
    rs = st.executeQuery("select * from quiz where n="+i); 


    while(rs.next()) { 
%> 



<form  name="form1">

Question:
  <h3> <%= rs.getString("quest")%></h3>

  1: <%= rs.getString("a") %> <input type="radio" name="a" value= "a"  />

  2: <%= rs.getString("b") %>  <input type="radio" name="a" value="b" /> 

  3:<%= rs.getString("c") %>  <input type="radio" name="a" value="c"   />

  4: <%= rs.getString("d") %>  <input type="radio" name="a" value="d"   />
  <input type="submit" value="OK"> 
</form> 

<% g=request.getParameter("a");
        %>
<% ans= rs.getString("ans");
if(g.equals(ans)){
    count++;
        out.println("Correct");}
else 
    out.println("false");
%>


}
<% 

    }}
   // }
  catch (Exception ex) { 
    ex.printStackTrace(); 


        %>

<% 
    }  finally { 
    if (rs != null) rs.close(); 
    if (st != null) st.close(); 
    if (conn != null) conn.close(); 
    } 
    out.println("Score="+count);
%> 


</html>
------------------------------------------------------------------------------------------------------
contents of database
mySql-->select * from quiz;
quest                               a             b              c          d                e     ans
What is capital of india    Chennai    Mumbai    Delhi    bangalore    c    1
What is capital of tamil nadu    Chennai    madurai    trichy    coim    a    2
--------------------------------------------------------------------------------------------
i need Some ideas to build this project !!!


----------



## Desi-Tek.com (Nov 16, 2008)

what you are doing is not a best practice you should write business logic in java
try Struts 2 it is quite easy to learn and very effective in developing any kind of application in very short time.


----------



## chandru.in (Nov 17, 2008)

@sganesh

What Desi-Tek told is right.  What you are doing is completely bad way of  doing it and IMHO it is a reason you are stuck.  Even if you don't want to use any framework, I'd suggest you to read up on MVC architecture.  It will make  your life a breeze once you learn.

A simple MVC for your case would be writing a servlet, which invokes quiz validation code from another simple class.  All JSPs can post to this servlet and the servlet should dispatch to appropriate JSP after validation.

I personally like not using frameworks for simple apps and for complex apps I prefer JSF.


----------



## sganesh (Nov 17, 2008)

ok fien,i thought to reimplement the project in Struts,
Thanks!


----------

