File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet Update Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Update Code" Watch "Servlet Update Code" New topic
Author

Servlet Update Code

Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155



Throws Following Exception

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 512
Hello,
Check Line 46 in your servlet. "ad" is not initialized and hence the NullPointer.

Regards,
Amit
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Hey great your idea worked...!! now it leads me to the blank page.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Well, that's because the boolean expression in line 29 can only be false, and so the "forward" is never done.
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Paul Clapham wrote:Well, that's because the boolean expression in line 29 can only be false, and so the "forward" is never done.


Well am sorry but some more clarification I need please cause am already new to this.! Please help me!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Carefully check what you are comparing on line 29. Are you really comparing what you think you are comparing? If all else fails, use print statements to see the values. Learning to debug is a skill you must acquire if you are going to be writing this level of code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Bear Bibeault wrote:Carefully check what you are comparing on line 29. Are you really comparing what you think you are comparing? If all else fails, use print statements to see the values. Learning to debug is a skill you must acquire if you are going to be writing this level of code.


Sir please have a look at this code



In this the very first text field will contain the the value of current password & what I'm trying to check is if the fetched value is which is stored in sql1 variable is equal then it should update the password. Also you are correct that I have never written this level of code. But if you suggest me something that will really be helpful to me. Thanks a lot.

I still need to know what changes should be made to my code please.




Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Use print statements just before line 29 to check what you are comparing, as I already suggested. I'm not going to give you code. You need to learn how to diagnose problems in your own code.

Bonus hint: does sql1 really contain what you think it contains?
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Bear Bibeault wrote:Use print statements just before line 29 to check what you are comparing, as I already suggested. I'm not going to give you code. You need to learn how to diagnose problems in your own code.

Bonus hint: does sql1 really contain what you think it contains?


hey yes i did got an idea about it but i dint dare to touch it like that...!! let me try it....thanks alot
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Oh my God...!!! you are amazing...!! it printed this

select pass from staff where pass = admin1

Just super liked your solution...!! thanks a ton...!!!
let me try something else. And I'll get back to you cause I know I'd need help again...well thanks a ton anyway
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

To quote a great Jedi: "You have taken your first step into a larger world."
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Hey now I made changes to my code and changed it as below


its not updating the database but does redirect to the home page and shows username as Welcome, null

any suggestions please?
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Bear Bibeault wrote:To quote a great Jedi: "You have taken your first step into a larger world."


Well trust me if I have your support when I need I'll win this battle.!!
I promise you try myself but where I end up...I'd need your help :O
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

It cannot be redirecting because you have no redirects in your code; you have forwards. Forwards are not the same as redirects so it's important not to confuse them with each other.

If you want data to be available to the JSP you are forwarding to, you must create scoped variables in request scope (using setAttribute() on the request instance). Then these scoped variables can be accessed on the JSP via the EL.

Be sure that your JSP is using the JSTL and EL and not obsolete Java scriptlets.
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

It cannot be redirecting

Its being redirected but not updating the table records
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Again, no it's not. It's being forwarded. A forward is not a redirect.

In any case, that's independent of the database update. If that's not working, then your database code isn't succeeding. That's a JDBC issue.

My suggestion: move the database code out of the servlet and into its own independent class. It'll be easier to test (you can even write a command line program to test it) and is a better design than mixing database code into user interface code.

Check to see if an exception is being thrown.
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Bear Bibeault wrote:Again, no it's not. It's being forwarded. A forward is not a redirect.

In any case, that's independent of the database update. If that's not working, then your database code isn't succeeding. That's a JDBC issue.

My suggestion: move the database code out of the servlet and into its own independent class. It'll be easier to test (you can even write a command line program to test it) and is a better design than mixing database code into user interface code.

Check to see if an exception is being thrown.



hmm well am sorry am asking too many questions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Who said that?
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Hey there this is how I changed the code now..!!




its going on another page but not actually updating the database what's the problem. Please Suggest..!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Well, that would be because your update statement doesn't find any rows to update. Have a look at the parameters of your SQL update statement, and then look at the variables you assign to them.

And by the way, in this statement here:



Could you explain when you think the body of this if-statement would NOT be executed?
Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155

Resolved...!! Its working now...!! Thanks All for my help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Update Code