實現搜索框查詢功能在頁面顯示(數據庫中讀取)


搞了半天,才出來效果。不說了直接上圖

如上圖所示,從數據庫中查詢出來的數據中通過搜索框進行模糊匹配查詢,找到指定的字段數據信息。

下面給出相關的代碼:

jsp代碼:

<body>
 <%
UserServiceImpl usi=new UserServiceImpl();
try{
String name=request.getParameter("search");
if(name==null){
    name="";
}
List<User> all=usi.findName(name);
Iterator<User> iter=all.iterator();
%>  
    <div class="topnav">
        <a>請輸入要查詢的信息</a>
        <div class="search-container">
           <form id="searchForm">
              <input type="text" placeholder="搜索..." name="search">
              <button type="submit">提交</button>
           </form>
        </div>     
    </div>

    <div style="width: 98%; height: 420px;margin:auto; overflow: auto;margin-top:20px;">
        <table>
            <thead>
                <tr class="mytr">
                    <td><input type="checkbox" name="checkall"></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('name')">姓名</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('age')">年齡</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('nationality')">國籍</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('sex')">性別</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('birth')">生日</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('phone')">電話號碼</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('idcard')">身份證</a></td>
                    <td><a href="javascript:void(0)"
                        onclick="showListOrder('address')">地址</a></td>
                </tr>
            </thead>
            <tbody>
                <%-- <c:forEach items="${list}" var="list"> --%>
                <%
                while(iter.hasNext()){
                    User user=iter.next();
                %>
                    <tr>
                        <td><input type="checkbox" value="<%=user.getId() %>" name="checkone"></td>
                        <td style="word-wrap: break-word; width: 70px;"><%=user.getName() %></td>
                        <td><%=user.getAge() %></td>
                        <td><%=user.getNationality() %></td>
                        <td><%=user.getSex() %></td>
                        <td><%=user.getBirth() %></td>
                        <td><%=user.getPhone() %></td>
                        <td><%=user.getIdcard() %></td>
                        <td><%=user.getAddress() %></td>
                    </tr>
                <%
                } 
                %>
            </tbody>
        </table>
    </div>

        <%
}catch(Exception e){
    e.printStackTrace();
}
       %> 
</body>

Dao層Impl:

@Override
    public List<User> findName(String name) throws Exception {
        List<User> all=new ArrayList<User>();
        Connection con=DBUtil.getConnection();
        PreparedStatement ps=con.prepareStatement("select * from User where name like ?");
        ps.setString(1, "%"+name+"%");
        ResultSet rs=ps.executeQuery();
        User user=null;
        while(rs.next()){
            user=new User();
            user.setId(rs.getInt(1));
            user.setName(rs.getString(2));
            user.setAge(rs.getInt(3));
            user.setNationality(rs.getString(4));
            user.setSex(rs.getString(5));
            user.setBirth(rs.getDate(6));
            user.setPhone(rs.getString(7));
            user.setIdcard(rs.getString(8));
            user.setAddress(rs.getString(9));
            all.add(user);
        }
        ps.close();
        DBUtil.close(con);
        return all;
    }

service層:

@Service
public class UserServiceImpl implements UserService{
    
    @Resource
    private UserDao userDao;

    @Override
    public List<User> findName(String name) throws Exception {
        UserDaoImpl dao=new UserDaoImpl();
        List<User> all=dao.findName(name);
        return all;
    }

}

DBUtil包(獲取數據庫連接):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {
    //連接數據庫
    public static Connection getConnection(){
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = 
                DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sertest?useUnicode=true&characterEncoding=utf8",
                "root","123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    } 
    
    //關閉連接
    public static void close(Connection con){
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

其中主要部分核心代碼基本附上!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM