private void isEdit(Boolean isEdit) { if (isEdit) { try { Connection con = FrameLogin.getConnect(); String sql = "SELECT * FROM Persons WHERE personId = " + Id; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); if (rs.first()) { String firstname = rs.getString("FirstName"); String lastname = rs.getString("LastName"); String cellphone = rs.getString("CellPhoneNo"); String homephone = rs.getString("HomePhoneNo"); String gradyear = rs.getString("Graduation Year"); String Gender = rs.getString("Gender"); firstName.setText(firstname); lastName.setText(lastname); cellPhone.setText(cellphone); homePhone.setText(homephone); gradYear.setText(gradyear); gender.setSelectedItem(Gender); jLabel7.setVisible(false); studentId.setVisible(false); } else { MessageBox.infoBox("Error: ID not found", "Error"); } } catch (Exception e) { MessageBox.infoBox(e.toString(), "Error in isEdit"); } } FrameLogin.closeConnect(); }
public static void closeConnect() { try { con.close(); } catch (SQLException e) { MessageBox.infoBox(e.toString(), "Error in closeConnect"); } }
private void submitButtonActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_submitButtonActionPerformed // TODO add your handling code here: Connection con = FrameLogin.getConnect(); String SQLInsert = "INSERT INTO `2761DB`.`Persons` (`FirstName`, `LastName`, `CellPhoneNo`, `HomePhoneNo`, " + "`SchoolId`, `Graduation Year`, `Gender`) VALUES ('" + firstName.getText() + "', '" + lastName.getText() + "', '" + cellPhone.getText() + "', '" + homePhone.getText() + "', '" + studentId.getText() + "', '" + gradYear.getText() + "', '" + (String) (gender.getSelectedItem()) + "');"; String SQLUpdate = "UPDATE `2761DB`.`Persons` SET `FirstName` = '" + firstName.getText() + "', `LastName` = '" + lastName.getText() + "', `CellPhoneNo` = ' " + cellPhone.getText() + "', `HomePhoneNo` = '" + homePhone.getText() + "', `Graduation Year` = '" + gradYear.getText() + "', `Gender` = '" + (String) (gender.getSelectedItem()) + "' WHERE `PersonId` = '" + Id + "';"; // UPDATE `2761DB`.`Persons` SET `FirstName`='Robbie', `LastName`='Tacescu', `CellPhoneNo`='', // `HomePhoneNo`='559300', `SchoolId`='15648916', `Graduation Year`='6545', `Gender`='males' // WHERE // `PersonId`='42'; try { if (isEdit) { Statement stmt = con.createStatement(); // System.out.println(SQLUpdate); stmt.executeUpdate(SQLUpdate); } else { Statement stmt = con.createStatement(); // System.out.println(SQLInsert); stmt.executeUpdate(SQLInsert); } } catch (SQLException err) { MessageBox.infoBox(err.toString(), "Error in AddUserForm submitButton"); } FrameLogin.closeConnect(); this.dispose(); } // GEN-LAST:event_submitButtonActionPerformed
public static boolean isId(String id) { boolean isInt = true; try { Integer.parseInt(id); } catch (Error er) { isInt = false; MessageBox.infoBox("Error: ID incorrect", "Error"); } return isInt; }
private void formWindowClosing( java.awt.event.WindowEvent evt) { // GEN-FIRST:event_formWindowClosing // TODO add your handling code here: Connection con = getConnect(); try { // con.close(); } catch (Exception err) { MessageBox.infoBox(err.toString(), "Error closing connection in formWindowClosed"); } } // GEN-LAST:event_formWindowClosing
private void logIn(int personId) { Connection con = getConnect(); try { // MessageBox.infoBox("You have successfully logged in", "Login"); // INSERT INTO LogInOut (PersonId, TimeIn) VALUES (1, NOW()) String SQLLogin = "******" + personId + ", NOW())"; Statement stmtLogin = con.createStatement(); stmtLogin.executeUpdate(SQLLogin); Update_table(); } catch (SQLException err) { MessageBox.infoBox(err.toString(), "Error"); } closeConnect(); }
private void manualOutActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_manualOutActionPerformed String strId = manualInput.getText(); if (isId(strId)) { int id = getId(); if (isIdFound(id)) { int personId = getPersonId(id); if (!isLoggedIn(id)) { MessageBox.infoBox("Error: Already Logged In", "Error"); } else { logOut(personId); manualInput.setText(""); } } } } // GEN-LAST:event_manualOutActionPerformed
private void logOut(int personId) { Connection con = getConnect(); try { // MessageBox.infoBox("You have successfully logged out", "Logout"); // UPDATE LogInOut SET TimeOut = NOW() WHERE PersonId = 1 AND TimeOut IS NULL String SQLLogin = "******" + personId + " AND TimeOut IS NULL"; Statement stmtLogin = con.createStatement(); stmtLogin.executeUpdate(SQLLogin); Update_table(); } catch (SQLException err) { System.out.println(err); MessageBox.infoBox(err.toString(), "Error"); } closeConnect(); }
private boolean isLoggedIn(int id) { boolean isLoggedIn = false; Connection con = getConnect(); int personId = getPersonId(id); try { String SQL = "SELECT * FROM LogInOut WHERE personId = " + personId + " AND TimeOut IS NULL"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(SQL); if (rs != null && rs.next()) { rs.last(); isLoggedIn = true; } } catch (Exception err) { MessageBox.infoBox(err.toString(), "Error from isLoggedIn"); System.out.println(err); } closeConnect(); return isLoggedIn; // SELECT * FROM LogInOut WHERE personId = 1 AND TimeOut IS NULL }
public static int getPersonId(int id) { Connection con = getConnect(); ResultSet rs = null; int personId = 0; try { String SQL = "SELECT PersonId FROM 2761DB.Persons WHERE SchoolId = " + id; Statement stmt = con.createStatement(); rs = stmt.executeQuery(SQL); if (rs.next()) { rs.first(); personId = rs.getInt("PersonId"); } else { AddUserQuery addUserQuery = new AddUserQuery(); addUserQuery.setVisible(true); } } catch (Exception err) { MessageBox.infoBox(err.toString(), "Error in getPersonId"); } closeConnect(); return personId; }
public static boolean isIdFound(int id) { Connection con = getConnect(); boolean isFound = false; try { String SQL = "SELECT PersonId, FirstName, LastName, SchoolId FROM 2761DB.Persons WHERE SchoolId = " + id; Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(SQL); if (rs.first()) { isFound = true; } else { AddUserQuery addUser = new AddUserQuery(); addUser.setVisible(true); } } catch (SQLException err) { System.out.println(err); MessageBox.infoBox(err.toString(), "Error"); } closeConnect(); return isFound; }