0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nitin Jain
  • 133
  • 2
  • 12
  • 1
    Thats such an open ended question dude. Firstly please do not write java code inside JSP please this 2012.Any way then you need to check the logs to see if there is any error or not. put a breakpoint and see what happens when the code executes. – Shahzeb Feb 23 '12 at 03:03

2 Answers2

3

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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.

Sean
  • 7,597
  • 1
  • 24
  • 26