Hi all,

How to display search results from MYSQL database in JSP page??

is it possibile to create gridview in jsp page?

please help

Dani AI

Generated

asked how to show MySQL search results in a JSP. ’s quick scriptlet demonstrates the basic idea, and correctly warns against putting DB logic in the JSP. Below is a compact, practical pattern that keeps logic in a servlet/DAO, returns a list of beans, and uses the JSP only for rendering — safe, testable, and easy to style like a grid.

Use a DAO method that runs a PreparedStatement and returns a List of simple beans:

public List<User> searchUsers(Connection con, String q) throws SQLException {
    String sql = "SELECT uname, regno FROM emptbl WHERE uname LIKE ?";
    List<User> list = new ArrayList<>();
    try (PreparedStatement ps = con.prepareStatement(sql)) {
        ps.setString(1, "%" + q + "%");
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                list.add(new User(rs.getString("uname"), rs.getString("regno")));
            }
        }
    }
    return list;
}

Set the returned list as a request attribute in your servlet/controller, then forward to a JSP that renders the rows with JSTL/EL (no scriptlets):

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<table>
  <tr><th>Username</th><th>RegNo</th></tr>
  <c:forEach var="u" items="${results}">
    <tr><td>${u.uname}</td><td>${u.regno}</td></tr>
  </c:forEach>
</table>

Notes and cautions: always use PreparedStatement to avoid SQL injection; close resources (try-with-resources shown); run DB connections from a pooled DataSource (JNDI) for production; escape or validate any user-provided data before rendering to avoid XSS; handle empty result sets and add pagination for large result sets. If you want a richer “grid” (sorting, paging, client filters), layer a client-side table library on the JSP output rather than embedding business logic in the view. This keeps the design clean and aligns with the points raised by and .

Recommended Answers

All 5 Replies

<%

   try{
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        con=DriverManager.getConnection("jdbc:derby://localhost:1527/Employee");
       stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery("select uname,regno from emptbl");

        while(rs.next()){
            %>
            <table>
            <tr>
                <td>rs.getString(1)</td>  // 1 indicates coloum number(uname)
                <td>rs.getString(2)</td>
            </tr>   
            </table>

            <%
            } 
      }
     catch(Exception e)
             {
         }
      finally
              {
              try {
                   stmt.close();

                con.close();
            }

         catch (Exception ex) {

        }
       }

        %>

thanku for ur codes..

html table will be the perfect choice, but dont code logic in jsp ,it just view purpose,
check this link

datagird option is possible?

It can be easily rendered using JSTL for loop tag
but still you have option like that check this link

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.