0
Well, I’m trying to make a login screen, in which the data are in the database, and if you have the right data as in the bd appears a message to the user, but is giving error.
The Login page:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import Banco.Manipula;
import Logica.Usuario;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingConstants;
public class Login extends JFrame {
private JPanel contentPane;
private JTextField txtUsuario;
private JPasswordField txtSenha;
private Manipula mani ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private JTextField txtUsuario;
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() throws Exception {
Manipula mani = new Manipula();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 429, 414);
contentPane = new JPanel();
contentPane.setBackground(new Color(204, 0, 0));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setUndecorated(true);
setLocationRelativeTo(null);
mani=new Manipula();
JLabel lblNewLabel_1 = new JLabel("X");
lblNewLabel_1.setBounds(407, 10, 12, 13);
lblNewLabel_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(JOptionPane.showConfirmDialog(null, "Deseja realmente fechar? ", "Confirmação", JOptionPane.YES_NO_OPTION)==0) {
Login.this.dispose();
}
}
});
contentPane.setLayout(null);
lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 11));
lblNewLabel_1.setForeground(new Color(255, 255, 255));
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_5 = new JLabel("-");
lblNewLabel_5.setBounds(385, 10, 12, 13);
lblNewLabel_5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
setExtendedState(ICONIFIED);
}
});
lblNewLabel_5.setFont(new Font("Arial", Font.BOLD, 14));
lblNewLabel_5.setForeground(Color.WHITE);
contentPane.add(lblNewLabel_5);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(101, 173, 225, 51);
contentPane.add(panel);
panel.setLayout(null);
txtUsuario = new JTextField();
txtUsuario.setBounds(12, 13, 162, 22);
panel.add(txtUsuario);
txtUsuario.setColumns(10);
txtUsuario.setBorder(null);
JLabel label = new JLabel("");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setIcon(new ImageIcon(Login.class.getResource("/imagens/e-mail_login.png")));
label.setBounds(171, 0, 54, 51);
panel.add(label);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.WHITE);
panel_1.setBounds(101, 250, 225, 51);
contentPane.add(panel_1);
panel_1.setLayout(null);
txtSenha = new JPasswordField();
txtSenha.setBounds(12, 13, 159, 19);
panel_1.add(txtSenha);
txtSenha.setBorder(null);
JLabel label_1 = new JLabel("");
label_1.setIcon(new ImageIcon(Login.class.getResource("/imagens/senha_login.png")));
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setBounds(171, 0, 54, 51);
panel_1.add(label_1);
JPanel panel_2 = new JPanel();
panel_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Usuario user = new Usuario();
user.setEmail(txtUsuario.getText());
user.setSenha(new String(txtSenha.getPassword()));
if(VerificarUsuario(user.getEmail(),user.getSenha())) {
JOptionPane.showMessageDialog(null,"Usuário e senha corretos"+user.getEmail()+user.getSenha());
}else {
JOptionPane.showMessageDialog(null,"Usuário ou senha incorretos");
}
}
});
panel_2.setBackground(Color.BLUE);
panel_2.setBounds(137, 331, 146, 19);
contentPane.add(panel_2);
panel_2.setLayout(null);
JLabel lblLogin = new JLabel("LOGIN");
lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
lblLogin.setForeground(Color.WHITE);
lblLogin.setFont(new Font("Arial", Font.BOLD, 16));
lblLogin.setBounds(48, 0, 56, 16);
panel_2.add(lblLogin);
JLabel label_2 = new JLabel("");
label_2.setIcon(new ImageIcon(Login.class.getResource("/imagens/user_login.png")));
label_2.setBounds(185, 52, 64, 94);
contentPane.add(label_2);
JLabel lblCadastrarse = new JLabel("CADASTRAR-SE");
lblCadastrarse.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
Cadastrar cad;
try {
cad = new Cadastrar();
Login.this.dispose();//fecha a janela do Login
cad.setVisible(true);//Abrindo o formulário principal
} catch (Exception e) {
e.printStackTrace();
}
}
});
lblCadastrarse.setFont(new Font("Arial", Font.BOLD, 13));
lblCadastrarse.setForeground(Color.WHITE);
lblCadastrarse.setBounds(40, 13, 111, 16);
contentPane.add(lblCadastrarse);
}
public boolean VerificarUsuario(String email, String senha) {
return(mani.Logar(email,senha));
}
}
The connection:
import java.sql.*;
public class Conexao {
public Connection connection = null;
private final String DRIVER = "com.mysql.jdbc.Driver";
private final String DBNAME = "sys_bible";
private final String URL = "jdbc:mysql://localhost/"+DBNAME+"?autoReconnect=true&useSSL=false";
private final String LOGIN = "root";
private final String SENHA = "";
public Connection getConnection(){
try{
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, LOGIN, SENHA);
System.out.println("Conectou");
return connection;
}
catch(ClassNotFoundException erro){
System.out.println("Driver nao encontrado " +erro.toString());
return connection;
}
catch(SQLException erro){
System.out.println("Falha ao conectar "+ erro.toString());
return connection;
}
}
public void close(){
try{
connection.close();
System.out.println("Desconectou");
}catch(SQLException erro){
}
}
}
Manipulative class:
import java.sql.*;
import javax.swing.JOptionPane;
public class Manipula {
private Connection minhaConexao;
public Manipula() throws Exception{
this.minhaConexao = new Conexao().getConnection();
}
public void inserir(String nome, String email, int rg, String senha){
try{
String sql ="insert into usuario (nome,email,rg,senha) values('"+nome+"','"+email+"',"+rg+",'"+senha+"')";
PreparedStatement stmt = minhaConexao.prepareStatement(sql);
stmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Inserido com Sucesso!");
}catch(SQLException erro){
JOptionPane.showMessageDialog(null,"Erro ao inserir");
}
}
public boolean Logar(String email, String senha) {
try {
String sql = "Select * from usuario where email = '"+ email+"' and senha='"+senha+"'";
PreparedStatement stmt = minhaConexao.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
String nomebanco = rs.getString("email");
String senhabanco = rs.getString("senha");
if(email.equals(nomebanco) && senhabanco.equals(senha)){
JOptionPane.showMessageDialog(null,"Seja bem vindo: ");
return true;
}else{
JOptionPane.showMessageDialog(null,"Login ou Senha inválidos.");
return true;
}
}
}catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Erro na conexão, com o banco de dados!","Título",JOptionPane.WARNING_MESSAGE);
return false;
}
return false;
}
}
And the User class
public class Usuario {
private String email;
private String senha;
public Usuario() {
}
public void setEmail(String n) {
this.email=n;
}
public String getEmail() {
return this.email;
}
public void setSenha(String s) {
this.senha=s;
}
public String getSenha() {
return this.senha;
}
}
I gave it right, thank you so much for the help, really, vlw!!
– Matheus Henrique Souza