On one page I displayed a table from my database and the user chooses a department. Based on that option the next page will display the items within that department. Is there a way that you can pass the department id from page 1 to page 2's SQL query so that you can display only that department's items?
The only thing I am aware of is printing the department number on the page with
<%=session.getAttribute("deptid")>;
First Page:
<table>
<tr>
<td>Select Department</td>
<td><select name="deptid">
<option value="select">select</option>
<%
while(rs.next()){
String deptid= rs.getString("deptid");
%>
<option value=<%=deptid%>><%=deptid%></option>
<%
}
}catch(SQLException sqe){
out.println("showcourses"+sqe);
}
%>
</select></td>
</tr>
</table>
Here's a connector JSP page
String sql = "select * from courses where deptid=?";
String deptid= request.getParameter("deptid");
if(!(deptid.equalsIgnoreCase("select"))){
try{
Class.forName(driverName);
con = DriverManager.getConnection(url, user, dbpsw);
ps = con.prepareStatement(sql);
ps.setString(1, majorid);
rs = ps.executeQuery();
if(rs.next()){
userdeptid = rs.getString("deptid");
System.out.println(userdeptid );
if(deptid.equals(userdeptid )){
session.setAttribute("deptid",userdeptid);
response.sendRedirect("showitems.jsp");
}
}else
response.sendRedirect("error.jsp");
rs.close();
ps.close();
} catch(SQLException sqe){
out.println(sqe);
}
}
%>