• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

JDBC and Servlet

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a form
<form name= "businessunit" action="insertBusinessUnit" method="POST" title="theProjectForm">
<table>
<tr>
<td align="right">Business Unit :</td>
<td align="left"> <input type="text" name="businessunit"></td>
</tr>
<tr>
<td align="right">Description :</td>
<td align="left"> <textarea name="description" cols='30' rows='1'>
</textarea> </td>
</tr>
</form>
It calls servlet insertBusinessUnit in action tag and this servlet is inserting values in database. My SQl query is :
"Insert into sandbox.businessunit (businessunitname, businessunitdescription)values ('" + businessunit + "', '" + description + "')
My query is fine, but when i compile it gives me error which states:
businessunit not defined and description not defined.
I don't how to pass those values from my form to the database.
Plz help.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
How are you communicating with your database ? JDBC ?
-Sri
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the connection pooling is done through BEA Weblogic
The connection pooling is like this:
Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup( "itads-datasource" );
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
stmt.executeUpdate ("Insert into sandbox.businessunit (businessunitname, businessunitdescription)values ('" + BusinessUnit + "', '" + Description + "');");
stmt.close();
con.close();
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
Have you made sure you have gotten the input parameters, businessunit and the description from the request into your servlet(request.getParameter()) ?
Obviously, you have to do the above and assign the value to a variable and then pass that to the JDBC Code. Also, you may want to look at PreparedStatements for this. If you use a regualr statement you will have to provide values and not variables.
-Sri
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I misspoke about prepared statements. Ignore it. Just make sure you have gotten those values from your form into the servlet for starters.
 
author & internet detective
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
How are you getting the form values from the HTML form? You should be using:
String businessUnit = request.getParameter("businessUnit");
Otherwise, the servlet doesn't know the parameter exists.
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i have done
String businessUnit = request.getParameter("businessUnit");
in my servlet but how do i extract the values from the form. I tried to print the values of those variable names, it is printing null, so it is not getting the values from the form, how can i do that. plz help.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're using:
String businessUnit = request.getParameter("businessUnit");
in your servlet, but you specify that on the form you're using "businessunit" - check that the name attribute of the input field matches with what you're attempting to get out of the request. This may simply just be a spelling issue.
--BW
 
Author
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup..
Get the values from the form in the servlet as

hth
MB
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic