| package project; import javax.swing.*; import java.awt.event.*; import java.sql.*; public class LoginDialog extends JDialog{ private JPanel contentPane; private JButton buttonOK; private JButton buttonCancel; private JLabel lblUserName; private JLabel lblPassword; private JTextField tfPassword; private JTextField tfUserName; public LoginDialog() { this.setSize(500, 450); contentPane = new JPanel(); contentPane.setLayout(null); lblUserName = new JLabel(“用户名:”); lblUserName.setSize(50, 40); lblUserName.setLocation(50, 50); tfUserName = new JTextField(); tfUserName.setSize(250, 40); tfUserName.setLocation(150, 50); lblPassword = new JLabel(“密码:”); lblPassword.setSize(50, 40); lblPassword.setLocation(50, 150); tfPassword = new JPasswordField(); tfPassword.setSize(250, 40); tfPassword.setLocation(150, 150); buttonOK = new JButton(“登录”); buttonOK.setSize(80, 40); buttonOK.setLocation(130, 250); buttonCancel = new JButton(“取消”); buttonCancel.setSize(80, 40); buttonCancel.setLocation(280, 250); contentPane.add(lblUserName); contentPane.add(tfUserName); contentPane.add(lblPassword); contentPane.add(tfPassword); contentPane.add(buttonOK); contentPane.add(buttonCancel); setContentPane(contentPane); setModal(true); getRootPane().setDefaultButton(buttonOK); buttonOK.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { onOK(); } }); buttonCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { onCancel(); } }); // call onCancel() when cross is clicked setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { onCancel(); } }); } private void onOK() { Connection conn = getConnection(); String username = tfUserName.getText(); String password = tfPassword.getText(); String result = login(conn, username, password); JOptionPane.showMessageDialog(null, result); } private void onCancel() { dispose(); } private Connection getConnection() { Connection connection = null; try { Class.forName(“com.mysql.cj.jdbc.Driver”); // 加载驱动程序 connection = DriverManager.getConnection( “jdbc:mysql://localhost:3306/sms”, “root”, “Succld3388”); // 取得数据库连接 } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } return connection; } /** * 具体操作方法:查询 */ public String login(Connection conn, String userName, String password){ String result = null; try{ String sql = “SELECT username, password FROM login WHERE username = ?”; PreparedStatement pstmt = null; //定义数据库操作对象 pstmt = conn.prepareStatement(sql); // 实例化操作 pstmt.setString(1, userName); // 设置用户名称 ResultSet rs = pstmt.executeQuery(); // 取得查询结果 if(rs.next()){ String pass = rs.getString(2); if (pass.equals(password)){ result = “登录成功”; }else{ result = “密码错误”; } }else{ result = “用户名不正确”; } }catch(Exception e){ result = “数据库错误”; } return result; } public static void main(String[] args) { LoginDialog dialog = new LoginDialog(); dialog.setLocationRelativeTo(null); dialog.setVisible(true); System.exit(0); } } |
发表评论 取消回复