【图书管理系统Java】

图书管理系统

用户/管理员登录

用户管理员登录

用户注册在这里插入图片描述

用户使用界面
在这里插入图片描述

管理员使用界面
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

`package Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Logon {
    public static JFrame frame;
    public static String no;
    public static String password;
    public static void main(String[] args) throws AWTException {
        Logon logon = new Logon();
    }
    public Logon(){
        //主界面窗口
        frame = new JFrame("图书管理系统");
        frame.setSize(400,300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false); //大小不可缩放
        frame.setLocationRelativeTo(null); //居中显示
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        panel.setLayout(null);
        //用户框
        JLabel userLabel = new JLabel("用户名:");
        userLabel.setFont(new Font(null, 3, 15));
        userLabel.setBounds(40,75,80,25);
        panel.add(userLabel);
        JTextField userText = new JTextField(20);
        userText.setFont(new Font(null, 3, 15));
        userText.setBounds(140,75,190,25);
        panel.add(userText);

        //密码框
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font(null, 3, 15));
        passwordLabel.setBounds(40, 120, 80, 25);
        panel.add(passwordLabel);
        JPasswordField passwordText = new JPasswordField(20);
        passwordLabel.setFont(new Font(null, 3, 15));
        passwordText.setBounds(140, 120, 190, 25);
        panel.add(passwordText);

        //用户类型
        JLabel user_class = new JLabel("用户类型:");
        user_class.setFont(new Font(null, 3, 15));
        user_class.setBounds(40,30,80,25);
        panel.add(user_class);
        JRadioButton radioButton1 = new JRadioButton("读者");
        radioButton1.setFont(new Font(null, 3, 15));
        radioButton1.setBounds(140,30,70,25);
        JRadioButton radioButton2 = new JRadioButton("管理员");
        radioButton2.setFont(new Font(null, 3, 15));
        radioButton2.setBounds(230,30,100,25);
        ButtonGroup BtGroup = new ButtonGroup();
        BtGroup.add(radioButton1);
        BtGroup.add(radioButton2);
        radioButton1.setSelected(true);
        panel.add(radioButton1);
        panel.add(radioButton2);


        //登陆按钮
        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font(null, 3, 15));
        loginButton.setBounds(70, 170, 100, 30);
        panel.add(loginButton);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userc = new String(); //用户类型
                String cno =new String(); //数据库用户名列名
                String ccode = new String(); //数据库密码列名
                no = userText.getText(); //数据库用户号
                password = new String(passwordText.getPassword()); //密码
                boolean Reader_b = radioButton1.isSelected();
                if(Reader_b == true) {
                    userc = "reader";
                    cno = "Rno";
                    ccode = "Rcode";
                }
                else {
                    userc = "administer";
                    cno = "Ano";
                    ccode = "Acode";
                }
                try {
                    if(no.length() == 0){
                        JOptionPane.showMessageDialog(frame,"用户名不能为空");
                    }
                    else if(password.length() == 0){
                        JOptionPane.showMessageDialog(frame,"密码不能为空");
                    }
                    else {
                        String sql = "select " + ccode + " from " + userc + " where " + cno + "= '" + no + "'";
                        Connection conn = Connect.getConnection();
                        PreparedStatement preparedStatement = conn.prepareStatement(sql);
                        ResultSet resultSet = preparedStatement.executeQuery();
                        if(resultSet.next()) {
                            String pass = resultSet.getString(ccode);
                            if (pass.equals(password)) {
                                if(Reader_b == true) {
                                    Reader reader = new Reader();
                                }
                                else{
                                    Manager manager = new Manager();
                                }
                            } else {
                                JOptionPane.showMessageDialog(frame, "用户名或密码错误");
                            }
                        }
                        else {
                            JOptionPane.showMessageDialog(frame, "用户名或密码错误");
                        }
                        Connect.close(conn, preparedStatement);
                        resultSet.close();
                    }
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
        //注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setFont(new Font(null, 3, 15));
        registerButton.setBounds(200, 170, 100, 30);
        panel.add(registerButton);
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Register register = new Register();
            }
        });
    }
}
`代码链接:https://pan.baidu.com/s/1-e7myPmcbIxGv7-ZRbgdyA 
提取码:9sq4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值