写一个可以连接到数据库的todolist

一个可以连接到数据库的todolist

package com.westos.todo;

public class todolist {
	private Integer id;
	private String type;
	private String direction;
	private String content;

	public todolist() {
	}

	public todolist(String type, String direction, String content, Integer id) {
		super();
		this.id = id;
		this.type = type;
		this.direction = direction;
		this.content = content;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getId() {
		return id;
	}

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

	@Override
	public String toString() {
		return "todolist [id=" + id + ", type=" + type + ", direction=" + direction + ", content=" + content + "]";
	}

}

数据库连接:

package com.westos.util;

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

public class Util {
	private static String DRIVERCLASS = "com.mysql.jdbc.Driver";
	private static String URL = "jdbc:mysql://localhost/javaweb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
	private static String USERNAME = "root";
	private static String PASSWORD = "123456";

	public static Connection getConnection() {
		try {
			Class.forName(DRIVERCLASS);
			Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			return conn;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

service

package com.westos.todo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.westos.util.Util;

public class TodoService {

	public boolean save(Integer id,String type,String direction,String content){
	        String sql="";
	        Connection conn= Util.getConnection();

	        PreparedStatement ps = null;
	        sql="insert into todolist(id,type,direction,content) values(?,?,?,?)";
	        try{
	            ps=conn.prepareStatement(sql);
	            ps.setInt(1,id);
	            ps.setString(2,type);
	            ps.setString(3, direction);
	            ps.setString(4, content);
	            boolean result = ps.execute();
	            if(!conn.getAutoCommit()){
	                conn.commit();
	            }

	            return result;
	        }catch (Exception ex){
	            ex.printStackTrace();
	            return false;
	        }finally {
	            try{
	                if(ps!=null){ps.close();}
	                if(conn!=null){conn.close();}

	            }catch (Exception ex){
	                ex.printStackTrace();
	            }

	        }
	    }
	 public List> list(){
	        String sql="";
	        sql="select id,type,direction,content from todolist order by id asc";
	        Connection conn=Util.getConnection();
	        List> result=new ArrayList<>();
	        PreparedStatement ps = null;
	        ResultSet rs=null;
	        try{
	            ps=conn.prepareStatement(sql);
	            rs = ps.executeQuery();

	            while (rs.next()){
	                Map amap=new HashMap();
	                amap.put("id",rs.getInt("id"));
	                amap.put("type",rs.getString("type"));
	                amap.put("direction",rs.getString("direction"));
	                amap.put("content",rs.getString("content"));
	                result.add(amap);
	            }
	            return result;
	        }catch (Exception ex){
	            ex.printStackTrace();
	        }finally{
	            try{
	                if(rs!=null){rs.close();}
	                if(ps!=null){ps.close();}
	                if(conn!=null){conn.close();}

	            }catch (Exception ex){
	                ex.printStackTrace();
	            }
	        }
	        return result;
	}

}

servlet

package com.westos.todo;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;

public class TodoServlet extends HttpServlet{
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			 req.setCharacterEncoding("utf-8");
		        resp.setCharacterEncoding("utf-8");
		        String action=req.getParameter("action");
		        TodoService todoService=new TodoService();
		        if(StringUtils.equalsIgnoreCase("save",action)){
		            String id=req.getParameter("id");
		            String type=req.getParameter("type");
		            String direction=req.getParameter("direction");
		            String content=req.getParameter("content");
		            boolean result = todoService.save(Integer.valueOf(id), type,direction,content);
		        }else if(StringUtils.equalsIgnoreCase("listjson",action)){
		        	
		            List> result = todoService.list();
		            JSONArray jsonArray=new JSONArray();
		            jsonArray.addAll(result);
		            resp.getWriter().print(jsonArray.toJSONString());
		        }
		        else{
		            List> result = todoService.list();
		            req.setAttribute("list",result);
		        }

		}
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
			doPost(req, resp);
		}
}

html

  
  
  
    todolist  
  
  
  
  

  
  
  

{{title}}

序号
类型:
方向:
内容:

序号

类型

方向

内容

{{item.id}} {{item.type}} {{item.direction}} {{item.content}}

html页面样式

写一个可以连接到数据库的todolist_第1张图片

数据库显示

写一个可以连接到数据库的todolist_第2张图片

保存完毕后如果不刷新会出现如下现象,还未解决

写一个可以连接到数据库的todolist_第3张图片




你可能感兴趣的:(写一个可以连接到数据库的todolist)