Estou trabalhando na função para alterar senha do usuário caso ele esqueça a senha, eu criei um frame para buscar login do usuário no banco ai ok, só que não to conseguindo trazer o id do usuário pra mudar a senha do mesmo…
Segue o código abaixo
// resetsenha controller
package Teladelogin.controller;
import Teladelogin.bean.loginbean;
import Teladelogin.dao.logindao;
import chamartela.GerarTela;
import jBDC.Conexao;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import load.LoadFXML;
public class ResetSenhaController implements Initializable {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
Stage dialogStage = new Stage();
Scene scene;
@FXML
private TextField user;
@FXML
private PasswordField senha;
@FXML
private Button salvar;
@FXML
private Button buscar;
@FXML
private AnchorPane redimensionar;
@FXML
private TextField codigo;
@Override
public void initialize(URL url, ResourceBundle rb) {
this.carregarImage();
this.buscar.setOnAction(event -> {
if (buscarLogin()) {
conn = Conexao.getConnection();
String sql = "Select * from usuario where login = ? ";
try {
pst = (PreparedStatement) conn.prepareStatement(sql);
pst.setString(1, user.getText());
rs = pst.executeQuery();
if (!rs.next()) {
infobox("Login não encontrado, Verifique e tente Novamente ", null, "Erro");
this.user.setText("");
} else {
infobox2("Login Encontrado", null, "Sucesso");
Node node = (Node) event.getSource();
dialogStage = (Stage) node.getScene().getWindow();
}
} catch (Exception e) {
// JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
}
});
this.salvar.setOnAction(event -> {
if (validaCampo()) {
loginbean login = new loginbean();
logindao logi = new logindao();
login.setLogin(user.getText());
login.setSenha(senha.getText());
try {
logi.Alterar(login);
infobox2("Usuário e Senha Alterado com Sucesso", null, "Ok");
Node node = (Node) event.getSource();
dialogStage = (Stage) node.getScene().getWindow();
dialogStage.close();
} catch (Exception ex) {
Logger.getLogger(ResetSenhaController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public static URL getFXML() {
return ResetSenhaController.class.getResource("/Teladelogin/view/ResetSenha.fxml");
}
public void infobox(String infoMessage, String headerText, String title) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText(infoMessage);
alert.setTitle(title);
alert.setHeaderText(headerText);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("/imagem/logo b.png"));
alert.showAndWait();
}
public void infobox2(String infoMessage, String headerText, String title) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText(infoMessage);
alert.setTitle(title);
alert.setHeaderText(headerText);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("/imagem/logo b.png"));
alert.showAndWait();
}
private boolean validaCampo() {
String erroMessage = "";
if (user.getText() == null || user.getText().length() == 0) {
erroMessage += "Login inválido\n";
}
if (senha.getText() == null || senha.getText().length() == 0) {
erroMessage += "Senha inválido\n";
}
if (erroMessage.length() == 0) {
return true;
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Erro ao Alterar de Senha");
alert.setHeaderText("Campos Vazios, Insira os Dados para Atualizar as Informações ");
alert.setContentText(erroMessage);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("/imagem/logo b.png"));
alert.showAndWait();
return false;
}
}
private boolean buscarLogin() {
String erroMessage = "";
if (user.getText() == null || user.getText().length() == 0) {
erroMessage += "Login inválido\n";
}
if (erroMessage.length() == 0) {
return true;
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Buscar Login ");
alert.setHeaderText("Campo Vazio, Insira o Login para Buscar mais Informações ");
alert.setContentText(erroMessage);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("/imagem/logo b.png"));
alert.showAndWait();
return false;
}
}
private void carregarImage() {
this.redimensionar.setStyle(
"-fx-background-image:url(/imagem/login.png);"
+ "-fx-background-repeat:round;"
+ "-fx-background-size:contain;"
+ "-fx-background-position:center center;");
}
}
// classe DAO Login
package Teladelogin.dao;
import Teladelogin.bean.loginbean;
import jBDC.Conexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class logindao {
private Connection con = null;
private PreparedStatement pst = null;
private ResultSet rs = null;
public Object Alterar(Object obj) throws Exception {
loginbean login = (loginbean) obj;
String sql = "update usuario set login = ?,senha = ? where codigo = ?";
try {
con = Conexao.getConnection();
pst = con.prepareStatement(sql);
pst.setString(1, login.getLogin());
pst.setString(2, login.getSenha());
pst.setInt(3, login.getCodigo());
pst.execute();
return login;
} catch (SQLException ex) {
Logger.getLogger(logindao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
Conexao.fechar(pst);
Conexao.fechar(con);
}
return null;
}
}