目錄
檔案說明:
一、語言和環境
二、技術要求
三、功能要求
四、資料庫設計
五、具體要求及推薦實作步驟
六、注意事項
實作代碼:
一、資料庫
二、Java Swing
檔案說明:
一、語言和環境
- 實作語言:Java,
- 環境要求:MyEclipse、JDK 1.6 以上、MySQL,
二、技術要求
該系統采用 SWING 技術配合 JDBC 使用 JAVA 編程語言完成桌面應用開發,
三、功能要求
某電商公司為了方便客服查看用戶的訂單資訊,需開發一個用戶訂單管理系統,要求本系統實作以下功能:
1、啟動程式后進入主界面,顯示所有訂單資訊,效果如圖 1 所示:
2、在訂單編號文本框中輸入正確訂單號顯示該訂單資訊,如圖 2:
3、選中某個訂單后點擊洗掉按鈕,提示是否真的要洗掉,確定后洗掉該資料,如圖 3:
4. 洗掉成功后顯示洗掉后訂單資訊,如圖 4:
四、資料庫設計
資料庫名為 OrderDb,表結構如下,
表 1 訂單表(tb_order)
| 列名 | 含義 | 資料型別 | 約束(描述) |
| id | 編號 | int | 主鍵,自動增長 |
| name | 商品名稱 | varchar(20) | 非空 |
| price | 價格 | decimal(5,2) | 非空 |
| orderID | 所屬訂單單號 | varchar(20) | 非空 |
| descinfo | 描述 | varchar(100) |
|
五、具體要求及推薦實作步驟
- 創建資料庫和表
- 創建訂單物體類
- 創建 BaseDAO 類完成資料庫的連接和關閉
- 創建 OrderDAO 完成對訂單的查詢與洗掉功能
- 創建 MainFrame 類并添加相應組件,完成界面設計
- 在 MainFrame 中添加 JTable 完成資料展示,
- 在 MainFrame 中添加 Jbutton 完成資料洗掉
- 在 MainFrame 中添加文本框完成單條資料搜索
六、注意事項
| 題目:物流跟蹤管理系統 | ||
| 該程式的評分標準如下: | ||
| 10 | 資料庫和表 | |
|
| 正確創建資料庫和表 6 分,測驗資料 4 分,必須以分離資料庫或腳本方式提交, 否則該項不得分 | |
| 5 | 正確創建和撰寫物體類,包含所有屬性及方法 | |
| 10 | 正確創建并撰寫 BaseDAO 完成資料庫的連接(5 分)和關閉(5 分), | |
| 30 | 正確創建并撰寫 OrderDAO 完成資料查詢與洗掉 | |
|
| 10 | 串列資料查詢 |
|
| 10 | 單條資料查詢 |
|
| 10 | 資料洗掉 |
| 40 | 創建并撰寫 MainFrame 類完成界面設計及功能呼叫 | |
|
| 5 | 圖 1 中各個組件的設計 |
|
| 10 | 圖 1 中資料展示 |
|
| 10 | 圖 2 中單條資料并正確展示在 JTable 中 |
|
| 5 | 圖 3 中單條資料洗掉詢問 |
|
| 10 | 圖 4 中資料洗掉并展示新資料在 Jtable 中, |
| 5 | 總體編程技術 | |
|
| 2 | 編碼規范 |
|
| 3 | 注釋完善 |
| 總分 | 100 分 | |
實作代碼:
一、資料庫
-- ----------------------------
-- Table structure for tb_order
-- ----------------------------
DROP TABLE IF EXISTS `tb_order`;
CREATE TABLE `tb_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`price` decimal(10,2) NOT NULL,
`orderid` varchar(20) DEFAULT NULL,
`descinfo` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_order
-- ----------------------------
INSERT INTO `tb_order` VALUES ('1', 'iphones Max', '8999.00', '5325781', '楊明金的訂單');
INSERT INTO `tb_order` VALUES ('2', '小米10', '3655.00', '20201005', '楊楊的訂單');
INSERT INTO `tb_order` VALUES ('3', '華為榮耀10 =', '3200.00', '20201102', '小白的訂單');
二、Java Swing
com.ynavc.Bean
tb_order.java
package com.ynavc.Base;
public class tb_order {
private int id;
private String name;
private double price;
private int orderID;
private String descinfo;
public tb_order() {
super();
}
public tb_order(int id, String name, double price, int orderID, String descinfo) {
super();
this.id = id;
this.name = name;
this.price = price;
this.orderID = orderID;
this.descinfo = descinfo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getOrderID() {
return orderID;
}
public void setOrderID(int orderID) {
this.orderID = orderID;
}
public String getDescinfo() {
return descinfo;
}
public void setDescinfo(String descinfo) {
this.descinfo = descinfo;
}
}
com.ynavc.BaseDAO
DbConnection.java
package com.ynavc.BaseDAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Statement;
public class DbConnection {
//驅動類的類名
private static final String DRIVERNAME="com.mysql.jdbc.Driver";
//連接資料的URL路徑
private static final String URL="jdbc:mysql://127.0.0.1:3306/orderdb";
//資料庫登錄賬號
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) {
JOptionPane.showMessageDialog(null,"執行陳述句出錯\n"+e.toString());
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) {
JOptionPane.showMessageDialog(null,"執行陳述句出錯\n"+e.toString());
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){
stmt.cancel();
}
if (conn != null) { conn.close(); }
} catch (Exception e) {
e.printStackTrace(); throw new Exception();
}
}
}
com.ynavc.OrderDAO
Update.java
package com.ynavc.OrderDAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.ynavc.Base.tb_order;
import com.ynavc.BaseDAO.DbConnection;
public class Update {
//查詢主頁資訊
public Object[][] getMainInfo(String id) {
String sql;
if (id.equals("")) {
sql = "select * from tb_order";
}else {
sql = "select * from tb_order WHERE orderid LIKE '"+id+"%';";
}
ResultSet re = DbConnection.query(sql);
ArrayList<tb_order> list = new ArrayList<tb_order>();
try {
while (re.next()) {
tb_order tb = new tb_order();
tb.setId(re.getInt(1));
tb.setName(re.getString(2));
tb.setPrice(re.getDouble(3));
tb.setOrderID(re.getInt(4));
tb.setDescinfo(re.getString(5));
list.add(tb);
}
} catch (SQLException e) {
e.printStackTrace();
}
Object[][] ob = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
ob[i][0] = list.get(i).getId();
ob[i][1] = list.get(i).getName();
ob[i][2] = list.get(i).getPrice();
ob[i][3] = list.get(i).getOrderID();
ob[i][4] = list.get(i).getDescinfo();
}
return ob;
}
//洗掉資料
public int addData(int id) {
String sql = "DELETE FROM tb_order WHERE id='"+id+"';";
return DbConnection.updataInfo(sql);
}
}
com.ynavc.Vive
MainFrame.java
package com.ynavc.Vive;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.ynavc.OrderDAO.Update;
import javax.swing.JButton;
public class MainFrame extends JFrame {
private JTextField orderid;
Update update = new Update();
Object[] header = {"編號","商品名稱","商品價格","訂單編號","訂單描述"};
Object[][] data = update.getMainInfo("");
DefaultTableModel df = new DefaultTableModel(data, header);
public MainFrame() {
super("物流a跟蹤管理系統");
this.setBounds(0, 0, 700, 500);
getContentPane().setLayout(null);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title = new JLabel("物流跟蹤管理系統");
title.setFont(new Font("宋體", Font.BOLD, 40));
title.setBounds(182, 13, 336, 50);
getContentPane().add(title);
JLabel orderid_text = new JLabel("訂單編號");
orderid_text.setBounds(14, 70, 72, 18);
getContentPane().add(orderid_text);
orderid = new JTextField();
orderid.setBounds(80, 67, 179, 24);
getContentPane().add(orderid);
orderid.setColumns(10);
JButton seach = new JButton("搜索");
seach.setBounds(273, 66, 90, 27);
getContentPane().add(seach);
seach.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (orderid.equals("")) {
JOptionPane.showMessageDialog(null, "請輸入要查詢的訂單編號!");
} else {
String id = orderid.getText();
data = update.getMainInfo(id);
df.setDataVector(data, header);
}
}
});
JTable jTable = new JTable(df);
JScrollPane scrollPane = new JScrollPane(jTable);
scrollPane.setBounds(14, 104, 666, 302);
getContentPane().add(scrollPane);
JButton delete = new JButton("洗掉");
delete.setBounds(590, 425, 90, 27);
getContentPane().add(delete);
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTable.getSelectedColumn()<0) {
JOptionPane.showMessageDialog(null, "請選擇要洗掉的資料!");
} else {
int num = JOptionPane.showConfirmDialog(null, "您真的真的要洗掉嗎?","溫馨提示", 0,1);
int id = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
if(num == JOptionPane.OK_OPTION){
int result = update.addData(id);
if (result>0) {
JOptionPane.showMessageDialog(null, "洗掉成功!");
orderid.setText("");
} else {
JOptionPane.showMessageDialog(null, "洗掉失敗!");
}
}
}
}
});
}
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
}
com.ynavc.Test
Test.java
package com.ynavc.Test;
import com.ynavc.Vive.MainFrame;
public class Test {
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/160860.html
標籤:其他
上一篇:OSPF協議總結(1)
