@Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btnSave) { if (tfFirstName.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter first name!"); return; } else if (tfLastName.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter last name!"); return; } else if (tfAddress.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter Address!"); return; } else if (tfPhoneNumber.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter phone number!"); return; } else if (dateModel.getValue().toString().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter date of birth!"); return; } DBConnection dbconnection = new DBConnection(); if (person == null) { person = new Person(); } person.setFirstName(tfFirstName.getText()); person.setLastName(tfLastName.getText()); person.setAddress(tfAddress.getText()); person.setPhoneNumber(tfPhoneNumber.getText()); person.setDOB(dateModel.getYear() + "-" + dateModel.getMonth() + "-" + dateModel.getDay()); int result = -1; if (person.getId() == 0) { result = dbconnection.savePerson(person); } else { result = dbconnection.updatePerson(person); } if (result != -1) { JOptionPane.showMessageDialog(null, "Saved Successfully!"); } else { JOptionPane.showMessageDialog(null, "Save Unsuccessful!"); } } else if (e.getSource() == btnReset) { tfFirstName.setText(""); tfLastName.setText(""); tfAddress.setText(""); tfPhoneNumber.setText(""); tfDOB.setText(""); dateModel.setValue(new Date()); } else if (e.getSource() == btnDelete) { DBConnection dBConnection = new DBConnection(); int result = dBConnection.deletePerson(person.getId()); if (result != -1) { JOptionPane.showMessageDialog(null, "Deleted Successfully!"); } else { JOptionPane.showMessageDialog(null, "Deletion Unsuccessful!"); } } }
public void deletePerson(Person p) throws SQLException { conn = DBTool.getInstance(); System.out.println("Deleting person person..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); int idperson = p.getId(); s.execute("DELETE FROM guest WHERE idperson = " + idperson + ";"); s.execute("DELETE FROM person WHERE idperson = " + idperson + ";"); System.out.println("Deletion complete!"); s.close(); conn.close(); } else { System.out.println("Connection not valid"); conn.close(); } }
public int saveGuest(Person p, Guest g) throws SQLException { conn = DBTool.getInstance(); System.out.println("Saving guest..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); int idPerson = p.getId(); String guestFirstname = g.getFirstname(); String guestMiddlename = g.getMiddlename(); String guestLastname = g.getLastname(); String guestBirthday = ADate.formatADate(g.getBirthday(), ""); String guestCreationdate = ADate.formatADate(g.getCreationDate(), ""); s.execute( "INSERT INTO guest (firstname, middlename, lastname, creationdate, birthday, idperson) VALUES('" + guestFirstname + "', '" + guestMiddlename + "', '" + guestLastname + "', '" + guestCreationdate + "', '" + guestBirthday + "', '" + idPerson + "');"); ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID();"); rs.next(); int returnId = rs.getInt(1); System.out.println("Save complete!"); rs.close(); s.close(); conn.close(); return returnId; } else { System.out.println("Connection not valid"); conn.close(); return -1; } }
public void updatePerson(Person person) { Person personDao = em.find(Person.class, person.getId()); em.getTransaction().begin(); System.out.println("--->" + person); personDao.setFirstname(person.getFirstname()); personDao.setLastname(person.getLastname()); personDao.setBirthdate(person.getBirthdate()); personDao.setEmail(person.getEmail()); personDao.setWebsite(person.getWebsite()); if (person.getGroup() != null) { Group group = new Group(); group.setGroupID(person.getGroup().getGroupID()); group.setName(person.getGroup().getName()); personDao.setGroup(group); } em.getTransaction().commit(); }
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(); } }