分离GUI类和主类



我有两个class .1为GUI.1从别人

LoginUser.java

package login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import database.DatabaseConnection;

public class LoginUser{
    public int doLogin(String username, String password) throws Exception
    {
        int level =0;
        DatabaseConnection databaseConnection = new DatabaseConnection();
        Connection con = databaseConnection.getConnection();
        String sql ="select level from staff where username=? and password=?";
        PreparedStatement ps =con.prepareStatement(sql);
        //no 1 and two refer to ? at String sql =...
        //Tukar String pada int
        ps.setString(1, username);
        //int iPassword = Integer.parseInt(password);
        ps.setString(2, password);
        ResultSet rs = ps.executeQuery();
        if(rs.next())
        {
            level = rs.getInt(1);
        }
        //must close all connection
        rs.close();ps.close();con.close();
        return level; 
    }
    public static void main(String[] args) {
        //test sama ada login berjaya atau tak?
        LoginUser lgn = new LoginUser();
        try {
            int level =lgn.doLogin("1008", "test123");
            System.out.println("Access Level : "+level);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

另一个是GUI类

LoginInterface.java

package login;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import java.awt.Color;
import login.LoginUser;
public class LoginInterface {
    private JFrame frame;
    private JTextField txtStaffID;
    private JPasswordField txtPassword;
    /**
     * Launch the application.
     */
    public static void login() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginInterface window = new LoginInterface();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the application.
     */
    public LoginInterface() {
        initialize();
    }
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 678, 421);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        JLabel lblStaffId = new JLabel("Username :");
        lblStaffId.setForeground(Color.BLACK);
        lblStaffId.setFont(new Font("Times New Roman", Font.PLAIN, 16));
        lblStaffId.setBounds(108, 158, 89, 23);
        frame.getContentPane().add(lblStaffId);
        txtStaffID = new JTextField();
        txtStaffID.setBounds(207, 160, 200, 23);
        frame.getContentPane().add(txtStaffID);
        txtStaffID.setColumns(10);
        JLabel lblPassword = new JLabel("Password :");
        lblPassword.setForeground(Color.BLACK);
        lblPassword.setFont(new Font("Times New Roman", Font.PLAIN, 16));
        lblPassword.setBounds(108, 192, 101, 49);
        frame.getContentPane().add(lblPassword);
        JButton btnLogIn = new JButton("LOGIN");
        btnLogIn.setBackground(new Color(0, 206, 209));
        btnLogIn.setForeground(new Color(0, 0, 0));
        btnLogIn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                LoginUser lgn = new LoginUser();
                try {
                    String username =txtStaffID.getText();
                    @SuppressWarnings("deprecation")
                    String password = txtPassword.getText();
                    int level =lgn.doLogin(username, password);
                    if(level == 1)
                    {
                        JOptionPane.showMessageDialog(null, "You successfully login");
                    }
                    else 
                    {
                        JOptionPane.showMessageDialog(null, "Your password or username incorrect");
                    }
                    System.out.println("Access Level : "+level);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Please insert your username and password");    
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        btnLogIn.setFont(new Font("Times New Roman", Font.PLAIN, 16));
        btnLogIn.setBounds(283, 272, 113, 34);
        frame.getContentPane().add(btnLogIn);
        txtPassword = new JPasswordField();
        txtPassword.setBounds(207, 208, 200, 20);
        frame.getContentPane().add(txtPassword);
    }
}

我使用PHPmyadmin和XAMPP作为数据库。请帮帮我:

无法运行LoginInterface。我不知道为什么。我的月食月亮有什么问题吗?它只显示运行LoginUser.java

您从未调用过LoginInterface.login();,只需将其添加到主方法中。

相关内容

  • 没有找到相关文章

最新更新