import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/**
*
* @author master
*/
public class Login extends javax.swing.JFrame {
/** Creates new form Login */
public Login() {
initComponents();
botaoX();
}
Component comp;
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
strUsuario = new javax.swing.JFormattedTextField();
strSenha = new javax.swing.JPasswordField();
jLabel4 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Login");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 36));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("LOGIN");
jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jLabel2.setFont(new java.awt.Font("MS Sans Serif", 0, 24));
jLabel2.setText("USUARIO:");
jLabel3.setFont(new java.awt.Font("MS Sans Serif", 0, 24));
jLabel3.setText("SENHA:");
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("CANCELAR");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("MS Sans Serif", 0, 12));
jLabel4.setText("Entre com o usu\u00e1rio e senha, caso voc\u00ea n\u00e3o tenha fa\u00e7a a solicita\u00e7\u00e3o para seu supervisor.");
jLabel6.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
jLabel6.setText("Copy By Scorsatto");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(447, 447, 447)
.add(jLabel1)
.addContainerGap(1814, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(287, 287, 287)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel2)
.add(43, 43, 43)
.add(strUsuario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 228, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(jLabel3)
.add(69, 69, 69)
.add(strSenha, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(1702, 1702, 1702))
.add(layout.createSequentialGroup()
.add(473, 473, 473)
.add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 199, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(1817, Short.MAX_VALUE))
.add(layout.createSequentialGroup()
.add(245, 245, 245)
.add(jLabel4)
.addContainerGap(1752, Short.MAX_VALUE))
);
layout.linkSize(new java.awt.Component[] {jLabel1, strSenha, strUsuario}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap(17, Short.MAX_VALUE)
.add(jLabel1)
.add(89, 89, 89)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(strUsuario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel2))
.add(48, 48, 48)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(strSenha, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel3))
.add(58, 58, 58)
.add(jLabel4)
.add(75, 75, 75)
.add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(242, 242, 242))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
boolean validado=validaCampos();
if (validado){
boolean logado=ConsultaLogin(); //BOTAO OK
if (!logado){
JOptionPane.showMessageDialog(comp,"ERRO ! usuário ou senha inválida.",null,JOptionPane.ERROR_MESSAGE);
limpaCampos();
}else{
setVisible(false);
new Principal().setVisible(true);
}
}else msgerro();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); //BOTAO CANCELAR
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName() );
}catch( Exception e ){
e.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
private String getsenha(){
String senha;
senha= new String(strSenha.getPassword());
return senha;
}
private void limpaCampos(){
strSenha.setText("");
strUsuario.setText("");
}
private boolean validaCampos(){
boolean validado=true;
String senha=getsenha();
if((strUsuario.getText().equals("")) || (senha.equals(""))){
validado=false;
}
return validado;
}
private void msgerro(){
JOptionPane.showMessageDialog(comp,"ERRO ! Todos os campos são obrigatórios.",null,JOptionPane.ERROR_MESSAGE);
}
private boolean ConsultaLogin(){
Connection c;
String user="",password="";
ResultSet rs = null;
Component comp = null;
boolean logado=false;
try{
//dado.getText().toUpperCase();
//registro do driver
Class.forName("org.gjt.mm.mysql.Driver");
//obtendo coneccao
c = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase");
//criado um manipulador de instrucao
Statement s = c.createStatement();
String senha=getsenha();
rs = s.executeQuery("SELECT * FROM login where usuario='"+strUsuario.getText()+"'AND senha='"+senha+"'");
// ResultSetMetaData rsmd = rs.getMetaData();//executa as colunas id, nome ...
while (rs.next()){
logado=true;
}
} catch (Exception e) {
System.err.println(e);
}finally {
try{
//fechar coneccao com o banco de dados
rs.close();
}catch (Exception e){
e.printStackTrace();
}
}
return logado;
}
public void botaoX(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
//caixa de dialogo retorna um inteiro
int resposta = JOptionPane.showConfirmDialog(null,"Deseja Realmente finalizar o Sistema?","Finalizar",JOptionPane.YES_NO_OPTION);
//sim = 0, nao = 1
if (resposta == 0) {
System.exit(0);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JPasswordField strSenha;
private javax.swing.JFormattedTextField strUsuario;
// End of variables declaration
}