0

i am new in jsp and i used this code to insert data into msaccess table i got problem that when i click submit button after filing all required field the page should redirect to quiz.jsp. but it is not happening and noting inserted to the db too.

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

<%
   if(request.getParameter("submit")!=null)
 {
Connection conn=null;
Statement stmt=null;
try
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn=DriverManager.getConnection("jdbc:odbc:biplob");
try{
Statement st = con.createStatement();
String quest = request.getParameter("quest").toString();
String QA = request.getParameter("QA").toString();
String QB = request.getParameter("QB").toString();
String QC = request.getParameter("QC").toString();
String QD = request.getParameter("QD").toString();
String correctAns = request.getParameter("correctAns").toString();
       out.println("quest : " + quest);

   String qry = "insert into quiz(quest,QA,QB,QC,QD,correctAns)values('"+quest+"','"+QA+"','"+QB+"','"+QC+"','"+QD+"','"+correctAns+"')";

     out.println("qry : " + qry);

int val = st.executeUpdate(qry);

    if(val>0)
    {
      response.sendRedirect("quiz.jsp");
    }
con.close();
   }

   catch(SQLException ex){
System.out.println("SQL satatment not found");
   }
   }
   catch(Exception e){
e.printStackTrace();
   }
   }
   %>

pls help me wih this .. where is wrong in this code .

biplob
  • 39
  • 4
  • 12

3 Answers3

1

You have kept one extra closing brace. I think it will work if you remove it.

Echilon
  • 10,064
  • 33
  • 131
  • 217
surya
  • 11
  • 1
0

Scriplets are really bad practice in jsp , you should avoid using it . The workflow you mentioned can entirely done without scriplet . Insert data to the db from servlet instead.

Do it in that way and if you would face any problems ask it here at SO. See How to avoid Java Code in JSP-Files?.

Community
  • 1
  • 1
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
0

Replace

    } catch(SQLException ex) {
        System.out.println("SQL satatment not found");
    }
} catch(Exception e) {
    e.printStackTrace();
}

by

    } catch(SQLException ex) {
        throw new ServletException(ex);
    }
} catch(Exception e) {
    throw new ServletException(e);
}

To get a detailed error report complete with the root cause exception, its message and the full stacktrace. It contains the answer.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555