Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

sql server - displaying multiple records by using resultset

how to display multiple records from database having the same field value by using result set statement

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First create a Javabean class which represents one row of the table. I have no idea what data you're talking about, but let's take an User as real world example:

public class User {
    private Long id;
    private String name;
    private Integer age;
    // Add/generate public getters and setters.
}

The above is of course an example. You need to name the class and the properties accordingly what the actual data represents.

Now create DAO class which does the desired database interaction task with help of JDBC. You only need to ensure that you have the correct SQL Server JDBC driver in the classpath. I can recommend jTDS for this as it is much better and faster than Microsoft's own JDBC drivers. OK, let's assume that you want to list all Users which have the same age:

public List<User> listByAge(Integer age) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    List<User> users = new ArrayList<User>();

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement("SELECT id, name, age FROM user WHERE age = ?");
        statement.setInt(1, age);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            User user = new User();
            user.setId(resultSet.getLong("id"));
            user.setName(resultSet.getString("name"));
            user.setAge(resultSet.getInt("age"));
            users.add(user);
        }
    } 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 users;
}

Now create a Servlet class UsersServlet which does the preprocessing of the data in the doGet() method.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<User> users = userDAO.list();
    request.setAttribute("users", users);
    request.getRequestDispatcher("/WEB-INF/users.jsp").forward(request, response);
}

Map this servlet in web.xml as follows:

    <servlet>
        <servlet-name>users</servlet-name>
        <servlet-class>mypackage.UsersServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>users</servlet-name>
        <url-pattern>/users</url-pattern>
    </servlet-mapping>

Note the <url-pattern>, you can execute this servlet by http://example.com/context/users.

Now create a JSP file users.jsp which you place in WEB-INF folder so that nobody can directly access it without using the servlet. You can use JSTL c:forEach to iterate over a List:

<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Age</th></tr>
    </thead>
    <tbody>
        <c:forEach items="${users}" var="user">
            <tr><td>${user.id}</td><td>${user.name}</td><td>${user.age}</td></tr>
        </c:forEach>
    </tbody>
</table>

Execute it by http://example.com/context/users. That should be it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...