mvc分页、用户登录

之前在单独写crud功能时已经实现了相应的dao代码,直接拿过来用了。

package dao;
  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.novel;
  
public class noveldao {
  
    public noveldao() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
  
    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8", "root",
                "123456");
    }
  
    public int getTotal() {
    	
    	int cnt = 0;
        
    	try (Connection c = getConnection(); Statement s = c.createStatement();) {
  
            String sql = "select count(*) from novel";
            ResultSet rs = s.executeQuery(sql);
            
            
            while (rs.next()) {
                cnt = rs.getInt(1);
            }
        } catch (SQLException e) {
  
            e.printStackTrace();
        }
        return cnt;
    }
  
    public void add(novel character) {
  
        String sql = "insert into novel values(null,?,?,?)";
        try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) 
        {
            ps.setString(1, character.name);
            ps.setString(2, character.area);
            ps.setString(3, character.task);
  
            ps.execute();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
  
    public void update(novel character) {
  
        String sql = "update novel set name= ?, area = ? , task = ? where id = ?";
        try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
  
            ps.setString(1, character.name);
            ps.setString(2, character.area);
            ps.setString(3, character.task);
            ps.setInt(4, character.id);
            ps.execute();
  
        } catch (SQLException e) {
            e.printStackTrace();
        }
  
    }
  
    public void delete(int id) {
  
        try (Connection c = getConnection(); Statement s = c.createStatement();) {
  
            String sql = "delete from novel where id = " + id;
  
            s.execute(sql);
  
        } catch (SQLException e) {
  
            e.printStackTrace();
        }
    }
  
    public novel get(int id) {
        novel character = null;
  
        try (Connection c = getConnection(); Statement s = c.createStatement();) {
  
            String sql = "select * from novel where id = " + id;
  
            ResultSet rs = s.executeQuery(sql);
  
            if (rs.next()) {
            	character = new novel();
                
            	String name = rs.getString(2);
                String area = rs.getString(3);
                String task = rs.getString(4);
                
                character.id = id;
                character.name = name;
                character.area = area;
                character.task = task;
            }
  
        } catch (SQLException e) {
  
            e.printStackTrace();
        }
        return character;
    }
  
    public List list() {
        return list(0, Short.MAX_VALUE);
    }
  
    public List list(int start, int count) {
        List characters = new ArrayList();
  
        String sql = "select * from novel order by id desc limit ?,? ";
  
        try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
  
            ps.setInt(1, start);
            ps.setInt(2, count);
  
            ResultSet rs = ps.executeQuery();
  
            while (rs.next()) {
            	novel character = new novel();
                
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String area = rs.getString(3);
                String task = rs.getString(4);
                
                character.id = id;
                character.name = name;
                character.area = area;
                character.task = task;
                characters.add(character);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return characters;
    }
  
}

在servlet界面直接调用dao的相关方法即可,设置好起始参数。

mvc分页、用户登录_第1张图片

 mvc分页、用户登录_第2张图片

 然后我们再给jsp页面加上翻页的超链接,并在servlet页面做好数据处理。代码思路很简单,就是每执行一次翻页操作我们就相应的更新好起始位置,再将更新好的数据传给jsp页面。接着我们点击jsp页面的按钮之前更新好的数据就会传回来,我们拿着这个参数就可以显示下一页数据,如此循环往复。

mvc分页、用户登录_第3张图片

mvc分页、用户登录_第4张图片 

mvc分页、用户登录_第5张图片 

 首页跟尾页的原理与上面类似,不过尾页的参数需要我们特判一下,用到了java的除法向下取整的特性。至于边界问题可以按照教程上的三元表达式直接写或者if语句都可以。

mvc分页、用户登录_第6张图片

mvc分页、用户登录_第7张图片 mvc分页、用户登录_第8张图片

 然后按照要求套上bootstrap,再用css调一下细节

mvc分页、用户登录_第9张图片

 最后我们借助session实现一个用户登录功能,网站并没有给你登录界面的html,记得自己写一个。然后我们自己再写一个servlet用来负责接受session的数据

mvc分页、用户登录_第10张图片

mvc分页、用户登录_第11张图片 

 

 

 

 

你可能感兴趣的:(web应用,mvc,sql,数据库)