0

I am trying to check if a user has voted or not with this method below. I used the same method as login and login works perfectly. An error is occuring in the ResultSet execute query part. This is to check if the user already voted or not.

public boolean checkVote(int userId) throws ClassNotFoundException, SQLException{        
    String query = "SELECT * FROM polls WHERE User='"+ userId +"''";        
    connect();        
    PreparedStatement ps = (PreparedStatement) dbConnection.prepareStatement(query);
    ResultSet results = (ResultSet)ps.executeQuery();

    while(results.next())
    {
         return true;
    }
    disconnect();
    return false;

}

Stack Trace:

     org.apache.jasper.JasperException: An exception occurred processing JSP page     /    Gadgets.jsp at line 40

 37:                 <%db.DBConnection db = new  db.DBConnection(); 
 38:                 ArrayList<Product> myProducts =db.getAllProducts();
 39:                 User u = db.getUser(session.getAttribute("Username").toString());
 40:                 boolean checkVote = db.checkVote(u.getId());
 41:                 if(checkVote == true)
 42:                                        { %>
 43:                                          <form id="Gadgets" action="Gadgets.jsp"      method="post">    
Mark Fenech
  • 1,358
  • 6
  • 26
  • 36
  • 2
    your stacktrace does not show the exception – Archer Jan 20 '13 at 15:46
  • org.apache.jasper.JasperException: An exception occurred processing JSP page /Gadgets.jsp at line 40 37: <%db.DBConnection db = new db.DBConnection(); 38: ArrayList myProducts =db.getAllProducts(); 39: User u = db.getUser(session.getAttribute("Username").toString()); 40: boolean checkVote = db.checkVote(u.getId()); 41: if(checkVote == true) 42: { %> 43:
    – Mark Fenech Jan 20 '13 at 15:48
  • are you using exactly the same code as you paste here? because i think in your sql code is a little error at the end `+ userId + "''"` - there are two `'` also in your code or this is just a mistake in your question only? If your code looks the same as here it's possible that there are exception under `JasperException`, some kind of `SQLException` and `JasperException` is just an effect of `SQLException`. Can it be true? – Michał Kupisiński Jan 20 '13 at 16:07
  • As a side note: [using scriplets in JSP is highly discouraged](http://stackoverflow.com/a/3180202/814702) – informatik01 Jan 20 '13 at 22:37

1 Answers1

0

Not about your exception, but there's a comment on your code. Since you're not using statement parameters (passed through ?) you don't need PreparedStatement, you can use simple Statement and get it through connection.createStatement. Or better approach is to keep using PreparedStatement and pass userId through parameter.

More on this here

Now about your exception. Seems that checkVote method is your servlet method, and not method of your db object. But you're trying to call db's checkVote. This is a reason of JasperException.

Archer
  • 5,073
  • 8
  • 50
  • 96