使用DAO / servlet中的form / parameter过滤数据

时间:2012-03-16 16:52:24

标签: java jsp dao

我有这个代码......

ifsDAO.java

package source;

import java.sql.*;
import java.sql.SQLException;
import java.util.*;


public class ifsDAO {

private Database database;

public ifsDAO(Database database) {
this.database = database;

}

public List<ifsBean> list() throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<ifsBean> ifs_array = new ArrayList<ifsBean>();



try {

connection = database.getConnection();
statement = connection.prepareStatement("select * from emp_details");
resultSet = statement.executeQuery();
while (resultSet.next()) {
ifsBean arraylist = new ifsBean();
arraylist.setFname(resultSet.getString("fname"));
arraylist.setSname(resultSet.getString("sname"));
arraylist.setTown(resultSet.getString("town"));
ifs_array.add(arraylist);
}
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}

return ifs_array;
}
}

ifsBean.java

package source;


public class ifsBean {

private String fname;
private String sname;
private String town;

public String getFname() { return fname; }
public String getSname() { return sname; }
public String getTown() { return town; }

public void setFname(String fname) { this.fname = fname; }
public void setSname(String sname) { this.sname = sname; }
public void setTown(String town) { this.town = town; }
}

Database.java

package source;

import java.sql.*;

public class Database {
private String url;
private String username;
private String password;

public Database(String driver, String url, String username, String password) {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Driver class is missing in classpath", e);
}
this.url = url;
this.username = username;
this.password = password;
}

public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
}

ifsServlet.java

package source;

import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;


public class ifsServlet extends HttpServlet {

private ifsDAO ifsDAO;


@Override
public void init() throws ServletException {
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost/mydb";
String username = "user";
String password = "pass";


Database database = new Database(driver, url, username, password);
this.ifsDAO = new ifsDAO(database);
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {
List<ifsBean> ifs_array = ifsDAO.list();

request.setAttribute("ifs_array", ifs_array);
request.getRequestDispatcher("ifslist.jsp").forward(request, response);
} catch (SQLException e) {
throw new ServletException("Cannot retrieve areas", e);
}
}
}

ifslist.java

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello</h1>
<table>
<c:forEach items="${ifs_array}" var="array">
<tr>
<td>${array.fname}</td>
<td>${array.sname}</td>
<td>${array.town}</td>

</tr>
</c:forEach>



</table>

</body>
</html>

我可以显示所有数据并且效果很好

但是如何使用过滤器或链接和参数来过滤结果...

示例:www.example.com?id =参数

抱歉,我对此非常陌生......并且学习......

1 个答案:

答案 0 :(得分:1)

您的问题有点广泛,但这里有一些提示:

  • 我假设您知道如何使用额外的?name=parameter向同一个servlet执行另一个GET请求。

  • 在servlet的doGet()方法中,您需要获取name参数的值:

    String name = request.getParameter("name");
    

    如果null参数不存在,则会返回name=

  • 将您的name传递给DAO图层。您需要list()方法的额外参数:

    List<ifsBean> ifs_array = ifsDAO.list(name);
    
  • 在您的DAO中,您需要利用此参数:

    connection.prepareStatement("select * from emp_details WHERE fname = ?");
    

    然后,您需要将name传递给查询,有关详细信息,请参阅Using Prepared Statements

就是这样!您的代码使用非常低级别的结构,但除此之外看起来还不错。