java大作业家庭理财系统
java大作业家庭理财系统
java大作业,用到了swing做界面。access和odbc做数据库部分。(mysql和sqlserver崩了还没好。。。)
前端主要用swing和swt,后端用access调用sql语句。
swing部分包含确认窗口的应用等技巧。
一共有有adddate,analysis,changedate。changepw,deldate,enter,formdate。getOldPW。help,moneymanger九个模块。都很简单,代码如下。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
class AddData {
public AddData() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//安装驱动
} catch (java.lang.ClassNotFoundException e) {
System.out.println("forname: " + e.getMessage());
}
try {
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER");//连接数据库
Statement s = c.createStatement();
Integer shouru = Integer.valueOf(Moneymanager.txt1.getText());
Integer zhichu = Integer.valueOf(Moneymanager.txt2.getText());
String shijian = Moneymanager.txt3.getText();
String beizhu = Moneymanager.txt4.getText();
if (!shouru.equals(0) || !zhichu.equals(0)) {
new Show().getplayerInfo();
s.executeUpdate("insert into Moneymanager(序号,收入,支出,时间,备注) values('" + (new Show().xuhao + 1) + "','" + shouru + "','" + zhichu + "','" + shijian + "','" + beizhu + " ')");//添加数据
Moneymanager.txt1.setText("0");
Moneymanager.txt2.setText("0");
Moneymanager.txt4.setText(" ");
} else
JOptionPane.showMessageDialog(null, "请输入收入或支出 ! ");
s.close();
c.close();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
public static void main(String[] args) {
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
class Analysis extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
static JFrame f = new JFrame("财务统计");
static JLabel lb1 = new JLabel("您的余额为:");
static JLabel lb2 = new JLabel("");
static JButton bt1 = new JButton("确定");
public String getlb2() {
int i = 0, k = 0;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:odbc:MANGER"); //建立连接这里MANGER为数据库名
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from Moneymanager");//将查询得到的结果集给rs
while (rs.next()) {
i = i + rs.getInt("收入");
k = k + rs.getInt("支出");
}
} catch (SQLException e1) {
e1.getStackTrace();
}
return String.valueOf(i - k) + "元";
}
Analysis() {
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setBounds(370, 200, 300, 200); //主窗口大小
f.setLayout(null);
f.setResizable(false);
bt1.setBounds(110, 110, 60, 30);
f.add(bt1);
bt1.addActionListener(this);
lb1.setBounds(75, 27, 160, 20);
f.add(lb1);
lb1.setFont(new Font("Serif", Font.BOLD, 18));
lb2.setText(getlb2());
lb2.setBounds(120, 70, 160, 20);
f.add(lb2);
lb2.setFont(new Font("Serif", Font.BOLD, 24));
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
f.setVisible(false);
}
public static void main(String[] args) {
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class ChangeData extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public static JButton bt3 = new JButton("修改");
public static JButton bt4 = new JButton("取消");
public static JLabel lb2 = new JLabel("收入 元");
public static JLabel lb3 = new JLabel("支出 元");
public static JLabel lb4 = new JLabel("时间");
public static JLabel lb5 = new JLabel("备注");
public static JTextField txt2 = new JTextField();
public static JTextField txt3 = new JTextField();
public static JTextField txt4 = new JTextField();
public static JTextField txt5 = new JTextField();
String j=JOptionPane.showInputDialog("请输入需要修改的行序号:");//确认窗口
public Integer i=Integer.valueOf(j);
public JDialog f2 = new JDialog(Moneymanager.m.f,"修改第"+i+"行数据",true);//对话框新建窗口
public ChangeData(){
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setLayout(null);
f2.setBounds(220, 200, 600, 300);
f2.setResizable(false);
bt3.setBounds(180, 200, 60, 30);f2.add(bt3);bt3.addActionListener(this);
bt4.setBounds(350, 200, 60, 30);f2.add(bt4);bt4.addActionListener(this);
lb2.setBounds(100,60,200,40);f2.add(lb2);
lb2.setFont(new Font("Serif",Font.BOLD,18));
lb3.setBounds(315,60,200,40);f2.add(lb3);
lb3.setFont(new Font("Serif",Font.BOLD,18));
lb4.setBounds(100,90,200,40);f2.add(lb4);
lb4.setFont(new Font("Serif",Font.BOLD,18));
lb5.setBounds(100,120,250,40);f2.add(lb5);
lb5.setFont(new Font("Serif",Font.BOLD,18));
txt2.setBounds(140,70,55,20);f2.add(txt2);
txt3.setBounds(355,70,55,20);f2.add(txt3);
txt4.setBounds(140,100,100,20);f2.add(txt4);
txt5.setBounds(140, 130, 300, 20);f2.add(txt5);
if(i>new Show().xuhao||i<1){
JOptionPane.showMessageDialog(null,"没有第"+i+"行记录!!!!");f2.setVisible(false);
}
else{
txt2.setText(new Show().playerInfo[i-1][1]);//获取数据
txt3.setText(new Show().playerInfo[i-1][2]);
txt4.setText(new Show().playerInfo[i-1][3]);
txt5.setText(new Show().playerInfo[i-1][4]);
f2.setVisible(true);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt3){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}
try{
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER"); //建立连接这里MANGER为数据库名
PreparedStatement p = c.prepareStatement("UPDATE Moneymanager set 收入=?, 支出=? ,时间=? ,备注=? where 序号=?");
p.setInt(1, Integer.valueOf(txt2.getText()));
p.setInt(2, Integer.valueOf(txt3.getText()));
p.setString(3, txt4.getText()+" ");
p.setString(4, txt5.getText()+" ");
p.setInt(5, i);
p.executeUpdate();
p.close();c.close();
JOptionPane.showMessageDialog(null,"修改第"+i+"行数据成功!! ");
f2.setVisible(false);
dispose();
}catch(SQLException e2){
e2.getStackTrace();
}
}
else if(e.getSource()==bt4){//取消按钮退出
f2.setVisible(false);
dispose();
}
}
public static void main(String[] args){
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
class Changepw {
String j = JOptionPane.showInputDialog("请输入原密码:");
public Changepw(){
if(j.equals(new GetOldPW().getpw())){
String k = JOptionPane.showInputDialog("请输入新密码:");
String l = JOptionPane.showInputDialog("请确认新密码:");
if(l.equals(k)){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//安装驱动
}catch(java.lang.ClassNotFoundException e){
System.out.println("forname: " + e.getMessage());
}
try{
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER");//连接数据库
PreparedStatement s = c.prepareStatement("UPDATE pw set password=?");
s.setString(1, l);
s.executeUpdate();
s.close();
c.close();
JOptionPane.showMessageDialog(null,"密码修改成功!!");
}catch(SQLException e){
System.out.println("SQLException: "+ e.getMessage());
}
}else
JOptionPane.showMessageDialog(null,"两次输入密码不同!修改失败!!");
}else
JOptionPane.showMessageDialog(null,"密码错误!!修改失败!! ");
}
public static void main(String[] args) {
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
class DelData {
private static final long serialVersionUID = 1L;
String j = JOptionPane.showInputDialog("请输入需要删除的行序号:");
public Integer i = Integer.valueOf(j);
public DelData() {
new Show().getplayerInfo();
if (i > new Show().xuhao || i < 1) {
JOptionPane.showMessageDialog(null, "没有第" + i + "行记录!!!!");
} else {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//安装驱动
} catch (java.lang.ClassNotFoundException e) {
System.out.println("forname: " + e.getMessage());
}
try {
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER");//连接数据库
Statement s = c.createStatement();
PreparedStatement p1 = c.prepareStatement("delete from Moneymanager where 序号=?");
PreparedStatement p2 = c.prepareStatement("UPDATE Moneymanager set 序号=? where 序号=?");
p1.setInt(1, i);
p1.executeUpdate();
ResultSet rs = s.executeQuery("select * from Moneymanager");
int k = 1;
while (rs.next()) {
if (k >= i && k <= new Show().xuhao) {
p2.setInt(1, k);
p2.setInt(2, k + 1);
p2.executeUpdate();
}
k++;
}
Moneymanager.m.fresh();
int l = 0;
for (l = 0; l < 4; l++)
Moneymanager.m.table.setValueAt("", k - 1, l);
p1.close();
p2.close();
s.close();
c.close();
JOptionPane.showMessageDialog(null, "删除第" + i + "行数据成功!! ");
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
}
public static void main(String[] args) {
}
}
import java.awt.event.*;
import javax.swing.*;
public class Enter extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;//序列化类,防止更换jdk时由于jvm的变化报错
static JFrame f1 = new JFrame("家庭理财系统");
static JButton bt1 = new JButton("登入");
static JButton bt2 = new JButton("退出");
static JLabel lb1 = new JLabel("请输入密码: ");
static JPasswordField pw = new JPasswordField();
public Enter (){
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭选项卡等
f1.setLayout(null);
f1.setBounds(300, 200, 300, 200);
f1.setResizable(false);
bt1.setBounds(60, 100, 60, 30);
bt2.setBounds(160, 100, 60, 30);
lb1.setBounds(60, 30, 200, 20);
pw.setBounds(60, 60, 150, 25);
f1.add(bt1);bt1.addActionListener(this);
f1.add(bt2); bt2.addActionListener(this);
f1.add(lb1);
f1.add(pw);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String a,b;
a=new String (pw.getPassword());
b=new GetOldPW().getpw();
if(e.getSource()==bt1)
{
if(a.equals(b)){
f1.setVisible(false);
Moneymanager.m.MoneymanagerWindow();
}
else
{ pw.cut();//错误声音提示
JOptionPane.showMessageDialog(null,"密码错误,请重新输入密码 ! ");
}
}
else if(e.getSource()==bt2){
System.exit(0);
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
class FormartData {
public FormartData(){
int r=new Show().xuhao;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//安装驱动
}catch(java.lang.ClassNotFoundException e){
System.out.println("forname: " + e.getMessage());
}
try{
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER");//连接数据库
PreparedStatement p = c.prepareStatement("delete from Moneymanager");
p.executeUpdate();
p.close();
c.close();
}catch(SQLException e){
System.out.println("SQLException: "+ e.getMessage());
}
int m=0,n=0;
for(m=0;m<r;m++)
for(n=0;n<4;n++){
Moneymanager.m.table.setValueAt("", m,n);
}
Moneymanager.m.fresh();
JOptionPane.showMessageDialog(null,"数据已成功清空!! ");
}
public static void main(String[] args) {
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class GetOldPW {
String oldpw;
public String getpw(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//安装驱动
}catch(java.lang.ClassNotFoundException e){
System.out.println("forname: " + e.getMessage());
}
try{
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER");//连接数据库
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select * from pw");
while(rs.next()){
oldpw=rs.getString("password");
}
s.close();
c.close();
}catch(SQLException e){
System.out.println("SQLException: "+ e.getMessage());
}
return oldpw;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Help extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
static JFrame hp = new JFrame("家庭理财系统帮助");
static JPanel help = new JPanel();
static JButton bt = new JButton("退出");
static JLabel lb1 = new JLabel("帮助:");
static JLabel lb2 = new JLabel(" 1、 修改密码:单击修改密码,则直接输入新的密码,然后确定就可以了;");
static JLabel lb3 = new JLabel(" 2、 修改数据:单击修改数据,然后选择要修改的对象的序号,再按需要修改;");
static JLabel lb4 = new JLabel(" 3、 删除数据:单击删除数据,然后选择要删除的对象的序号,直接点击删除;");
static JLabel lb5 = new JLabel(" 4、 财务统计:单击统计,可以按照给定的规则处理,可以按照某一时间进行统计;");
static JLabel lb6 = new JLabel(" 5、 总结余——显示的是最后的总计,与现款直接对应,简单明了;");
static JLabel lb7 = new JLabel(" 6、 程序的初始密码为:123;");
static JLabel lb8 = new JLabel(" 7、 单击添加,则可以在该行右边直接按次序输入财务信息;");
public Help() {
hp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
help.setLayout(null);
hp.setLayout(null);
hp.setBounds(270, 180, 520, 300);
help.setBounds(0, 0, 520, 200);
hp.setResizable(false);
bt.setBounds(150, 220, 200, 40);
hp.add(bt);
bt.addActionListener(this);
lb1.setBounds(15, 20, 600, 20);
help.add(lb1);
lb2.setBounds(15, 40, 600, 20);
help.add(lb2);
lb3.setBounds(15, 60, 600, 20);
help.add(lb3);
lb4.setBounds(15, 80, 600, 20);
help.add(lb4);
lb5.setBounds(15, 100, 600, 20);
help.add(lb5);
lb6.setBounds(15, 120, 600, 20);
help.add(lb6);
lb7.setBounds(15, 140, 600, 20);
help.add(lb7);
lb8.setBounds(15, 160, 600, 20);
help.add(lb8);
hp.add(help);
hp.setVisible(true);
help.setVisible(true);
}
public static void main(String[] args) {
}
public void actionPerformed(ActionEvent e) {
hp.setVisible(false);
}
}
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.*;
public class Moneymanager implements ActionListener {
public static Moneymanager m = new Moneymanager();//后续对m调用
JFrame f = new JFrame("家庭理财系统");
String[] Names={"序号","收入","支出","时间","备注"};
JTable table=new JTable(new Show().playerInfo,Names);//新建表格
JScrollPane s=new JScrollPane(table);//给table加滚动条
JPanel p = new JPanel();//储存表格和滚动条
static JButton bt1 = new JButton("修改密码");//static成员变量,不用写函数调用,编译时直接创建
static JButton bt2 = new JButton("修改数据");
static JButton bt3 = new JButton("删除数据");
static JButton bt4 = new JButton("财务统计");
static JButton bt5 = new JButton("添加记录");
static JButton bt6 = new JButton("帮助");
static JButton bt7 = new JButton("数据清空");
static JLabel lb1 = new JLabel("收入 元");
static JLabel lb2 = new JLabel("支出 元");
static JLabel lb3 = new JLabel("时间");
static JLabel lb4 = new JLabel("备注");
static JTextField txt1 = new JTextField("0");
static JTextField txt2 = new JTextField("0");
static JTextField txt3 = new JTextField(m.gettime());
static JTextField txt4 = new JTextField();
public void MoneymanagerWindow (){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 50, 849, 600); //主窗口大小
f.setLayout(null);f.setResizable(false);
p.add(s);f.add(p);
table.setPreferredScrollableViewportSize(new Dimension(770,370));
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
p.setBounds(20,105,800,410);
bt1.setBounds(5,5,200,40);f.add(bt1);bt1.addActionListener(this);
bt2.setBounds(215,5,200,40);f.add(bt2);bt2.addActionListener(this);
bt3.setBounds(425,5,200,40);f.add(bt3);bt3.addActionListener(this);
bt4.setBounds(635,5,200,40);f.add(bt4);bt4.addActionListener(this);
bt5.setBounds(720,60,100,40);f.add(bt5);bt5.addActionListener(this);
bt6.setBounds(40,520,200,40);f.add(bt6);bt6.addActionListener(this);
bt7.setBounds(260,520,200,40);f.add(bt7);bt7.addActionListener(this);
lb1.setBounds(20,60,200,40);f.add(lb1);
lb1.setFont(new Font("Serif",Font.BOLD,18));
lb2.setBounds(170,60,200,40);f.add(lb2);
lb2.setFont(new Font("Serif",Font.BOLD,18));
lb3.setBounds(315,60,200,40);f.add(lb3);
lb3.setFont(new Font("Serif",Font.BOLD,18));
lb4.setBounds(470,60,200,40);f.add(lb4);
lb4.setFont(new Font("Serif",Font.BOLD,18));
txt1.setBounds(60,70,55,20);f.add(txt1);
txt2.setBounds(210,70,55,20);f.add(txt2);
txt3.setBounds(355,70,100,20);f.add(txt3);
txt4.setBounds(510,70,200,20);f.add(txt4);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1){
new Changepw();
}
else if(e.getSource()==bt2){
new ChangeData();
m.fresh();
}
else if(e.getSource()==bt3){
new DelData();
}else if(e.getSource()==bt4){
new Analysis();
}
else if(e.getSource()==bt5){
new AddData();
m.fresh();
}
else if(e.getSource()==bt6){
new Help();
}
else if(e.getSource()==bt7){
String j=JOptionPane.showInputDialog("请输入密码:");
if(new GetOldPW().getpw().equals(j)){
new FormartData();
}
else
JOptionPane.showMessageDialog(null,"密码错误!!,清空失败!!");
}
}
public String gettime(){
Date now = new Date();
DateFormat d = DateFormat.getDateInstance();
return d.format(now);
}
public void fresh(){//刷新表格
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try{
Connection c = DriverManager.getConnection("jdbc:odbc:MANGER"); //建立连接MANGER数据库
Statement s = c.createStatement();
ResultSet r=s.executeQuery("select * from Moneymanager");//将查询得到的结果集给rs
int i=0;
while(r.next()){
Moneymanager.m.table.setValueAt(String.valueOf(i+1), i, 0);
Moneymanager.m.table.setValueAt(String.valueOf((r.getInt("收入"))), i, 1);
Moneymanager.m.table.setValueAt(String.valueOf((r.getInt("支出"))), i, 2);
Moneymanager.m.table.setValueAt(r.getString("时间"), i, 3);
Moneymanager.m.table.setValueAt(r.getString("备注"), i, 4);
i++;
}
c.close();s.close();
}catch(SQLException e1){
e1.getStackTrace();
}
}
public static void main(String args[]){
new Enter();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class Show {
String[][] playerInfo= new String[10000][5];//最大一万条,每条五个属性
int xuhao=0;
public Show(){
this.getplayerInfo();
}
public void getplayerInfo(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try{
Connection conn = DriverManager.getConnection("jdbc:odbc:MANGER"); //建立连接
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Moneymanager");//将查询得到的结果集给rs
int i=0;
while(rs.next()){//数据获取
playerInfo[i][0]=String.valueOf(i+1);//数据库序号数值
playerInfo[i][1]=String.valueOf((rs.getInt("收入")));
playerInfo[i][2]=String.valueOf((rs.getInt("支出")));
playerInfo[i][3]=rs.getString("时间");
playerInfo[i][4]=rs.getString("备注");
i++;
}
xuhao=i;conn.close();stmt.close();
}catch(SQLException e1){
e1.getStackTrace();
}
}
public static void main(String[] args) {
}
}