实现搜索框查询功能在页面显示(数据库中读取)

搞了半天,才出来效果。不说了直接上图

实现搜索框查询功能在页面显示(数据库中读取)_第1张图片

实现搜索框查询功能在页面显示(数据库中读取)_第2张图片

如上图所示,从数据库中查询出来的数据中通过搜索框进行模糊匹配查询,找到指定的字段数据信息。

下面给出相关的代码:

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 findName(String name) throws Exception {
        List all=new ArrayList();
        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 findName(String name) throws Exception {
        UserDaoImpl dao=new UserDaoImpl();
        List 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();
            }
        }
    }
}

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

转载于:https://www.cnblogs.com/xslzwm/p/9681956.html

你可能感兴趣的:(实现搜索框查询功能在页面显示(数据库中读取))