Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import java.lang.*;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.sql.*;
- import javax.swing.*;
- public class Login {
- Connection connection = null;
- private JFrame frame;
- private JTextField username;
- private JPasswordField password;
- /**
- * 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();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Login() {
- initialize();
- connection = Database.dbconector();
- }
- /**
- * 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);
- username = new JTextField();
- username.setBounds(127, 55, 200, 50);
- frame.getContentPane().add(username);
- username.setColumns(10);
- password = new JPasswordField();
- password.setBounds(127, 125, 200, 50);
- frame.getContentPane().add(password);
- JButton btnLogin = new JButton("Log in");
- btnLogin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- String query = "select * from bsit-db where Username=? and Password=?";
- PreparedStatement pst = connection.prepareStatement(query);
- pst.setString(1, username.getText());
- pst.setString(2, password.getText());
- ResultSet rs = pst.executeQuery();
- int count = 0;
- while (rs.next()) {
- count = count + 1;
- }
- if (count == 1) {
- JOptionPane.showInternalInputDialog(null, "Successfully Login to your account!");
- } else {
- JOptionPane.showInternalInputDialog(null, "Incorrect ID/Password. Please Try Again!");
- }
- rs.close();
- pst.close();
- } catch (Exception e1) {
- JOptionPane.showInternalInputDialog(null, e1);
- } finally {
- try {
- } catch (Exception e1) {
- JOptionPane.showInternalInputDialog(null, e1);
- }
- }
- }
- });
- btnLogin.setBounds(103, 202, 112, 37);
- frame.getContentPane().add(btnLogin);
- JButton btnClear = new JButton("clear");
- btnClear.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- username.setText(null);
- password.setText(null);
- }
- });
- btnClear.setBounds(237, 202, 112, 37);
- frame.getContentPane().add(btnClear);
- JLabel lblUsername = new JLabel("username");
- lblUsername.setBounds(66, 55, 200, 50);
- frame.getContentPane().add(lblUsername);
- JLabel lblPassword = new JLabel("password");
- lblPassword.setBounds(66, 125, 200, 50);
- frame.getContentPane().add(lblPassword);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement