一个可以连接到数据库的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
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页面样式
数据库显示
保存完毕后如果不刷新会出现如下现象,还未解决