java的制作"时间账本"

一直以来我都感觉自己的时间过得好荒废啊,貌似只是打开了一个网页链接的时间,一个下午便过去了;仿佛就是看了看空间,刷了刷微信,一天就过去了。哈,当然这是夸张的说法。但是我仔细地算了一下,大概我们每个人每天使用手机16个小时,其中接近百分之百的时间在无聊的翻来看去,也许我们拿起手机的一刹那想到的是”我就刷几分钟微博,看几条QQ信息“,但是现实呢,现实就是我们本来要拿起手机看时间,却忘记了去看时间…鉴于此,也为了更好的约束自己,掌控自己的时间,我就自己开发了这么一款简易的时间账本,来记录我的时间的走向。
我的整体的思路是采用MVC模型搭建一个框架,每一部分分别为完成相应的功能,下面就直接上代码吧。
1、bean层,也就是模型层:
记得要实现序列化接口啊,否则你会发现意想不到的错误

package com.tiger.bean;

import java.io.Serializable;

public class beanUtils implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private String title;
    private String time;


    public beanUtils(int id,String title,String time){
        this.id=id;
        this.time=time;
        this.title=title;
    }
    @Override
    public String toString() {
        return "beanUtils [id=" + id + ", title=" + title + ", time=" + time
                + "]";
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public beanUtils() {
        // TODO Auto-generated constructor stub
    }

}

2、然后是数据库操作层,这也是控制层的一部分:
数据库的建库建表的sql语句如下:

create database tiger;
use tiger;
create table timebill(
id int(10) primary key auto_increment,
title varchar(24),
time varchar(20)
);
package com.tiger.jdbcUtils;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.tiger.bean.beanUtils;

public class JDBCUtils {

    private static String DRIVER = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/tiger";
    private static String username = "****";//这个就不说了哈
    private static String password = "*****";//密码好像也不能说
    static Connection conn = null;

    static PreparedStatement ptst;

    public JDBCUtils() {
        // TODO Auto-generated constructor stub
    }

    public static Connection getConnection(String DRIVER,String url, String username,
            String password) {
        try {
            Class.forName(DRIVER);
            conn = (Connection) DriverManager.getConnection(url, username,
                    password);
            System.out.println("Succeed!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public static String Insert(beanUtils bean) throws SQLException{
        String sql="insert into tiger.timebill values(?,?,?)";
        conn=(Connection) getConnection(DRIVER, url, username, password);
         ptst=(PreparedStatement) conn.prepareStatement(sql);
         int id=bean.getId();
         String title=bean.getTitle();
         String time=bean.getTime();
         ptst.setInt(1, id);
         ptst.setString(2, title);
         ptst.setString(3, time);
        if(ptst.execute()){
            return "数据插入成功!";
        }else{
            return "数据插入失败!";
        }
    }

    public static List<beanUtils> getResult(String sql) throws SQLException {
        List<beanUtils>list=new ArrayList<beanUtils>();
        ptst=(PreparedStatement) getConnection(DRIVER, url, username, password).prepareStatement(sql);
        if(ptst.execute()){
            ResultSet result=ptst.executeQuery();
            while(result.next()){
                String temp="";
                beanUtils bean=new beanUtils();
                bean.setId(result.getInt("id"));
                bean.setTitle(result.getString("title"));
                bean.setTime(result.getString("time"));
                list.add(bean);
                temp=null;
            }
        }else{
            return null;
        }

        return list;
    }

    public static String getResultByString(ArrayList<beanUtils> list){
        String result="";
        for(int i=list.size()-1;i>=0;i--){
            result+=list.get(i);
            result+="\n";
        }
        return result;
    }

}

3、貌似就剩一个视图层咯,虽然我这个View里面写的代码很混乱,而且没有加什么注释,嘿嘿,先凑活看吧:

package doMain;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.tiger.bean.beanUtils;
import com.tiger.jdbcUtils.JDBCUtils;

public class Frame extends JFrame {

    private static TextField tf;
    private static Button begin, stop;
    private static long currentTime, endTime;
    private static TextArea ta;
    private static int minute, second, day, year, m;
    private static int SQL_id = 0;
    static JLabel label = new JLabel("任务一经开始便不能暂停!请三思而后行!");

    public void init() {
        this.setSize(500, 400);
        this.setVisible(true);
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        tf = new TextField(28);
        ta = new TextArea();
        begin = new Button("开始计时");
        stop = new Button("结束任务");

        MyListener listener = new MyListener();
        begin.addActionListener(listener);
        stop.addActionListener(listener);
    }

    public void setFrame() {
        Panel panel = new Panel();
        panel.setLayout(new FlowLayout());
        panel.add(new Label("Title:"));
        panel.add(tf);
        panel.add(begin);
        panel.add(stop);
        this.add(panel);
        this.add(ta, BorderLayout.CENTER);
    }

    public Frame() {
        init();
        setFrame();

        this.add(label, BorderLayout.NORTH);
        this.setResizable(false);
    }

    public String getTime() {
        Date d = new Date();
        return d.toString();
    }

    class MyListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getActionCommand().equals("开始计时")) {
                currentTime = System.currentTimeMillis();
                System.out.println("已经开始计时了!开始时间:" + getTime());
                label.setText("任务一经开始便不能暂停!请三思而后行!");
                Date date=new Date();
                DateFormat format=DateFormat.getDateInstance();
                label.setText(label.getText()+"\n"+format.format(date));
            }

            if (e.getActionCommand().equals("结束任务")) {
                if (ta.getText().equals("")) {
                    label.setText("你还没有在标题栏输入信息,所以我不会开始计时的!");
                }
                    endTime = System.currentTimeMillis();
                    System.out.println("任务已经停止了,结束时间:" + getTime());
                    Date date=new Date();
                    DateFormat format=DateFormat.getDateInstance();
                    label.setText("任务一经开始便不能暂停!请三思而后行!"+"\n"+format.format(date));
                    String title = tf.getText();
                    SQL_id++;
                    String time = String
                            .valueOf(((endTime - currentTime) / 60000) + "分钟"+(endTime - currentTime) / 1000) + "秒";
                    beanUtils bean = new beanUtils(SQL_id, title, time);
                    JDBCUtils jdbc = new JDBCUtils();
                    try {
                        System.out.println(jdbc.Insert(bean));
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    String sql = "select * from tiger.timebill";
                    try {
                        ArrayList<beanUtils> list = (ArrayList) jdbc
                                .getResult(sql);
                        ta.setText(((JDBCUtils) jdbc).getResultByString(list));
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
            }
        }

    }

}

4、那么来一场轰轰烈烈的测试吧:

package com.tiger.test;

import doMain.Frame;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Frame();
    }
}

下面是完成的效果展示图:
额,下边的日期对应的分钟秒数貌似没出现哦
略水略水,勿喷哈。

好了来个总结吧,也为了今后我自己复习的时候更快的理解,软件使用的就是MVC的思想,将代码和实现分离开来,这也体现了java的面向对象的思想,万事万物皆对象嘛。然后就是我的收获,我是真的感受到了模型的威力了,让复杂的代码变得格外的清晰,这就体现在了我的那个List《beanUtils》和数据库的语句上了,一下子就让方法的参数列表变得苗条了。另外一个功能就是使用了栈的思想,FILO,最新的事件展示在最上层。
缺点:软件本身的缺点更加的明显,先不说界面不是很好看吧,然后就是代码书写的不够规范,这对我今后的发展并没有帮助,希望大家引以为戒。再就是数据库层面没有考虑好,貌似只能我一个人用,需要读者单独建库建表。希望读者不要学习我的这些缺点,取精华弃糟粕。让我么共同进步吧!

/*
其实我一开始并没有想太多,就是单纯的想做一个使用的工具,仅此而已。但是后来依次偶然的机会,我想到了可以结合报表统计的知识,将我的数据库中每天的时间流向做个数据分析,就会变得更好吧。嘿嘿,惭愧的是,一直没时间来实现这个。如果你们有兴趣,不妨给我留言哦。
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分析一下这个json {&quot;name&quot;:&quot;12312&quot;,&quot;project_id&quot;:&quot;87156&quot;,&quot;project_name&quot;:&quot;上上下下左左右右baba与聚法科技(长春)有限公司与公司、证券、保险、票据等有关的民事纠纷&quot;,&quot;client&quot;:&quot;[{&quot;type&quot;:&quot;自然人&quot;,&quot;customer_id&quot;:&quot;80236&quot;,&quot;customer_name&quot;:&quot;上上下下左左右右baba&quot;}]&quot;,&quot;sign_date&quot;:&quot;2023-06-06&quot;,&quot;expire_date&quot;:&quot;2023-06-21&quot;,&quot;subject_amount&quot;:&quot;123&quot;,&quot;contract_amount&quot;:&quot;123&quot;,&quot;charge_method&quot;:&quot;一次性,分阶段,风险,计时&quot;,&quot;equity_amount&quot;:&quot;13811&quot;,&quot;amount_info&quot;:&quot;[{&quot;type&quot;:&quot;一次性&quot;,&quot;pay_date&quot;:&quot;2023-07-03&quot;,&quot;charge_amount&quot;:&quot;12&quot;},{&quot;type&quot;:&quot;分阶段&quot;,&quot;pay_date&quot;:&quot;2023-06-13&quot;,&quot;charge_amount&quot;:&quot;123&quot;,&quot;is_satisfy&quot;:&quot;是&quot;,&quot;pay_condition&quot;:&quot;12312&quot;},{&quot;type&quot;:&quot;风险&quot;,&quot;pay_date&quot;:&quot;&quot;,&quot;charge_amount&quot;:&quot;&quot;,&quot;is_satisfy&quot;:&quot;是&quot;,&quot;pay_condition&quot;:&quot;123&quot;,&quot;basic_amount&quot;:&quot;123&quot;,&quot;risk_amount&quot;:&quot;12&quot;,&quot;object_amount&quot;:&quot;123123&quot;,&quot;object&quot;:&quot;赔偿金&quot;,&quot;risk_prop&quot;:&quot;13213&quot;,&quot;member&quot;:&quot;&quot;,&quot;rate&quot;:&quot;&quot;,&quot;hours&quot;:&quot;&quot;},{&quot;type&quot;:&quot;计时&quot;,&quot;member_id&quot;:&quot;392159&quot;,&quot;member&quot;:&quot;曹野&quot;,&quot;rate&quot;:&quot;11&quot;,&quot;hours&quot;:&quot;1231&quot;}]&quot;,&quot;seal_person&quot;:&quot;123&quot;,&quot;seal_type&quot;:&quot;律所公章,法人名章,财务章&quot;,&quot;seal_num&quot;:&quot;123&quot;,&quot;file_path&quot;:&quot;[{&quot;title&quot;:&quot;导入错误数据 (15).xls&quot;,&quot;path&quot;:&quot;382585/1686381522542/导入错误数据 (15).xls&quot;,&quot;size&quot;:&quot;91136&quot;},{&quot;title&quot;:&quot;3.txt&quot;,&quot;path&quot;:&quot;382585/1686561731102/3.txt&quot;,&quot;size&quot;:44078}]&quot;,&quot;remark&quot;:&quot;123123&quot;} 并使用php转换成字符串
最新发布
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值