> 查询数据库
public ArrayList selectByProjectName(String projectname) {
Connection conn = BaseDAO.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList list = null;//最后存放数据的集合
if(conn == null){
return null;
}else{
try {
//convert 转换
String sql ="select * from s_data where projectname like ? ";
stmt=conn.prepareStatement(sql);
stmt.setString(1,"%"+projectname+"%");
rs = stmt.executeQuery();
if(rs!=null){
list = new ArrayList<TestData>();
while(rs.next()){
TestData testData = new TestData();
testData.setId(rs.getInt("id"));
testData.setProjectname(rs.getString("projectname"));
testData.setTestnum(rs.getInt("testnum"));
testData.setCustomname(rs.getString("customname"));
testData.setTestname(rs.getString("testname"));
testData.setData1(rs.getString("data1"));
testData.setData1(rs.getString("data2"));
testData.setData1(rs.getString("data3"));
testData.setData1(rs.getString("data4"));
testData.setData1(rs.getString("data5"));
testData.setData1(rs.getString("data6"));
testData.setData1(rs.getString("data7"));
testData.setData1(rs.getString("data8"));
list.add(testData) ;
}
}
System.out.println("sql"+sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
BaseDAO.close(rs);
BaseDAO.close(stmt);
BaseDAO.close(conn);
}
return list;
}
查找servlert
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
TestDataService service = new TestDataService();
String type =request.getParameter("selects");
String content =request.getParameter("content");
if(content.equals("") || content==null) {
System.out.print("<script>alert('实验数据正在更新');</script>");
System.out.println("实验数据正在更新!");
// response.sendRedirect("/manage/showAddSeachBook.jsp");
} else{
ArrayList <TestData> list = service.getAllDatas(type, content);
System.out.println("模糊查询:"+list);
System.out.println("content:"+content);
System.out.println("type:"+type);
session.setAttribute("alldata", list);
request.getRequestDispatcher("/manage/showAllData.jsp").forward(request, response);
}
jsp页面
<%
Object o = session.getAttribute("alldata");
%>
<%System.out.println("前台页面"+o);%>
<% if(o!=null){
List <TestData> list = (List<TestData>)o;
%>
<% System.out.println("前台页面list:"+list);%>
<%
String type = request.getParameter("selects");
String content = request.getParameter("content");
TestDataService service = new TestDataService();
list = service.getAllDatas(type, content);
%>
<% System.out.println("前台页面list:"+list);%>
<%
for(int i = 0;i<list.size();i++){
TestData testData = list.get(i);
%>
<tr>
<td align="center"><%=i+1 %></td>
<td><%=testData.getProjectname()%></td>
<td><%=testData.getTestnum() %> </td>
<td><%=testData.getCustomname() %> </td>
<td><%=testData.getTestname() %> </td>
<td><%=testData.getData1() %></td>
<td><%=testData.getData2()%> </td>
<td><%=testData.getData3()%> </td>
<td><%=testData.getData4()%> </td>
<td><%=testData.getData5()%> </td>
<td><%=testData.getData6()%> </td>
<td><%=testData.getData7()%> </td>
<td><%=testData.getData8()%> </td>
</tr>
<%} %>
</table>
<% } %>