Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with servlet to edit database

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a jsp file that just contains textboxes that are populated with values from the database.
I can edit the textboxes and this edits the database. I created a servlet to handle the database code but after i edited the textboxes and press the submit button the screen just displays null

here is the code for the servlet:
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put an "e.printStackTrace()" after line 60, so that you'll see the exception stack trace in the log file. A mistyped parameter name could have caused this.

Also, the update statement takes 16 parameters, but you're only supplying 15. There's also a comma missing after the "lotsize" parameter, and you need to remove all those single quotes.

As an aside, for something that updates the DB you should be using a POST (and thus override doPost instead of doGet).
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karen

Try printing stack trace in catch block, you may get some useful information from stacktrace.

e.printstackTrace();

thanks
abhay
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i changed the jsp file to method="post"
the servlet gets the id of the value in the database from the jsp file so i'm not sure if i need the line int id = Integer.parseInt(request.getParameter("id"));
but everything is spelled correctly but i am still getting null

i changed the code to this:
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the stack trace. There's no point in us guessing at it when you can just post it so that we can see what, exactly, the problem is. It will include the line number in your code where the error occurs - that points very directly at the problem.

Also, the DB statement is not going to work until you fill in all parameters - "id" is still missing (I don't understand what you mean by your remark about it - it doesn't matter where the value comes from, if it is in the DB statement then you need to set it.)
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no stack trace. the only thing that appears on the screen is null

this is current code:
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace would be in the log files, not on the screen.

The DB statement now has 17 parameters, so you're still one short - id appears first and last. The first is unnecessary, though, since the WHERE condition takes care only to select records that already have that value of id.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't see the problem with the sql statement

i have 16 parameters

here is the stack trace:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at servlets.EditPropertyServlet.doPost(EditPropertyServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 16 parameters

This code does, yes, but the previous had 17 (the first and the last both named "id").

The code doesn't even get to the SQL statement, though, the exception happens before that.

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at servlets.EditPropertyServlet.doPost(EditPropertyServlet.java:31)

Which one is line 31 of your class? We don't see the entire code, so we can't know what line 31 is. But clearly one of the parameters the code assumes to be passed in, isn't. Probably mismatched parameter names between the HTML form and the servlet code.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 31 in my class is

here's the jsp page if that helps:
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Java code is expecting a parameter named "id". I don't see any such parameter in the HTML you posted.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm calling id from the line <jsp:useBean id="prop" type="beans.Property" scope="request"/>
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Good practice would be to get a String variable from your getParameter() call and check that for null.
If not null then check for empty String.

Bill
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Wilson wrote:i'm calling id from the line <jsp:useBean id="prop" type="beans.Property" scope="request"/>


I don't know what "calling" means in that context, but at any rate when you submit the form from that HTML you just submit the form and its input variables. Other things in the JSP are irrelevant.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we've been through this before. JSP markup runs before the page is loaded; as pointed out, it has nothing at all to do with the form submission which happens after the page loaded and displayed.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understand now thanks. so i added the line <input type="text" hidden="true" name="id" value="${prop.id}"/> to the jsp but now its displaying failed to update the data
is there still something wrong with the sql? I can't see the problem

this is the current code:
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Wilson wrote:is there still something wrong with the sql? I can't see the problem


Yes, that's right. Go through your PreparedStatement parameters, those question marks, and then compare to the following code which sets their values. Make sure that you're assigning the right value to each of the parameters.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 29, "id" is not the 1st parameter, it's the last. So all other parameters come down by 1, and "id" takes the last.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i see the problem. Thank you that has solved it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!