servlet-crud(一)1.5小时

为了起到代码练习效果,这次的代码纯由自己书写,顺便起到复习java基本语法的作用。

1、准备实体类novel

servlet-crud(一)1.5小时_第1张图片

2、准备dao类

封装好增删改查显示等方法,便于以后调用。主要用的就是sql语句、Statement、PreparedStatement的相关方法实现方法。

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 ++;
            }
        } catch (SQLException e) {
  
            e.printStackTrace();
        }
        return cnt;
    }
  
    public void add(novel character) {
  
        String sql = "insert into hero 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 hero set name= ?, hp = ? , damage = ? 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 hero 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 hero 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 hero 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;
    }
  
}

3、编写显示表的servlet

package servlet;
 
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.novel;
import dao.noveldao;
 
public class LList extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
      
        List characters = new noveldao().list();
 
        StringBuffer sb = new StringBuffer();
        sb.append("\r\n");
        sb.append("\r\n");
 
        String trFormat = "\r\n";
 
        for (novel n : characters) {
            String tr = String.format(trFormat, n.getid(), n.getname(), n.getarea(), n.gettask());
            sb.append(tr);
        }
 
        sb.append("
idnameareatask
%d%s%s%s
"); response.getWriter().write(sb.toString()); } }

然后配置web

servlet-crud(一)1.5小时_第2张图片

到这一步应该完成任务了,结果运行之后显示

servlet-crud(一)1.5小时_第3张图片 

 调试了很久都没有解决,明天继续

你可能感兴趣的:(web应用,java,spring,开发语言)