Set 6: Freshman - Logistics Tracking Management System

Explanation:
1. This article is the function realization of Java form (load data, delete a row of data, add order)
2. Need to use two other classes DB. Article http://t.csdn.cn/7m4pk)
3. For the design requirements of the database, see the test questions for details
Main interface:
insert image description here

Function 1: As soon as the program is executed, the table loads data

The specific implementation steps are as follows:

1. In the top constructor of the main form code, add and write the following code (the code in the red box)

insert image description here

2. A method to customize and initialize table data: (write below the constructor, as shown above:)

The source code is as follows:

//自定义的方法:用于初始化表格数据
	private void initTable(String sql) {
    
    
		// 1.准备好所有的数据,放入 data 中
		Vector data = new Vector();

		ResultSet rs = BaseDao.cx(sql); // 查询数据库
		try {
    
    
			while (rs.next()) {
    
    
				Vector line = new Vector(); // line 代表存放一行数据的集合
				
					int id = rs.getInt(1);   //分别从数据库中取出对应的值
					String name = rs.getString(2);
					double num = rs.getDouble(3);
					String oid = rs.getString(4);
					String desc = rs.getString(5);
				
				line.add(id);  //将上面获取到的每个数据放到行集合中
				line.add(name);
				line.add(num);
				line.add(oid);
				line.add(desc);

				data.add(line); // 将当前行存入大的data中
			}
		} catch (SQLException e) {
    
    
			e.printStackTrace();
		}

		// 2.准备好标题文字,放入 bt 中
		Vector bt = new Vector(); // bt 用于存放标题
		bt.add("编号");
		bt.add("商品名字");
		bt.add("商品价格");
		bt.add("订单编号");
		bt.add("订单描述");

		// 3.最终任务就是用上面准备好的 数据 和 标题 填充 表格
		jTable1.setModel(new DefaultTableModel(data, bt));

	}

Function 2: The function of the delete button

insert image description here

Implementation steps

1. Double-click the delete button in the main form, and it will automatically go to the code for writing the button event:

insert image description here

2. Write the delete function in the body of the above automatically opened code method, the code is as follows:

insert image description here
The source code is as follows :

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    
    
		
		int rowCount = jTable1.getSelectedRowCount(); // 获取选中的行数,如果为0,则说明没有选中任何行
		if (rowCount == 0) {
    
    
			JOptionPane.showMessageDialog(null, "您未选择任何数据");  //弹出一个带有确定按钮的对话框
		} else {
    
    
			//弹出带有两个按钮:确定和取消的对话框,如果点击【确定】则返回值为0,点击【取消】返回值为1
			int isOk = JOptionPane.showConfirmDialog(null, "您确定要删除吗?", "删除确认",JOptionPane.YES_NO_OPTION);  
			if (isOk == 0) {
    
     // 如果值是 0 ,说明点击了确定要删除
				int selectedRow = jTable1.getSelectedRow(); // 获取选中行的索引
				int id = (Integer) jTable1.getModel()
						.getValueAt(selectedRow, 0); // 找到要删除的行的第一列,其实就是要删除的记录的主键
				String sql = "delete from tb_order where id=" + id; // 拿到主键值后,组装出删除的sql语句
				boolean zsg = BaseDao.zsg(sql); // 执行删除操作,并返回是否成功
				if (zsg) {
    
    
					JOptionPane.showMessageDialog(null, "删除成功!");

					// 删除后,调用表格初始化的方法,重新加载数据,使表格加载显示最新数据:
					initTable("select * from tb_order");
				} else {
    
    
					JOptionPane.showMessageDialog(null, "删除失败!");
				}
			}
		}
		
	}

Function 3: Add function

Implementation steps:

1. First create the [Add Form] interface by dragging and dropping, and set the default close property toDISPOSE

insert image description here

2. Click the [Add] button in the [Main Form], and use the code to open the [Add Form]:

Double-click on the [Add] button of ==[main form]==, automatically go to the place where the code is written, and write the code to open the new form: double-click the
insert image description here
blank area of ​​the button above, and automatically enter the code area below:
insert image description here
write code as follows:
insert image description here

3. Write a handler on the [Confirm Add] button of [Add Form]:

insert image description here
After double-clicking the above button, it will automatically go to the following code:
insert image description here
Write the code as shown in the figure below:
insert image description here
Source code:

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
    
		
		// 1. 获取所有文本框的数据
		String name = jTextField1.getText(); // 获取商品名字。jTextField1是第一个输入框的名字(可以改名)
		String price = jTextField2.getText(); // 获取价格
		String oid = jTextField3.getText(); // 获取商品编号
		String desc = jTextField4.getText(); // 获取商品描述

		// 2.如果商品名、价格、商品编号有任意一个没填写,则给出提示
		if ("".equals(name) || "".equals(price) || "".equals(oid)) {
    
    
			JOptionPane.showMessageDialog(null, "请录入完整信息!");
		}
		// 3.如果所有的文本框都填写了,则进行插入操作:
		else {
    
    
			// 所有从文本输入框中获取到的内容都为String字符串类型,要想插入到数据库,必须与数据表中的数据类型一致。
			// 因此,需要对价格进行转换(数据库表中价格字段是小数类型)
			double pri = Double.parseDouble(price); // 将文本框获取的字符串转换为小数

			// 拼装sql插入语句:
			String sql = String.format(
					"insert into tb_order values(null,'%s',%f,'%s','%s')",
					name, pri, oid, desc);

			if (BaseDao.zsg(sql)) {
    
     // 真正的执行,并根据返回结果给出提示
				JOptionPane.showMessageDialog(null, "新增商品成功!");
				// this.setVisible(false); //隐藏当前窗体
				this.dispose(); // 销毁当前窗体(即 关闭新增窗口)
			} else {
    
    
				JOptionPane.showMessageDialog(null, "新增商品失败!");
			}
		}
	}

4. Back to home button

You can directly close the current form.
Double-click the button and write the following code: this.dispose(); // Destroy the current form (that is, close the newly added window)

Guess you like

Origin blog.csdn.net/weixin_42634814/article/details/131227311