美和易思 - JAVA开发&移动互联网 阶段性教学效果检测考试机试试题【题目:维护洗衣店消费项数据】

一、 语言和环境

1. 实现语言:Java 语言。

2. 环境要求:Eclipse 或 Myeclipse+MySQL。

二、 功能需求

利用 Java Swing 和 JDBC 技术维护洗衣店消费项数据。

具体要求如下:

1. 首界面显示洗衣店所有的消费项目信息,如图 1 所示。

美和易思 - JAVA开发&移动互联网 阶段性教学效果检测考试机试试题【题目:维护洗衣店消费项数据】_第1张图片

2. 点击“新增”按钮,弹出消费项新增界面,如图 2 所示。输入消费项相关信息之后,点击 “新增”按钮,在数据库中新增一条消费项记录,并返回至图 1 刷新消费项数据;点击“返 回”按钮则取消新增,返回至图 1。

美和易思 - JAVA开发&移动互联网 阶段性教学效果检测考试机试试题【题目:维护洗衣店消费项数据】_第2张图片

3. 在图 1 所示的消费项显示界面中,选中所需修改的消费项,点击“修改”按钮,弹出消费项修改界面,如图 3 所示。消费项信息修改完毕之后,点击“修改”按钮,完成该消费项信息的更新,并返回至图 1 刷新消费项数据;点击“返回”按钮,则取消更新,返回至图 1。

美和易思 - JAVA开发&移动互联网 阶段性教学效果检测考试机试试题【题目:维护洗衣店消费项数据】_第3张图片

三、数据库设计

数据库名为 db_cosume,表名为 tb_items,具体表结构见下表。

美和易思 - JAVA开发&移动互联网 阶段性教学效果检测考试机试试题【题目:维护洗衣店消费项数据】_第4张图片

四、注意事项:

1. 在工程中添加 MySQL 驱动。

2. 注意程序逻辑分明、命名规范以及书写有缩进。

3. 添加适当的注释。

4. 消费项测试数据至少三条。

五、评分标准

美和易思 - JAVA开发&移动互联网 阶段性教学效果检测考试机试试题【题目:维护洗衣店消费项数据】_第5张图片

六、实现代码:

下载链接:https://pan-yz.chaoxing.com/external/m/file/488056817269772288

Mysql:

-- ----------------------------
-- Table structure for `tb_items`
-- ----------------------------
DROP TABLE IF EXISTS `tb_items`;
CREATE TABLE `tb_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item_name` varchar(30) NOT NULL,
  `unit_price` decimal(4,1) NOT NULL,
  `member_price` decimal(4,1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_items
-- ----------------------------
INSERT INTO `tb_items` VALUES ('1', '外套', '10.0', '9.0');
INSERT INTO `tb_items` VALUES ('2', '裤子', '8.0', '7.0');
INSERT INTO `tb_items` VALUES ('3', '衬衫', '8.0', '7.0');
INSERT INTO `tb_items` VALUES ('4', '领带', '6.0', '5.0');
INSERT INTO `tb_items` VALUES ('5', '风衣', '16.0', '15.0');
INSERT INTO `tb_items` VALUES ('6', '羊毛衫', '16.0', '15.0');
INSERT INTO `tb_items` VALUES ('7', '羽绒服', '20.0', '23.0');
INSERT INTO `tb_items` VALUES ('8', '短裙', '6.0', '5.0');
INSERT INTO `tb_items` VALUES ('9', '长裙', '10.0', '9.0');
INSERT INTO `tb_items` VALUES ('10', '羊绒裙', '18.0', '16.0');
INSERT INTO `tb_items` VALUES ('11', '西服', '20.0', '18.0');
INSERT INTO `tb_items` VALUES ('12', '床单', '12.0', '10.0');

Java:

1、com.test.Dao >>DbConnection

package com.test.Dao;

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

import com.mysql.cj.xdevapi.Statement;

public class DbConnection {
	//驱动类的类名
	private static final String DRIVERNAME="com.mysql.cj.jdbc.Driver";
	//连接数据的URL路径
	private static final String URL="jdbc:mysql://localhost:3306/db_cosume?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL";
	//数据库登录账号
	private static final String USER="root";
	//数据库登录密码
	private static final String PASSWORD="root123";
	//加载驱动
	static{
		try {
			Class.forName(DRIVERNAME);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
    //获取数据库连接
	public static Connection getConnection() {
	          try {
		return DriverManager.getConnection(URL,USER,PASSWORD);
	           } catch (SQLException e) {	
		e.printStackTrace();
	           }
			return null;
	}
	//查询
	public static ResultSet query(String sql) {
		System.out.println(sql);
		//获取连接
		Connection connection=getConnection();
		PreparedStatement psd;
		try {
			psd = connection.prepareStatement(sql);
			return psd.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//增、删、改、查
		public static int updataInfo(String sql) {
			System.out.println(sql);
			//获取连接
			Connection connection=getConnection();
			try {
				PreparedStatement psd=connection.prepareStatement(sql);
				return psd.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return 0;
		}
	//关闭连接
	public  static  void colse(ResultSet rs,Statement stmt,Connection  conn) throws Exception{
	          try { if (rs != null){ rs.close(); }
                         	 if (stmt != null) { ((java.sql.Statement) stmt).cancel(); }
		 if (conn != null) { conn.close(); }
	   } catch (Exception e) {
		   e.printStackTrace(); throw new Exception();
	   }
	}
}	

2、com.test.Entity >> tb_items

package com.test.Entity;

public class tb_items {
	private int id;//消费项编号
	private String item_name;//消费项名称
	private String unit_price;//单价
	private String member_price;//会员价
	//重写toString()方法
	@Override
	public String toString() {
		return "tb_items [id=" + id + ", item_name=" + item_name + ", unit_price=" + unit_price + ", member_price="
				+ member_price + "]";
	}
	//无参构造
	public tb_items() {
		super();
	}
	//有参构造
	public tb_items(int id, String item_name, String unit_price, String member_price) {
		super();
		this.id = id;
		this.item_name = item_name;
		this.unit_price = unit_price;
		this.member_price = member_price;
	}
	//get、set方法
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getItem_name() {
		return item_name;
	}
	public void setItem_name(String item_name) {
		this.item_name = item_name;
	}
	public String getUnit_price() {
		return unit_price;
	}
	public void setUnit_price(String unit_price) {
		this.unit_price = unit_price;
	}
	public String getMember_price() {
		return member_price;
	}
	public void setMember_price(String member_price) {
		this.member_price = member_price;
	}
	
	
}

3、com.test.Vive >> MainFrame

package com.test.Vive;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.test.Controller.Select;
import com.test.Entity.tb_items;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {
	Object[] columnNames = {"商品编号","名称","类别名称","库存"};
	Object[][] data = Select.getTb_items();
	DefaultTableModel df = new DefaultTableModel(data, columnNames);
	JTable jTable;
	public MainFrame() {
		super("消费项管理");
		this.setBounds(0, 0, 800, 500);
		this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
		this.setResizable(false);//让窗口大小不可改变
		getContentPane().setLayout(null);
		
		jTable=new JTable(df);
		JScrollPane jp = new JScrollPane(jTable);
		jp.setBounds(0, 87, 794, 350);
		getContentPane().add(jp);
		
		JButton jubtton_Add = new JButton("新增");
		jubtton_Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				IncreaseGUL increaseGUL = new IncreaseGUL();
				increaseGUL.setVisible(true);
			}
		});
		jubtton_Add.setBounds(522, 31, 87, 34);
		getContentPane().add(jubtton_Add);
		
		JButton jubtton_Revise = new JButton("修改");
		jubtton_Revise.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (jTable.getSelectedColumn()<0) {
					JOptionPane.showMessageDialog(null, "请选择要修改的数据!");
				} else {
					tb_items items = new tb_items();
					items.setId(Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString()));
					items.setItem_name(jTable.getValueAt(jTable.getSelectedRow(), 1).toString());
					items.setUnit_price(jTable.getValueAt(jTable.getSelectedRow(), 2).toString());
					items.setMember_price(jTable.getValueAt(jTable.getSelectedRow(), 3).toString());
					ModifyGUI modifyGUI = new ModifyGUI(items);
					modifyGUI.setVisible(true);
				}
			}
		});
		jubtton_Revise.setBounds(642, 31, 80, 34);
		getContentPane().add(jubtton_Revise);
	}
}

4、com.test.Vive >> IncreaseGUL

package com.test.Vive;

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

import com.test.Controller.Updata;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class IncreaseGUL extends JFrame{
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	public IncreaseGUL() {
		super("新增消费项");
		this.setBounds(0, 0, 491, 341);
		this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
		this.setResizable(false);//让窗口大小不可改变
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("名称");
		label.setBounds(85, 52, 72, 18);
		getContentPane().add(label);
		
		textField = new JTextField();
		textField.setBounds(138, 49, 172, 24);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("单价");
		label_1.setBounds(85, 101, 72, 18);
		getContentPane().add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(138, 98, 172, 24);
		getContentPane().add(textField_1);
		
		JLabel label_1_1 = new JLabel("会员价");
		label_1_1.setBounds(85, 146, 72, 18);
		getContentPane().add(label_1_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(138, 143, 172, 24);
		getContentPane().add(textField_2);
		
		JButton button = new JButton("新增");
		button.setBounds(85, 218, 113, 27);
		getContentPane().add(button);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//INSERT INTO `tb_items` VALUES ('1', '外套', '10.0', '9.0');
				String sql = "INSERT INTO tb_items VALUES (null, '"+textField.getText()+"', '"+textField_1.getText()+"', '"+textField_2.getText()+"');";
				int result = Updata.addData(sql);
				if (result>0) {
					JOptionPane.showMessageDialog(null, "新增成功!");
					MainFrame m =new MainFrame();
					m.dispose();
					m.setVisible(true);
					dispose();
				} else {
					JOptionPane.showMessageDialog(null, "新增失败!");
				}
			}
		});
		
		JButton button_1 = new JButton("返回");
		button_1.setBounds(265, 218, 113, 27);
		getContentPane().add(button_1);
		button_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
	}
}

5、com.test.Vive >> ModifyGUI 

package com.test.Vive;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import com.test.Controller.Updata;
import com.test.Entity.tb_items;

public class ModifyGUI extends JFrame{
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	public ModifyGUI(tb_items items) {
		super("消费项修改");
		this.setBounds(0, 0, 491, 341);
		this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
		this.setResizable(false);//让窗口大小不可改变
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("消费项");
		label.setBounds(38, 65, 72, 18);
		getContentPane().add(label);
		
		textField = new JTextField();
		textField.setBounds(95, 62, 129, 24);
		getContentPane().add(textField);
		textField.setColumns(10);
		textField.setText(items.getItem_name());
		
		JLabel label_1 = new JLabel("单价");
		label_1.setBounds(254, 65, 72, 18);
		getContentPane().add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(311, 62, 129, 24);
		getContentPane().add(textField_1);
		textField_1.setText(items.getUnit_price());
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(95, 129, 129, 24);
		getContentPane().add(textField_2);
		textField_2.setText(items.getMember_price());
		
		JLabel label_2 = new JLabel("会员价");
		label_2.setBounds(38, 132, 72, 18);
		getContentPane().add(label_2);
		
		JButton button = new JButton("修改");
		button.setBounds(85, 218, 113, 27);
		getContentPane().add(button);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String sql = "UPDATE tb_items SET item_name='"+textField.getText()+"',unit_price='"+textField_1.getText()+"',member_price='"+textField_2.getText()+"' WHERE id="+items.getId()+"";
				int result = Updata.addData(sql);
				if (result>0) {
					JOptionPane.showMessageDialog(null, "修改成功!");
					dispose();
				} else {
					JOptionPane.showMessageDialog(null, "修改失败!");
				}
			}
		});
		
		JButton button_1 = new JButton("返回");
		button_1.setBounds(265, 218, 113, 27);
		getContentPane().add(button_1);
		button_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}
}

6、com.test.Vive >> Select 

package com.test.Controller;

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

import com.test.Dao.DbConnection;
import com.test.Entity.tb_items;


public class Select {
	public static Object[][] getTb_items() {
		String sql = "SELECT * FROM tb_items";
		ResultSet resultSet = DbConnection.query(sql);
		ArrayList list=new ArrayList();
		try {
			while (resultSet.next()) {
				tb_items tb=new tb_items();
				tb.setId(resultSet.getInt(1));
				tb.setItem_name(resultSet.getString(2));
				tb.setUnit_price(resultSet.getString(3));
				tb.setMember_price(resultSet.getString(4));
				list.add(tb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Object[][] objects=new Object[list.size()][4];
		for(int i=0;i

7、com.test.Vive >> Updata 

package com.test.Controller;

import com.test.Dao.DbConnection;

public class Updata {
	//添加数据
	public static int addData(String sql) {
		
		return DbConnection.updataInfo(sql);
	}
	
}

8、com.test.Vive >> Test 

package com.test.Test;

import com.test.Vive.MainFrame;

public class Test {
	public static void main(String[] args) {
		MainFrame mainFrame = new MainFrame();
		mainFrame.setVisible(true);
	}
}

 

你可能感兴趣的:(#,Java_大学&作业)