-1
DAO class
package DAO;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
@SuppressWarnings("unused")
public class conectaBD {
public static Connection conectabd() throws ClassNotFoundException {
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/crud","postgres","tmk9405");
JOptionPane.showMessageDialog(null,"Conectado com sucesso!");
return con;
}
catch (SQLException error) {
JOptionPane.showMessageDialog(null, error);
return null;
}
}
}
Form LOGIN
package visual;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.sql.*;
import DAO.conectaBD;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import java.awt.Toolkit;
import java.awt.Color;
import javax.swing.ImageIcon;
@SuppressWarnings("unused")
public class frmLogin extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frmLogin frame = new frmLogin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
private JTextField txtUsuario;
private JPasswordField txtSenha;
private JLabel lblLogin;
private JLabel lblSistemaDesenvolvidoPela;
public frmLogin() throws ClassNotFoundException {
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Alessandro\\Downloads\\icons8-senha-50.png"));
setTitle("Login de usu\u00E1rio");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 299);
contentPane = new JPanel();
contentPane.setBorder(new TitledBorder(null, "", TitledBorder.CENTER, TitledBorder.TOP, null, null));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsurio = new JLabel("Usu\u00E1rio:");
lblUsurio.setBounds(139, 70, 85, 20);
lblUsurio.setHorizontalAlignment(SwingConstants.CENTER);
lblUsurio.setFont(new Font("Arial", Font.BOLD, 12));
contentPane.add(lblUsurio);
txtUsuario = new JTextField();
txtUsuario.setBounds(224, 73, 179, 20);
txtUsuario.setColumns(10);
contentPane.add(txtUsuario);
JLabel lblSenha = new JLabel("senha:");
lblSenha.setBounds(139, 106, 85, 20);
lblSenha.setHorizontalAlignment(SwingConstants.CENTER);
lblSenha.setFont(new Font("Arial", Font.BOLD, 12));
contentPane.add(lblSenha);
JButton btnEntrar = new JButton("ENTRAR");
btnEntrar.setIcon(new ImageIcon("C:\\Users\\Alessandro\\Downloads\\icones\\Icones\\accept.png"));
btnEntrar.setBounds(262, 166, 104, 23);
btnEntrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Logar();
}
});
txtSenha = new JPasswordField();
txtSenha.setBounds(224, 107, 179, 20);
contentPane.add(txtSenha);
contentPane.add(btnEntrar);
lblLogin = new JLabel("LOGIN");
lblLogin.setFont(new Font("Arial", Font.BOLD, 15));
lblLogin.setBounds(262, 11, 67, 21);
contentPane.add(lblLogin);
lblSistemaDesenvolvidoPela = new JLabel("Sistema desenvolvido por Alesssandro Jacques.)");
lblSistemaDesenvolvidoPela.setForeground(Color.BLACK);
lblSistemaDesenvolvidoPela.setFont(new Font("SansSerif", Font.PLAIN, 10));
lblSistemaDesenvolvidoPela.setBounds(233, 256, 391, 14);
contentPane.add(lblSistemaDesenvolvidoPela);
this.setLocationRelativeTo(null); // centralizando formulario
con = conectaBD.conectabd();
setResizable(false); //Bloquando a opção maximizar a tela de login
}
@SuppressWarnings("deprecation")
public void Logar() {
String sql = "Select * from login where usuario = ? and senha = ?";
try {
pst = con.prepareStatement(sql);
pst.setString(1, txtUsuario.getText());
pst.setString(2, txtSenha.getText());
rs = pst.executeQuery();
if(rs.next()) {
frmPrincipal frm = new frmPrincipal();
frm.setVisible(true);
dispose();
}else {
JOptionPane.showMessageDialog(null,"Usuario e senha inválidos.");
}
}
catch(SQLException error) {
JOptionPane.showMessageDialog(null, error);
}
}
}
MAIN FORM
package visual;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class frmPrincipal extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField txtBusca;
private JButton btnLimpar;
private JButton btnSair;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frmPrincipal frame = new frmPrincipal();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection conectabd=null;
/**
* Create the frame.
*/
public frmPrincipal() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1124, 744);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblPesquisar = new JLabel("Pesquisar:");
lblPesquisar.setBounds(50, 52, 85, 33);
lblPesquisar.setFont(new Font("Tahoma", Font.PLAIN, 18));
contentPane.add(lblPesquisar);
txtBusca = new JTextField();
txtBusca.setBounds(140, 62, 260, 23);
txtBusca.setColumns(10);
contentPane.add(txtBusca);
//pesquisarUsuarios();
JButton btnBuscar = new JButton("Buscar");
btnBuscar.setBounds(410, 60, 108, 23);
btnBuscar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnBuscar.setIcon(new ImageIcon("C:\\Users\\Alessandro\\Downloads\\icons8-pesquisar-filled-25.png"));
contentPane.add(btnBuscar);
btnLimpar = new JButton("Limpar ");
btnLimpar.setIcon(new ImageIcon("C:\\Users\\Alessandro\\Downloads\\icons8-limpar-pesquisa-25.png"));
btnLimpar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtBusca.setText(null);
}
});
btnLimpar.setBounds(523, 107, 115, 23);
contentPane.add(btnLimpar);
btnSair = new JButton("SAIR");
btnSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnSair.setBounds(410, 107, 108, 23);
btnSair.setFont(new Font("Tahoma", Font.BOLD, 11));
btnSair.setIcon(new ImageIcon("C:\\Users\\Alessandro\\Downloads\\icons8-fechar-janela-25.png"));
contentPane.add(btnSair);
this.setExtendedState(MAXIMIZED_BOTH); // utiliza para maximizar o formulário
//setResizable(false); //Bloquando a opção maximizar
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); //tirando a opção fechar o JFRAME
}
}
Be more specific about your problem. Explain to those who read your question what problems you are facing. Gather information like stacktrace, messages and logs.
– Augusto Vasques