0

I am having problem updating the a db table . Let me first post the code

 <%

String query="select * from COURSE";

Connection con=RegisterDao.connect();
PreparedStatement ps = con.prepareStatement(query);

ResultSet rs=ps.executeQuery();

%>
<form action="Faculty_Course.jsp" method="POST">

                <table align="center" cellpadding = "10" id="table_td">

                   <tr> <td></td>
                        <td ><b>Name</b></td>
                        <td ><b>Duration(in months)</b></td>
                        <td ><b>Start Date</b></td>
                        <td ><b>End Date</b></td>
                        <td ><b>Fees(in $)</b></td> 
                        <td><b> Faculty</b></td>
                    </tr>

<%
while(rs.next()){
%>
                  <tr width=20% style="color:black">
                    <td ><input type="radio"  name="name" style="display: inline" value='<%= rs.getString(1)%>' /></td>
                        <td><%= rs.getString(1)  %></td>
                        <td><%= rs.getString(2)  %></td>
                        <td><%= rs.getString(3)  %></td>
                        <td><%= rs.getString(4)  %></td>
                        <td><%= rs.getString(5)  %></td>
                    <td>
                    <select>    
                    <%  
                        String query1="select * from STAFF_REGISTER";   
                        PreparedStatement ps2=con.prepareStatement(query1);
                        ResultSet rs2=ps2.executeQuery();
                        while(rs2.next()) {%>   

                    <option   name="emailid" value='<%= rs2.getString(3) %>'><%= rs2.getString(3) %></option>

                    </select>

                </td>       

                    </tr> 



            <%  } 
            }
                %>

                  </table>
                  <input type="submit" name="submit" value="UPDATE" 
                    style="margin-left:50%;font-size: 22px;background-color: lightblue;color:black"/>
           </form>



<% 

 String name=request.getParameter("name");
 String emailid=request.getParameter("emailid");
 System.out.println(name+"  "+emailid);
if(name!=null && emailid!=null){
String insert_query="insert into FAC_COURSE values (?,?)";
PreparedStatement ps3=con.prepareStatement(insert_query);
ps3.setString(1, name);
ps3.setString(2, emailid);
int rs1=ps3.executeUpdate();

}

%>

Here I am trying to fetch value from course table and from emailid from staff_register and trying to insert them into a third table fac_course. But after clicking the submit button it's not entering. I also tried printing the values through the System.out.println(name+" "+emailid); to check if they are printed on console . after clicking submit only the name is printed but not emailid . the emailids' are showing in the dropdown list. So I am a bit confused about the problem .

2 Answers2

0

Move name="emailid" attribute from <option> to <select>.

Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25
  • I did that at first but that's causing some problem. Its only showing the first value of staff_register in the drop -down list. – subhadeep Jul 28 '16 at 14:26
  • Eh? How could just moving an attribute to a proper place* (without changing the Java code) cause such malfunction? * there is [no name attribute](http://www.w3schools.com/tags/tag_option.asp) defined for the ` – Jozef Chocholacek Jul 28 '16 at 14:32
  • By the way thanks. moving the attribute made the insertion successful. . Thnaks @Jozef Chocholacek – subhadeep Jul 28 '16 at 14:33
  • YW. Btw. as `query1="select * from STAFF_REGISTER"` is always the same, I would suggest moving it out of the cycle, running it just once, and pre-fill a list of options to use withing the cycle then. – Jozef Chocholacek Jul 28 '16 at 14:36
  • 1
    I would also recommend you to have a look at the [MVC pattern](http://stackoverflow.com/questions/18354034/how-to-create-mvc-based-application-without-using-framework) and move your business logic out of the JSP. – Jozef Chocholacek Jul 28 '16 at 14:38
  • my full project is in MVC(with servlets, model and data access object) pattern. But as I don't have time (got project of creating a demo distance learning website in 5 days ) I did this. Thanks anyway – subhadeep Jul 28 '16 at 14:44
-2

Use SQL Statement rather than using PreparedStatement. You will have the data.

Kintu Barot
  • 320
  • 2
  • 11