// 更新操作,注意这里是对除主码外所有属性同时更新,设定是这样的在要更新的文本框中先显示原来的信息,修改后 // 再将这些数据重新赋值给BusinessUnit对象,再调用该函数完成更新 public void update(BusinessUnit busUnit) { try { Connection con = initDB(); // 创建SQL命令对象 Statement stmt = con.createStatement(); // 创建SQL命令字符串 String query = "update BusinessUnit set " + "unitName='" + busUnit.getUnitName() + "',unitNature='" + busUnit.getUnitNature() + "',area='" + busUnit.getArea() + "',trade='" + busUnit.getTrade() + "',linkman='" + busUnit.getLinkman() + "',address='" + busUnit.getAddress() + "',bank='" + busUnit.getBank() + "',accountNum='" + busUnit.getAccountNum() + "',phone='" + busUnit.getPhone() + "',mail='" + busUnit.getMail() + "',mainBusiness='" + busUnit.getMainBusiness() + "'where unitId='" + busUnit.getUnitId() + "'"; // 执行SQL语句 stmt.execute(query); // 关闭命令对象连接 stmt.close(); // 关闭数据库连接 con.close(); } catch (SQLException e) { System.out.println("更新数据失败!"); e.printStackTrace(); } }
// 增操作 public void add(BusinessUnit busUnit) { try { Connection con = initDB(); // 创建SQL命令对象 Statement stmt = con.createStatement(); // 创建SQL命令字符串 String query = "insert into BusinessUnit(unitId,unitName,unitNature,area,trade," + "linkman,address,bank,accountNum,phone,mail,mainBusiness) values ('" + busUnit.getUnitId() + "','" + busUnit.getUnitName() + "','" + busUnit.getUnitNature() + "','" + busUnit.getArea() + "','" + busUnit.getTrade() + "','" + busUnit.getLinkman() + "','" + busUnit.getAddress() + "','" + busUnit.getBank() + "','" + busUnit.getAccountNum() + "','" + busUnit.getPhone() + "','" + busUnit.getMail() + "','" + busUnit.getMainBusiness() + "')"; // 执行SQL语句 stmt.execute(query); // 关闭命令对象连接 stmt.close(); // 关闭数据库连接 con.close(); } catch (SQLException e) { System.out.println("删除数据失败!"); e.printStackTrace(); } }