Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import java.sql.*;
- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.Font;
- public class Login {
- private JFrame frame;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Login window = new Login();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- Connection connection=null;
- private JTextField textFieldUN;
- private JPasswordField passwordField;
- /**
- * Create the application.
- */
- public Login() {
- initialize();
- connection=sqliteConnection.dbConnector();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JLabel lblNewLabel = new JLabel("Username:");
- lblNewLabel.setBounds(12, 75, 97, 16);
- frame.getContentPane().add(lblNewLabel);
- JLabel lblNewLabel_1 = new JLabel("Password:");
- lblNewLabel_1.setBounds(12, 139, 97, 16);
- frame.getContentPane().add(lblNewLabel_1);
- textFieldUN = new JTextField();
- textFieldUN.setBounds(12, 104, 116, 22);
- frame.getContentPane().add(textFieldUN);
- textFieldUN.setColumns(10);
- passwordField = new JPasswordField();
- passwordField.setBounds(12, 168, 116, 22);
- frame.getContentPane().add(passwordField);
- JButton btnLogin = new JButton("Login");
- btnLogin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try {
- String query="select * from Users where UserName=? and Password=? ";
- PreparedStatement pst=connection.prepareStatement(query);
- pst.setString(1, textFieldUN.getText());
- pst.setString(2, passwordField.getText());
- ResultSet rs=pst.executeQuery();
- int count =0;
- while(rs.next()) {
- count=count+1;
- }
- if(count ==1)
- {
- JOptionPane.showMessageDialog(null, "Authentification Successfull!");
- frame.dispose();
- Events ev = new Events();
- ev.setVisible(true);
- }
- else if(count>1)
- {
- JOptionPane.showMessageDialog(null, "Duplicate Username or Password");
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Authentification failed!");
- }
- rs.close();
- pst.close();
- }catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e);
- }
- }
- });
- btnLogin.setBounds(12, 203, 97, 25);
- frame.getContentPane().add(btnLogin);
- JLabel title = new JLabel("Login");
- title.setFont(new Font("Tahoma", Font.PLAIN, 25));
- title.setBounds(177, 0, 143, 51);
- frame.getContentPane().add(title);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement