1. 程式人生 > >記賬本----1

記賬本----1

https response type bsp rate getattr 功能 .sql quest

今天就做了一個簡單的主頁面。實現了基本的記賬功能。計劃明天實現刪除指定的記賬事件。

<%@ 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);
            }
        }
}

最後的結果顯示如下

技術分享圖片

技術分享圖片

記賬本----1