Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Checkbox values to a servlet

 
Chris Whited
Greenhorn
Posts: 24
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP that displays a table that is created by SQL Queries. At the end of each row I included a column that contains check boxes. Because the data in the table is constantly changing I need the value of that check box to be changing with it. So in my JSP I used an EL expression that dynamically generates the value of the value field and set it to the primary key field of the database it is querying.
Here is my JSP code that I was mentioning:

*That is just the relevant portion of it.
I know that this works (i.e. it makes each value of the value attribute equal to the primary key field (Ref_Num)). But how do I pass this value to a servlet so it can update the database?

Here is my servlet code:

*again just the relevant portion

When I run this I get a Null Pointer Exception.

Any help would be greatly appreciated. I can include the rest of the code if need be.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each value that is checked, you will get an entry in the value array. Unchecked checkboxes will not be included.
 
Chris Whited
Greenhorn
Posts: 24
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So then is that code correct?

And is my array "completed" being populated with the value attribute values?

And if so, why would I be getting a Null Pointer Exception? Or is
if("ON".equals(completed))
not the correct way of checking whether that check box was checked?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not. The values in the "completed" array will be the values assigned to the checked checkboxes.

Use the Chrome/Safari debugger and look at the request. You should see exactly what is being passed as request parameters.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Whited wrote:So then is that code correct?


Obviously not, since it throws a NullPointerException.

So the first thing for you to do is to find out what line of code is throwing the exception. You have a stack trace, right? It should tell you line numbers. So start there.
 
Chris Whited
Greenhorn
Posts: 24
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I changed my code to:


To check and see if the values were even being passed to it. And in my console this is what I was getting:
1.0
2.0
Database was updated (the 2 values I passed to it were 1.0 and 2.0. So it is getting the values and that is apparently the correct code for that.

But when I put the query and statement code back in (changed from a Statement to a PreparedStatement) I get this error:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

Can I not pass the value of an array as part of an SQL Statement?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing in the code you posted which uses SQL statements in any way, so it's impossible to understand what you are asking.
 
Chris Whited
Greenhorn
Posts: 24
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SQL statements were commented out. But never mind. I figured it out. I was incorrectly putting my array into the where clause of the update query.

Here is my code that works and updates the database for the row where the check box is selected:


Thanks everyone for their help.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're only expecting one of the boxes to be checked? Because that code will only work when exactly one of them is checked. Won't work if none are checked, won't work if two or more are checked.

I'd also suggest using a PreparedStatement in the way it was supposed to be used. Here's a fragment of code which shows the general idea:



 
Chris Whited
Greenhorn
Posts: 24
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestion. I changed the code and moved the PreparedStatement command to the correct spot so that more then one check box can be checked at one time.

Could you divulge on what you mean by this:
I'd also suggest using a PreparedStatement in the way it was supposed to be used. Here's a fragment of code which shows the general idea:

view plaincopy to clipboardprint?
String query = "UPDATE Objectives"
+ " SET Objectives.Completed=True"
+ " WHERE Objectives.Ref_Num = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, completed[i]);


I went to the API and I saw what setString did, but executeUpdate is also mentioned in there. And since I want to update the database, why would I want to do setString.
I am reasonably new to programming so I am still learning a lot of this stuff.

Again, I really appreciate all of the help.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Whited wrote:I went to the API and I saw what setString did, but executeUpdate is also mentioned in there. And since I want to update the database, why would I want to do setString.


Here's a tip: the API is a useful tool if you want to know what a method does, but it isn't a useful tool if you want to know why to use a method. For that you should be reading a tutorial. In general if you want to find a tutorial about Java feature X, your web search keywords are "java X tutorial".

In this case the PreparedStatement tutorial is here: Using Prepared Statements.
 
Chris Whited
Greenhorn
Posts: 24
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh okay. That makes a lot more sense. Thank you very much Paul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic