public int insertPerson(Person p) throws SQLException { conn = DBTool.getInstance(); System.out.println("Inserting person..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); String firstname = p.getFirstname(); String middlename = p.getMiddlename(); String lastname = p.getLastname(); String address = p.getAddress(); String birthday = ADate.formatADate(p.getBirthdayDate(), ""); String expirationDate = ADate.formatADate(p.getExpirationDate(), ""); File picturePathF = p.getPicturePath(); String picturePath = FileTool.getDefaultFolder() + "/" + picturePathF.getName(); int quarantine = 0; String quarantineExpirationDate = ""; if (p.getQuarantineExpirationDate() != null) { quarantineExpirationDate = p.getQuarantineExpirationDate().toString(); } int oneOne = 0; String creationDate = ADate.formatADate(p.getCreationDate(), ""); if (p.isQuarantine()) { quarantine = 1; } if (p.isOneOne()) { oneOne = 1; } int enrolled = 0; if (p.isEnrolled()) { enrolled = 1; } int hone = 0; if (p.isHone()) { hone = 1; } s.executeUpdate( "INSERT INTO person (firstname, middlename, lastname, address, birthday, expirationdate, picturepath, quarantine, quarantineexpirationdate, oneone, creationdate, enrolled, hone) " + "VALUES('" + firstname + "', '" + middlename + "', '" + lastname + "', '" + address + "', '" + birthday + "', '" + expirationDate + "', '" + picturePath + "', '" + quarantine + "', '" + quarantineExpirationDate + "', '" + oneOne + "', '" + creationDate + "', '" + enrolled + "', '" + hone + "');"); ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID();"); rs.next(); int idback = rs.getInt(1); System.out.println("Insertion complete!"); rs.close(); s.close(); conn.close(); return idback; } else { System.out.println("Connection not valid"); conn.close(); return -1; } }
public void savePerson(Person p) throws SQLException { conn = DBTool.getInstance(); System.out.println("Saving person..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); int idperson = p.getId(); String firstname = p.getFirstname(); String middlename = p.getMiddlename(); String lastname = p.getLastname(); String address = p.getAddress(); String birthday = ADate.formatADate(p.getBirthdayDate(), ""); String expirationDate = ADate.formatADate(p.getExpirationDate(), ""); File picturePathF = p.getPicturePath(); String picturePath = FileTool.getDefaultFolder() + "/" + picturePathF.getName(); int quarantine = 0; String quarantineExpirationDate = ""; if (p.getQuarantineExpirationDate() != null) { quarantineExpirationDate = p.getQuarantineExpirationDate().toString(); } int oneOne = 0; if (p.isQuarantine()) { quarantine = 1; } if (p.isOneOne()) { oneOne = 1; } int enrolled = 0; if (p.isEnrolled()) { enrolled = 1; } int hone = 0; if (p.isHone()) { hone = 1; } s.executeUpdate( "UPDATE person SET firstname = '" + firstname + "', middlename = '" + middlename + "', lastname = '" + lastname + "', address = '" + address + "', birthday = '" + birthday + "', expirationdate = '" + expirationDate + "', picturepath = '" + picturePath + "', oneone = " + oneOne + ", quarantine = " + quarantine + ", enrolled = " + enrolled + ", hone= " + hone + " WHERE idperson = " + idperson + ";"); System.out.println("Save complete!"); s.close(); conn.close(); } else { System.out.println("Connection not valid"); conn.close(); } }