aspose file tools*
The moose likes Servlets and the fly likes JDBC and Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JDBC and Servlet" Watch "JDBC and Servlet" New topic
Author

JDBC and Servlet

Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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.
Sri Basavanahally
Ranch Hand

Joined: Oct 07, 2003
Posts: 75
Deepak,
How are you communicating with your database ? JDBC ?
-Sri


UP THE IRONS !
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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

Joined: Oct 07, 2003
Posts: 75
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

Joined: Oct 07, 2003
Posts: 75
Sorry, I misspoke about prepared statements. Ignore it. Just make sure you have gotten those values from your form into the servlet for starters.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30948
    
158

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.


[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
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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.
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
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
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Yup..
Get the values from the form in the servlet as

hth
MB


Malhar Barai
SOA & Java Book
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC and Servlet