记账本----1

      今天就做了一个简单的主页面。实现了基本的记账功能。计划明天实现删除指定的记账事件。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
<style>
    .a{
        font-size: 26px;
        margin-top: 20px;
    }
</style>
</head>
<body>
<div align="center">
        <h1 style="color:bluegreen;">记帐本</h1>
        <div class="a">
            <a href="add.jsp">账单记录</a>
        </div>
        <div class="a">
            <a href="add.jsp">删除账单</a>
        </div>
        <div class="a">
            <a href="add.jsp">查询账单</a>
        </div>
        <div class="a">
            <a href="add.jsp">修改账单</a>
        </div>
</div>
</body>
</html>

这是我的主页显示------

添加账单记录的代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>记录账单</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">记账</h1>
        
        <form action="BillServlet?method=add" method="post" onsubmit="return check()">
            <div class="a">
                类型<input type="text" id="type" name="type"/>
            </div>
            <div class="a"><input type="text" id="year" name="year" />
            </div>
            <div class="a"><input type="text" id="month" name="month"/>
            </div>
            <div class="a"><input type="text" id="day" name="day"/>
            </div>
            <div class="a">
                收入<input type="text" id="income" name="income"/>
            </div>
            <div class="a">
                支出<input type="text" id="pay" name="pay"/>
            </div>
            
            <div class="a">
                <button type="submit" class="b">注&nbsp;&nbsp;&nbsp;册</button>
            </div>
            <div class="a">
            <a href="index.jsp" >返回</a>
            </div>
        </form>
    </div>
    
</body>
</html>

连接数据库

package com.bill.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false";
    public static String db_user = "root";
    public static String db_pass = "mm123456";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    

    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

been层的封装

package com.bill.been;

public class Bill {
    private int id;
    private String type;
    private String year;
    private String month;
    private String day;
    private String income;
    private String pay;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getMonth() {
        return month;
    }
    public void setMonth(String month) {
        this.month = month;
    }
    public String getDay() {
        return day;
    }
    public void setDay(String day) {
        this.day = day;
    }
    public String getIncome() {
        return income;
    }
    public void setIncome(String income) {
        this.income = income;
    }
    public String getPay() {
        return pay;
    }
    public void setPay(String pay) {
        this.pay = pay;
    }
    
    public Bill(int id,String type,String year,String month,String day,String income,String pay)
    {
        this.id=id;
        this.type=type;
        this.year=year;
        this.month=month;
        this.day=day;
        this.income=income;
        this.pay=pay;
    }
    public Bill(String type,String year,String month,String day,String income,String pay)
    {
        this.type=type;
        this.year=year;
        this.month=month;
        this.day=day;
        this.income=income;
        this.pay=pay;
    }
}

dao层调用数据库

package com.bill.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import com.bill.util.DBUtil;

import com.bill.been.Bill;
@SuppressWarnings("unused")
public class BillDao {
    public boolean add(Bill bill) {
        String sql = "insert into bill(type,year,month,day,income,pay) values('" + bill.getType() + "','" + bill.getYear() + "','"+bill.getMonth()+"','"+bill.getDay()+"','"+bill.getIncome()+"','"+bill.getPay()+"')";
        Connection conn = DBUtil.getConn();//调用方法连接数据库
        Statement state = null;
        boolean f = false;
        int a = 0 ;
        
        try {       //监视大括号内的代码
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (Exception e) {     //捕获错误
            e.printStackTrace();
        } finally {
            //关闭z    连接
            DBUtil.close(state, conn);
        }
        
        if (a > 0) {
            f = true;
        }
        return f;
    }
    
    
    
}

servlet实现页面和数据库的传递

package com.bill.servlet;
import java.io.IOException;

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


import com.bill.dao.BillDao;

import com.bill.been.Bill;
@WebServlet("/BillServlet")
public class BillServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    
    
    public BillServlet() {
        super();
    }
    BillDao dao=new BillDao();
    
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {
            add(req, resp);
        }  
    }
        private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            // TODO Auto-generated method stub
            String type = req.getParameter("type");
            String year = req.getParameter("year");
            String month = req.getParameter("month");
            String day = req.getParameter("day");
            String income = req.getParameter("income");
            String pay = req.getParameter("pay");
            
            Bill bill=new Bill(type,year,month,day,income,pay);
            if(dao.add(bill)) {
                req.setAttribute("message", "保存成功!");
                req.getRequestDispatcher("add.jsp").forward(req, resp);
            }else {
                req.setAttribute("message", "保存失败!");
                req.getRequestDispatcher("add.jsp").forward(req, resp);
            }
        }
}

最后的结果显示如下

 

猜你喜欢

转载自www.cnblogs.com/birdmmxx/p/10386267.html