Nitin Jain
Nitin Jain

Reputation: 133

New row is not inserted in database

I have been facing some serious issues while executing this particular JSP page. CODE

<tr>
<td>
<%rs2 = st.executeQuery("Select * from degrees"); %>
<select name = "courses">
<%while(rs2.next()) {%>
<option value = "<%=rs2.getString(1)%>"><%=rs2.getString(2)%></option>
<%} %>
</select> 
</td>
<td>
<input type = "text" name = "ratings" class = "span1">
</td>
<td>
<a href = "">Delete Course</a>
</td>
</tr>
<%if(request.getParameter("courses")!="0"&&request.getParameter("ratings")!=null){
   String degree_id = request.getParameter("courses");
   int ratings = Integer.parseInt(request.getParameter("ratings"));
   PreparedStatement ps = con.prepareStatement("Insert into college_degree values (?,?,?)");
   ps.setString(1,college_id);
   ps.setString(2,degree_id);
   ps.setInt(3,ratings);
   ps.executeUpdate();
   c_a = Integer.parseInt(course_added);
   c_a++;
   String str = Integer.toString(c_a);
   course_added = str;
   String site = new String("degree_to_college.jsp?course_added="+course_added);
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site);  
}

%>
</tbody>
</table>
<form action = "degree_to_college.jsp">
<input type = "submit" class = "btn-primary large" value = "NEW COURSE" />
</form>**

Here thr trouble is after clicking the NEW COURSE I am redirected to the same page without any changes in the database. No data is added to the database.

Upvotes: 0

Views: 116

Answers (2)

Sean
Sean

Reputation: 7737

On top of what BalusC said.

The HTML form is separate from the select/Option statement. Make sure the Select element is inside the Form to ensure it gets submitted.

Upvotes: 0

BalusC
BalusC

Reputation: 1108742

You forgot to close the statement and the connection after use. Closing the connection will immediately commit the transaction. The normal JDBC idiom is that they are closed in finally block of the try block as where they're created.

You've by the way more serious issues with this JSP.

Upvotes: 3

Related Questions