variable insertion into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sujaram90
    New Member
    • Jan 2013
    • 1

    variable insertion into database

    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,Subject 1,Subject2,Subj ect3,Subject4,S ubject5. 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


    Code:
    function addRow(tableID) {
    	var table = document.getElementById(tableID);   
    	var rowCount = table.rows.length; 
    	//alert("rowCount"+rowCount);
    	var row = table.insertRow(rowCount); 
    	var colCount = table.rows[0].cells.length;
    	//alert("colCount   :"+colCount);
    	
    	for(var i=0; i<colCount; i++) {                  
    		var newcell = row.insertCell(i);
    		
    		
    		
    		newcell.innerHTML = table.rows[0].cells[i].innerHTML;                
    		//alert(newcell.childNodes);                 
    		switch(newcell.childNodes[0].type) {                     
    		
    			case "label":      
    				//alert(newcell.childNodes[0].type);
    				newcell.childNodes[0].value = "";                             
    				break;                     
    				case "select-one":    
    					//alert(newcell.childNodes[0].type);
    					newcell.childNodes[0].selectedIndex = 0;   
    					newcell.childNodes[0].id = 'sub'+rowCount;
    					break;                 
    					}           
    		}         
    	}
    front end design


    Code:
    <form id="form2" name="form2" method="post" action="homeServlet" >
     <table>
    <tr>
    <th>Name</th>
    <td><input type="text" name="Name"/></td>
    </tr>
    <tr>
    <th>Id</th>
    <td><input type="text" name="Id"/></td>
    </tr>
    <tr><td>
    
      <table id="dataTable">
    <tr>
    <td>Subject
    </td>
    
    <td><select name="sub" style="width:150px;">
    	 <%ArrayList<String> al=null;
       al=(ArrayList<String>)arr; 
       int count= arr.size();
       System.out.println("Subject count:"+count);
       
       if(!al.isEmpty())
       {
       %>
      
       <option value="select" selected>----select one option----</option>
       <% for(int i=0;i<count;i++)	   { %>
       <option value="<%=al.get(i)%>"><%=al.get(i)%>
    	</option> <%	   }	  }	%>
    	 
    
    </select></td>
    
    
    </tr>
    </table></td></tr>
    </table> 
    
    
    <a href="javascript:addRow('dataTable');"><b>Add Subject</b></a><br/><br/>
    <input type="submit" value="submit" onclick="Second.jsp"/>

    Please help me.
    Thanks.
    Last edited by Meetee; Jan 18 '13, 11:01 AM. Reason: Use code tags <code/> around code
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    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 getParameterVal ues to get these values from the request.
    Code:
    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.

    Comment

    Working...