This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes unable to insert values in database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "unable to insert values in database" Watch "unable to insert values in database" New topic
Author

unable to insert values in database

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 202

data is not inserting the database.


please help in resolving this problem
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
1.) Do not have empty catch blocks. You are probably hiding an exception which is making the data not insert.
2.) Do not write code that connects to databases in JSPs. Use a servlet for that (or a normal class which you call from the servlet). JSPs should be used for presentation only not for processing.
3.) Use a PreparedStatement to set parameters to guard against SQL injection.
Krishna Kanth
Ranch Hand

Joined: Oct 23, 2006
Posts: 49
    
    1

Is there an error? is your out.println printing the message? is lang[] coming in as a non-null param?

I am expecting a NullPointerException.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 202

krishna kanth wrote:Is there an error? is your out.println printing the message? is lang[] coming in as a non-null param?

I am expecting a NullPointerException.


yes,you are right null pointer exception is coming.
how can i remove this?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6

You should avoid putting code in jsp as suggested by E Armitage. However to resolve this particular issue, you may wish to try this



Swastik
Krishna Kanth
Ranch Hand

Joined: Oct 23, 2006
Posts: 49
    
    1

I see you just started practicing JSP's, but you would need to follow guidelines Armitage mentioned above.
First thing move the code off JSP to a servlet or server side java class to execute the logic, which will show you the problem clearly.


Are you expecting a lang to be saved to DB, when you access this JSP by URL? lang would get saved to DB only when you submit this page.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 202

now this is printing "No data found to be inserted "
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6

It means you are not getting any values from the page (from where you are submitting the details) to this jsp page.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 202

krishna kanth wrote:I see you just started practicing JSP's, but you would need to follow guidelines Armitage mentioned above.
First thing move the code off JSP to a servlet or server side java class to execute the logic, which will show you the problem clearly.


Are you expecting a lang to be saved to DB, when you access this JSP by URL? lang would get saved to DB only when you submit this page.

ok. i'll first convert into servlet and jsp and then try to insert the values in the database.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 202

Swastik Dey wrote:It means you are not getting any values from the page (from where you are submitting the details) to this jsp page.


yes
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
The code shouldn't be doing any of the DB stuff unless the "lang" parameter actually exists. (Any yes - once you implement all the advice given above, the DB stuff will not longer be part of the JSP, so the issue is moot.) My guess is that you're accessing this JSP directly - in which case no parameter is passed.


Ping & DNS - my free Android networking tools app
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Why is there a need to use

As there is no multiple=true in your select tag. You can only select one value from the combo.

And catch more specific exceptions in catch block then catching all exceptions at once using a general Exception class.


OCPJP
 
wood burning stoves
 
subject: unable to insert values in database