// 通过行业查找 public ArrayList<BusinessUnit> findByTrade(String trade) { trade.trim(); BusinessUnit busUnit; ArrayList<BusinessUnit> busUnitList = new ArrayList<BusinessUnit>(); try { Connection con = initDB(); // 创建SQL命令对象 Statement stmt = con.createStatement(); // 创建SQL命令字符串 String query = "select * from BusinessUnit where trade='" + trade + "'"; // 执行SQL语句 ResultSet rs = stmt.executeQuery(query); while (rs.next()) { busUnit = new BusinessUnit(); busUnit.setUnitId(rs.getString("unitId")); busUnit.setUnitName(rs.getString("unitName")); busUnit.setUnitNature(rs.getString("unitNature")); busUnit.setArea(rs.getString("area")); busUnit.setTrade(rs.getString("trade")); busUnit.setLinkman(rs.getString("linkman")); busUnit.setAddress(rs.getString("address")); busUnit.setBank(rs.getString("bank")); busUnit.setAccountNum(rs.getString("accountNum")); busUnit.setPhone(rs.getString("phone")); busUnit.setMail(rs.getString("mail")); busUnit.setMainBusiness(rs.getString("mainBusiness")); busUnitList.add(busUnit); busUnit = null; } // 关闭记录集 rs.close(); // 关闭命令对象连接 stmt.close(); // 关闭数据库连接 con.close(); } catch (SQLException e) { System.out.println("查找数据失败!"); e.printStackTrace(); } return busUnitList; }
// 更新操作,注意这里是对除主码外所有属性同时更新,设定是这样的在要更新的文本框中先显示原来的信息,修改后 // 再将这些数据重新赋值给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(); } }