import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class frame2 extends JFrame implements ActionListener{ JTextField username ; JTextField password ; JButton login ; JLabel UserLabel ; JLabel PassLabel ; Connection connection = null; ResultSet rs = null; PreparedStatement pst = null; public frame2(){ super("Login:"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500,500); GridBagLayout g = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(g); c.gridx = 0; c.gridy = 0; UserLabel = new JLabel("Username"); add(UserLabel,c); c.gridx = 2; //for username txtfield c.gridy = 0; username = new JTextField(15); //name of textfield add(username,c); c.gridx = 0; //for password label c.gridy = 1; PassLabel = new JLabel("Password"); add(PassLabel,c); c.gridx = 2; //for username txtfield c.gridy = 1; password = new JTextField(15); add(password,c); c.gridx = 2;//login button c.gridy = 3; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(10,0,0,0); //top padding login = new JButton("Login"); add(login,c); login.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == login){ String sql = "select from Employee where Username=? and Password=?"; try{ pst = connection.prepareStatement(sql) ; pst.setString(1, username.getText()); pst.setString(2, password.getText()); rs = pst.executeQuery(); if(rs.next()){ JOptionPane.showMessageDialog(null, "Username and Password Correct"); setVisible(false); //closes login page } } catch(Exception ex){ setVisible(true); //frame is still visible JOptionPane.showMessageDialog(null, "Username and Password Incorrect"); } } } public static void main(String[] args){ frame2 l = new frame2(); } }
connection
connection = null;
JOptionPane.showMessageDialog(null, "Username and Password Incorrect");
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)