How to use the data retrieved from a servlet in the query statement present in a jsp file?
I am working on a project and I need to connect to a database from a jsp file. I have to use the data retrieved from a servlet in the where clause of the query. I am unable to do so correctly. Please post sample codes if possible.
Part of my code is:
//to retrieve data in jsp from servlet
<%! String[] staffData;%>
<% retrievedInfo = (ArrayList) request.getAttribut开发者_运维知识库e("filledInfo");%>
<% staffData = (String[]) retrievedInfo.get(0);%>
//After establishing connection:
ResultSet rs = stmt.executeQuery("Select * from EDUCATION where STAFF_NO= <% staffData[0] %>");
Please help me. Please tell me where to make the changes.
Thank you in advance.
Your executeQuery needs to be in a scriptlet tag and you can refer to staffData[0] directly in the code
//to retrieve data in jsp from servlet
<%! String[] staffData;%>
<% retrievedInfo = (ArrayList) request.getAttribute("filledInfo");%>
<% staffData = (String[]) retrievedInfo.get(0);%>
<%
//After establishing connection:
ResultSet rs = stmt.executeQuery("Select * from EDUCATION where STAFF_NO = ' "+staffData[0]+" ' ");
%>
<%
//then iterate through the resultset...
while(rs.next()) {
out.print(rs.getObject(1).toString());
//...etc..
}
%>
Check these ..
Display table from database
JSP Tags for SQL to connect to a database
Connecting to MySQL database and retrieving and displaying data in JSP page
Displaying Records from the Database with Java Servlets
MVC Example using Jsp, Servlets and java beans
精彩评论