diff --git a/Projects/LoginForm with database b/Projects/LoginForm with database new file mode 100644 index 0000000..c0dc82e --- /dev/null +++ b/Projects/LoginForm with database @@ -0,0 +1,258 @@ +import java.awt.Color; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import javax.swing.JOptionPane; +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author Muzamil + */ +public class Login extends javax.swing.JFrame { + + private static final long serialVersionUID = 1L; + int id; + /** + * Creates new form Login + */ + public Login() { + initComponents(); + getContentPane().setBackground(Color.DARK_GRAY); + + + } + + /** + * 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. + */ + + // + private void initComponents() { + + jLabel4 = new javax.swing.JLabel(); + jLabel1 = new javax.swing.JLabel(); + jButton1 = new javax.swing.JButton(); + jLabel2 = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + ID = new javax.swing.JTextField(); + ps = new javax.swing.JPasswordField(); + register = new javax.swing.JButton(); + + jLabel4.setBackground(new java.awt.Color(102, 102, 255)); + jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/download.jpg"))); // NOI18N + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setResizable(false); + + jLabel1.setBackground(new java.awt.Color(153, 153, 0)); + jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N + jLabel1.setForeground(new java.awt.Color(0, 102, 153)); + jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel1.setText("Employee Management"); + jLabel1.setBorder(new javax.swing.border.MatteBorder(null)); + + jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N + jButton1.setText("Login"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N + jLabel2.setText("ID"); + + jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N + jLabel3.setText("Password"); + + ID.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N + ID.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + IDActionPerformed(evt); + } + }); + + ps.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + psActionPerformed(evt); + } + }); + + register.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N + register.setText("Register"); + register.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + registerActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createSequentialGroup() + .addGap(30, 30, 30) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel2) + .addGap(64, 64, 64) + .addComponent(ID)) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel3) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(ps, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))) + .addGap(42, 42, 42)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(register, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGap(148, 148, 148)))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(74, 74, 74) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(6, 6, 6) + .addComponent(jLabel2)) + .addComponent(ID, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createSequentialGroup() + .addGap(120, 120, 120) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel3) + .addComponent(ps, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addGap(18, 18, 18) + .addComponent(jButton1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(register) + .addContainerGap(50, Short.MAX_VALUE)) + ); + + pack(); + }// + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + // with database login + try{ + //getting value from text field of ID + id = Integer.parseInt(ID.getText()); + //getting value from password field + String password = String.valueOf(ps.getPassword()); + //connection with database root is username and 1234 is password + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc","root","1234"); + String query = "SELECT * FROM logins WHERE id = '"+id+"' AND Password ='"+password+"'"; + Statement s = conn.createStatement(); + ResultSet rs = s.executeQuery(query); + if(rs.next()==true){ + setVisible(false); + new view().setVisible(true); + + } + else{ + JOptionPane.showMessageDialog(null, "Inccorrect username or password"); + ID.setText(""); + ps.setText(""); + } + s.close(); + } + catch(Exception e){ + JOptionPane.showMessageDialog(null, "connection error"); + } + //For manual login without database + /* String pas = ps.getText(); + String ID = id.getText(); + if(ID.contains("admin2")&&pas.contains("22")){ + setVisible(false); + new view().setVisible(true); + } + else{ + JOptionPane.showMessageDialog(ps, "Incorrect info","Message",0); + }*/ + } + + private void IDActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void psActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void registerActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + this.toFront(); + setVisible(false); + new Register().toFront(); + new Register().setState(java.awt.Frame.NORMAL); + new Register().setVisible(true); + + + } + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new Login().setVisible(true); + } + }); + } + + // Variables declaration - do not modify + private javax.swing.JTextField ID; + private javax.swing.JButton jButton1; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JLabel jLabel4; + private javax.swing.JPasswordField ps; + private javax.swing.JButton register; + // End of variables declaration +}