1,105,585 Community Members

filter the result using parameter Servlet-DAO

Member Avatar
japeeps
Newbie Poster
1 post since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have this Code...

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>

I can list all the results...

however how can I filter the results here using a filter or a link and parameters...

However.. in I'll create

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

You can filter the results at your query by adding a WHERE. Look for some tutorials on how to write sql statements with WHERE:

select * from emp_details WHERE fname='something'

Then at the jsp page that submits to the ifsServlet you can pass the name or whatever you want as parameter and then pass that as argument to your dao, so you can get the filtered list.

Of course it is assumed that you how to write forms that submit values in jsp.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article