使用 DAO/servlet 中的表单/参数过滤数据
Posted
技术标签:
【中文标题】使用 DAO/servlet 中的表单/参数过滤数据【英文标题】:filter the data using form/parameter in DAO/servlet 【发布时间】:2012-04-02 05:39:56 【问题描述】:我有这个代码...
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;
数据库.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=parameter
对不起,我对此很陌生..并且正在学习...
【问题讨论】:
【参考方案1】:您的问题有点宽泛,但这里有一些提示:
我假设您知道如何使用额外的?name=parameter
对同一个 servlet 执行另一个 GET 请求。
在你的servlet的doGet()
方法中你需要获取name
参数的值:
String name = request.getParameter("name");
如果name=
参数不存在,这将返回null
将您的 name
传递给 DAO 层。 list()
方法需要一个额外的参数:
List<ifsBean> ifs_array = ifsDAO.list(name);
在你的 DAO 中你需要利用这个参数:
connection.prepareStatement("select * from emp_details WHERE fname = ?");
然后您需要将您的name
传递给查询,请参阅Using Prepared Statements 了解详细信息。
就是这样!您的代码使用了非常低级的结构,但除此之外看起来还不错。
【讨论】:
您好 Tomasz.. 感谢您的回答.. 我按照您的步骤操作,但我在 netbeans 中遇到错误...就像我需要将字符串添加到“public Listname
传递给查询,详情请参阅Using Prepared Statements。”以上是关于使用 DAO/servlet 中的表单/参数过滤数据的主要内容,如果未能解决你的问题,请参考以下文章