• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserting information in DB from jsp

 
Donna Bachner
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Donna Bachner
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Donna Bachner
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic