This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JDBC and the fly likes inserting information in DB from jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting information in DB from jsp" Watch "inserting information in DB from jsp" New topic
Author

inserting information in DB from jsp

Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I want to insert values from jsp text boxes into my database. I have no clue on how to start. I am using netBeans 5.5 with the derby database(JDBC). My problem is I don't know how to take the varible from the text box and place it in the SQL insert statement. How do I start? for examlpe I have 3 textboxes labeled firstName, lastName, phone. I have the values firstname.value, lastnname.value and phone.value.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Donna,
What do you have so far? A page submits the form and then you ___________.

The major chunks to start are reading the request parameters and building a statement.

Note that it is considered good practice to keep JDBC code out of a JSP. Instead, put it in a servlet or Java class.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

when JSP page got rendered and after user fills those text boxes and submits the form.

In server side you will get those values using HttpServletRequest object. using getParameter("<name you given for that field in JSP>") you can get those values.

After getting those values,Then construct Query with those values and execute it using JDBC.

This is the simple way of doing it.

As Jeanne asked, Tell US what are you up to?. you will get good suggestion if you make requirement clear.

Thanks.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I want to send the inforrmation from the textboxes to the database. I really want to go to the bean but was not sure how. This is what I am trying to do. I was going from the submit to a jsp page. At this jsp page I was trying to call use bean to move to the bean. If I place the useBean before the submit in the form on the first jsp I would eliminate the second jsp would that work? I don't have the code after the submit to the jsp.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

first JSP ==> Has Text Boxes where user fills up the data and submit the form.


secondJSP ==> After "submit" pressed control comes to this JSP. Here you wanna do database operations.


I guess this is your requirement. Is this correct?
[ December 09, 2006: Message edited by: Prabhu venkatachalam ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Originally posted by Donna Bachner:
If I place the useBean before the submit in the form on the first jsp I would eliminate the second jsp would that work?


Of course not. The useBean executes on the server long before the page containing the form ever gets sent to the browser.

You really need to take a step back and get a handle on how the technologoes that you are using work. Perhaps this article will help with regard to understanding JSPs.

I don't have the code after the submit to the jsp.


As Jeanne pointed out, you shouldn't be submitting to a JSP. Using JSP for anything other than renderring the HTML view is considered an extremely poor practice. Submit to a servlet to perform your data processing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
Bear,
Your link helped tremdously Thats in the end what I have been trying to do. To useBean on the first jsp would cause it to deploy before the submit button. and sending it to the servlet was not working for me either. I just have one last question. How do you use a variable in your sql statement? I have the following sql statement with the varable "name" I have tried this:
select * from ADDRESSBOOK WHERE lastName = (name);
and
select * from ADDRESSBOOK WHERE lastName = ('name');
and
select * from ADDRESSBOOK WHERE lastName = (name.value); ;)
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
Prabhu,
you are right that is where I want to call the bean(or servlet) to do the JDBC stuff. If I call the servlet from the first JSP I would not need the second correct? can I pass the varibles using what means. I know how to send the information to a bean but not to a servlet. What I need is any way to send the information from the textbox On a JSP to a servlet (or bean) to put the information into a DB. I have tried to use the submit option to send the page to the servlet and that does not work. I also tried useBean
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I found the answer to my question about how to pass a value from the jsp to the servlet. The only question I have now is how to put the variable value into the SQL is there special syntax. ;)
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

I would suggest you to go through some JDBC tutorial to learn basics about it. If you are having HERE is the one SUN provides. Offcourse look into JDBC FAQ from Java Ranch.

Code will look something like this,

Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
thanks,
That tutorial and your suggestion was just what I needed. I find alsot of Suns tutorials hard to understand but this one was easy. I think I can now finish my assignment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inserting information in DB from jsp