jsp+servlet实现网页动态数据库查询

jsp+servlet实现网页动态数据库查询

实现思路

在网页中向服务器发送查询数据的请求,服务器启动并调用servlet,根据客户端请求响应内容并将其传给服务器,服务器再讲响应返回客户端。

实现过程

首先在IntelliJ IDEA配置好tomcat服务器,建立一个JavaWeb服务器。
前台页面设置,一个简单的超链接,超链接指向服务器servlet:



 

Servlet设置

@WebServlet({"/checkServlet"})
public class CheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List list=new JdbcDaoImpl().getInfo();//使用list接收返回来的数据,数据库连接使用德鲁伊数据库连接池+springJDBC
        req.setAttribute("list",list);//将数据返回给客户端
        req.getRequestDispatcher("showList.jsp").forward(req, resp);//返回给客户端的地址
    }
}

关于德鲁伊数据库连接池+springJDBC的具体实现可参考

https://blog.csdn.net/CharmJay/article/details/95938016

数据库连接类接口

public interface JdbcDao {
    List getInfo();
}

接口实现

public class JdbcDaoImpl implements JdbcDao {
JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDs());springjdbc连接数据库

public List getInfo() {
    String sql="select * from userInfo";//定义sql查询语句
    List list=jt.query(sql,new BeanPropertyRowMapper(User.class)); //将结果封装为user类格式放到一个list集合中

    return list;
}}

接收页面showList.jsp



    showList



    
//接收list将其遍历,把每个list里的user类的数据取出来放到下面
id 姓名 性别 年龄 籍贯 qq email 选项
${user.id} ${user.name} ${user.sex} ${user.age} ${user.born} ${user.qq} ${user.email}

实体类

public class User {
    private Integer id;
    private String name;
    private String sex;
    private String age;
    private String born;
    private String qq;
    private String email;

    public User() {
    }

    public User(Integer id, String name, String sex, String age, String address, String qq, String email) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.born = address;
        this.qq = qq;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getBorn() {
        return born;
    }

    public void setBorn(String born) {
        this.born = born;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                ", born='" + born + '\'' +
                ", qq='" + qq + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

数据库连接

public class JDBCUtils {
    public static DataSource ds;
    static {
        try {
            Properties ps=new Properties();
            InputStream is=JDBCUtils.class.getClassLoader().getResourceAsStream("durid.properties");
            ps.load(is);
            ds= DruidDataSourceFactory.createDataSource(ps);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDs(){
        return ds;
    }

    public Connection getConn() throws SQLException {
        return ds.getConnection();
    }

    public void close(){

    }
}

最后测试
测试结果

你可能感兴趣的:(JAVA)