Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC and Servlet

 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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.
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup..
Get the values from the form in the servlet as

hth
MB
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic