说明
这是我们小组一起共同完成的一个简易物流管理系统,我负责的部分是数据访问层中的删除功能,以及数据库的搭建,还有针对不同窗口大小的适应部分。
页面搭建
我们使用的是Java swing框架 ,根据框架中提供的按钮、文本框、多行文本框以及弹出对话框建立联系,在按下确定按钮时会调用相应的数据访问层语言,先检查数据库是否存在以及其他情况,返回相应的状态,从而进行判断。
//注册提交
BtnCo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String iam = String.valueOf(comboBox.getSelectedItem());
LoginPanel.ID = IDField.getText();
String address = AddressField.getText();
String password = String.valueOf(PasswordField.getPassword());
String passwordVerify = String.valueOf(PasswordVerifyField.getPassword());
if (iam == "买家") {
LoginPanel.type = 0;
}
if (iam == "商家") {
LoginPanel.type = 1;
}
switch (Response.register(type,ID,address,password,passwordVerify)) {
case Done : {
jf.setVisible(false);
jf.setBounds(0, (int) (Oheight*0.037), Owidth, (int) (Oheight*0.963));
MainPanel();
jf.setVisible(true);
break;
}
case ExistedUID : {
JOptionPane.showMessageDialog(jf,
"<html><font size=14>账号已存在!", "错误",
JOptionPane.ERROR_MESSAGE);
break;
}
case IncorrectUID : {
JOptionPane.showMessageDialog(jf,
"<html><font size=12>账号长度不是11位!", "错误",
JOptionPane.ERROR_MESSAGE);
break;
}
case IncorrectAddress : {
JOptionPane.showMessageDialog(jf,
"<html><font size=14>地址长度不符!", "错误",
JOptionPane.ERROR_MESSAGE);
break;
}
case IncorrectPassword : {
JOptionPane.showMessageDialog(jf,
"<html><font size=14>密码位数不符!", "错误",
JOptionPane.ERROR_MESSAGE);
break;
}
case PasswordMismatched : {
JOptionPane.showMessageDialog(jf,
"<html><font size=14>密码不同!", "错误",
JOptionPane.ERROR_MESSAGE);
break;
}
default : {
System.out.println(Response.register(type,ID,address,password,passwordVerify));
break;
}
}
}
});
}
当用户从下拉框列表中选择的是买家或者卖家时,根据买家类型为零,商家类型为一 进行注册尝试,如果返回的是error,根据不同error返回相应的提示框,如果成功,则会改变页面大小,添加新的Panel图像。
数据访问层
在deleteID这个方法中,先写sql语句,然后将传入的参数设置到SQL语句的问号中,因为删除不需要判断UID(一定是正确的UID),在执行以后再执行删除包裹语句,如果是用户这个账号删除了,就传入用户的id然后根据id删除对应的包裹,顾客同理。
static public Response.ResponseState deleteID(String table, String UID) throws SQLException{
Connection databaseConnection = null;
PreparedStatement databaseStatement = null;
ResultSet databaseResult = null;
Response.ResponseState processState = Response.ResponseState.Error;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
databaseConnection = DriverManager.getConnection(DatabaseURL,
"...", "...");
String SQL = "delete from " + table + " where UID = ?";
databaseStatement = databaseConnection.prepareStatement(SQL,
databaseResult.TYPE_SCROLL_INSENSITIVE, databaseResult.CONCUR_UPDATABLE);
databaseStatement.setString(1, UID);
databaseStatement.executeUpdate();
if ("sellers".equals(table)) {
return deletePack(UID,"sellers");
} else {
return deletePack(UID,"customers");
}
}
catch (SQLException e) {
processState = Response.ResponseState.DatabaseError;
}
catch (ClassNotFoundException e) {
processState = Response.ResponseState.DriverNotFound;
}
finally {
try {
assert databaseResult != null;
databaseResult.close();
assert databaseStatement != null;
databaseStatement.close();
assert databaseConnection != null;
databaseConnection.close();
}
catch(Exception e){
}
}
return processState;
}
static private Response.ResponseState deletePack(String UID, String type) throws SQLException{
Connection databaseConnection2 = null;
PreparedStatement databaseStatement2 = null;
ResultSet databaseResult2 = null;
Response.ResponseState processState = Response.ResponseState.Error;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
databaseConnection2 = DriverManager.getConnection(DatabaseURL,
"...", "...");
String SQL2;
if ("sellers".equals(type)) {
SQL2 = "delete from packages where SellerID = ?";
} else {
SQL2 = "delete from packages where CustomerID = ?";
}
databaseStatement2 = databaseConnection2.prepareStatement(SQL2,
databaseResult2.TYPE_SCROLL_INSENSITIVE, databaseResult2.CONCUR_UPDATABLE);
databaseStatement2.setString(1, UID);
databaseStatement2.executeUpdate();
processState = Response.ResponseState.Done;
}
catch (SQLException e) {
processState = Response.ResponseState.DatabaseError;
}
catch (ClassNotFoundException e) {
processState = Response.ResponseState.DriverNotFound;
}
finally {
try {
assert databaseResult2 != null;
databaseResult2.close();
assert databaseStatement2 != null;
databaseStatement2.close();
assert databaseConnection2 != null;
databaseConnection2.close();
}
catch(Exception e){
}
}
return processState;
}
增删改查
增
商家通过在收货人手机号上添加文本,然后点击添加包裹,只要数据库中有买家手机号,他就可以添加成功。
在收件人的账户上,可以在同一时间显示页面状况。
删
用户可以点击销毁账户,后端会根据用户的id删除相应的信息。就不演示了。
改
用户可以通过点击修改密码,以防密码的泄露。
查
每一次点击的货物列表以及个人信息,都会调用数据访问层中的方法,并显示在界面上。
ArrayList<Result> arrayList;
arrayList = Response.query(LoginPanel.type,LoginPanel.ID);
int len = arrayList.size();
repaint();
removeAll();
updateUI();
URL imageURL = LoginFrame.class.getResource("/images/wallpaper.jpg");
try {
image = ImageIO.read(imageURL);
} catch (Exception e) {
e.printStackTrace();
}
Container con = this;
//设置为自定义布局
con.setLayout(null);
数据库
数据库是根据mysql语句写的,搭建在服务器上,只要有软件,就可以轻松的根据手机号创建用户,进行模拟化的操作,商家可以发货给买家(因为是模拟,所以功能不会太多)。
如上图进行数据库的搭建,不仅可以使用图形化界面,也可以使用sql语句进行编写。
本项目的全部源码以及打包程序均上传至GitHub,并附有说明文档,具体信息请前往Github查看,测试程序请前往Release下载,项目遵循GPL-3.0 License协议。