By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

variable insertion into database

P: 1
Hi everybody
I am a fresher. I am working in java. Totally new with java. Now I am doing one simple project. The front end consists of name and one text box for entering name. And next row consists of Id and one text box for entering id. Then I need to select subjects from a combo box. The combo box entries should come from database. That things are working properly. I want to select 5 subjects. So I have to select subject from 5 different combo boxes. I gave same name for each combo box. And used java script for dynamically adding rows. That also working properly. Then I have a table in database to insert these values. My table has seven column withn column names Name,Id,Subject1,Subject2,Subject3,Subject4,Subjec t5. Name,Id are coming correctly into database. however same subjects are coming into all columns of table eventhough i select different subject names.Only first subject is ncoming into each column. I know the problem also. That is because I have given same name for each combo box. I am using oracle database.
I am giving my jsp page below.

java script for dynamically adding rows


Expand|Select|Wrap|Line Numbers
  1. function addRow(tableID) {
  2.     var table = document.getElementById(tableID);   
  3.     var rowCount = table.rows.length; 
  4.     //alert("rowCount"+rowCount);
  5.     var row = table.insertRow(rowCount); 
  6.     var colCount = table.rows[0].cells.length;
  7.     //alert("colCount   :"+colCount);
  8.  
  9.     for(var i=0; i<colCount; i++) {                  
  10.         var newcell = row.insertCell(i);
  11.  
  12.  
  13.  
  14.         newcell.innerHTML = table.rows[0].cells[i].innerHTML;                
  15.         //alert(newcell.childNodes);                 
  16.         switch(newcell.childNodes[0].type) {                     
  17.  
  18.             case "label":      
  19.                 //alert(newcell.childNodes[0].type);
  20.                 newcell.childNodes[0].value = "";                             
  21.                 break;                     
  22.                 case "select-one":    
  23.                     //alert(newcell.childNodes[0].type);
  24.                     newcell.childNodes[0].selectedIndex = 0;   
  25.                     newcell.childNodes[0].id = 'sub'+rowCount;
  26.                     break;                 
  27.                     }           
  28.         }         
  29.     } 
  30.  
front end design


Expand|Select|Wrap|Line Numbers
  1. <form id="form2" name="form2" method="post" action="homeServlet" >
  2.  <table>
  3. <tr>
  4. <th>Name</th>
  5. <td><input type="text" name="Name"/></td>
  6. </tr>
  7. <tr>
  8. <th>Id</th>
  9. <td><input type="text" name="Id"/></td>
  10. </tr>
  11. <tr><td>
  12.  
  13.   <table id="dataTable">
  14. <tr>
  15. <td>Subject
  16. </td>
  17.  
  18. <td><select name="sub" style="width:150px;">
  19.      <%ArrayList<String> al=null;
  20.    al=(ArrayList<String>)arr; 
  21.    int count= arr.size();
  22.    System.out.println("Subject count:"+count);
  23.  
  24.    if(!al.isEmpty())
  25.    {
  26.    %>
  27.  
  28.    <option value="select" selected>----select one option----</option>
  29.    <% for(int i=0;i<count;i++)       { %>
  30.    <option value="<%=al.get(i)%>"><%=al.get(i)%>
  31.     </option> <%       }      }    %>
  32.  
  33.  
  34. </select></td>
  35.  
  36.  
  37. </tr>
  38. </table></td></tr>
  39. </table> 
  40.  
  41.  
  42. <a href="javascript:addRow('dataTable');"><b>Add Subject</b></a><br/><br/>
  43. <input type="submit" value="submit" onclick="Second.jsp"/>
  44.  

Please help me.
Thanks.
Jan 18 '13 #1
Share this Question
Share on Google+
1 Reply


100+
P: 185
I'm not very aware of your case but it seems to me that the number of subjects is variables and not static (i.e. not always 5). Which means, you'll have either so many nulls in your DB or cases where more subjects need to be added. A better DB design would be to separate the subject in a child table.

Anyway, if you still need the above design to work, make sure that at the target servlet/JSP you use getParameterValues to get these values from the request.
Expand|Select|Wrap|Line Numbers
  1. String[] subjects = request.getParameterValues("sub");
If you still have the problem, please share the code you use to prepare the values for insertion into the DB -homeServlet code.
Jan 18 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.