IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> java 学生信息管理系统 -> 正文阅读

[Java知识库]java 学生信息管理系统

本章为我的期末项目,参考意义不大。其中有参考别人(howard2005)的代码和错误。
在这里插入图片描述

创建用户实体类User

public class User {
    private int id;
    private String username;
    private String password;
    private String telephone;
    private Date registerTime;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public Date getRegisterTime() {
        return registerTime;
    }
    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", telephone='" + telephone + '\'' +
                ", registerTime=" + registerTime +
                '}';
    }
}

数据库连接管理类ConnectionManager

public class ConnectionManager {// 数据库连接属性
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false";
    private static final String USER = "root";
    private static final String PASSWORD = "123456"; 
    /**
     * 私有化构造方法,拒绝实例化
     */
    private ConnectionManager() {
    }
    /**
     * 获取数据库连接静态方法
     *
     * @return 数据库连接
     */
    public static Connection getConnection() { // 定义数据库连接
        Connection conn = null;
        try {// 安装数据库驱动
            Class.forName(DRIVER); // 获取数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);// 提示用户数据库连接成功
            System.out.println("提示:数据库连接成功!");
        } catch (ClassNotFoundException e) {
            System.err.println("异常:数据库驱动程序未找到!");
        } catch (SQLException e) {
            System.err.println("异常:数据库连接失败!");
        }
        // 返回数据库连接
        return conn;
    }
    /**
     * 关闭数据库连接静态方法
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) { // 判断连接是否为空
        if (conn != null) {
            try {
                // 判断连接是否关闭
                if (!conn.isClosed()) {// 关闭数据库连接,释放资源
                    conn.close();
                    // 提示用户
                    System.out.println("提示:数据库连接关闭!");
                }
            } catch (SQLException e) {
                System.err.println(e.getMessage());
            }
        }
    }
    /**
     * 主方法测试数据库连接
     *
     * @param args
     */
    public static void main(String[] args) { // 获取数据库连接
        Connection conn = getConnection();// 关闭数据库连接
        closeConnection(conn);
    }
}

运行结果:
在这里插入图片描述

用户数据访问接口UserDao

public interface UserDao {
    int insert(User user);
    int delete(int id);
    int update(User user);
    User findById(int id);
    List<User> findAll();
    User login(String username, String password);
}

用户数据访问接口实现类UserDaoImpl

public class UserDaoImpl implements UserDao {
    @Override
    public int insert(User user) {
        return 0;
    }
    @Override
    public int delete(int id) {
        return 0;
    }
    @Override
    public int update(User user) {
        return 0;
    }
    @Override
    public User findById(int id) {
        return null;
    }
    @Override
    public List<User> findAll() {
        return null;
    }
    @Override
    public User login(String username, String password) {// 定义用户对象                                                                                
    User user = null;                                                                                                                                                                 
    // 1. 获取数据库连接                                                                            
    Connection conn = ConnectionManager.getConnection();                                     
    // 2. 创建SQL字符串                                                                           
    String strSQL = "select * from t_user where username = ? and password = ?";              
    try { // 3. 创建预备语句对象(准备提供参数)                                                               
        PreparedStatement pstmt = conn.prepareStatement(strSQL);  // 4. 用参数值设置占位符的值                                                                    
        pstmt.setString(1, username);                                                        
        pstmt.setString(2, password);                                                        
        // 5. 执行SQL查询,返回结果集                                                                 
        ResultSet rs = pstmt.executeQuery();                                                 
        // 6. 遍历结果集,用记录值填充用户对象                                                               
        while (rs.next()) {                                                                  
            // 创建用户对象   
            user = new User();                                                               
            // 用当前记录字段值设置用户对象属性                                                              
            user.setId(rs.getInt("id"));                                                     
            user.setUsername(rs.getString("username"));                                      
            user.setPassword(rs.getString("password"));                                      
            user.setTelephone(rs.getString("telephone"));                                    
            user.setRegisterTime(rs.getTimestamp("register_time"));                          
        }                                                                                    
    } catch (SQLException e) {                                                               
        System.err.println(e.getMessage());                                                  
    } finally {
        ConnectionManager.closeConnection(conn);
    }   
     // 返回用户对象                                                                                
    return user;                                                                             
}                       
    }
}

对UserDaoImpl进行单元测试TestUserDaoImpl

public class TestUserDaoImpl {    
    private UserDao userDao;    
    @Before
    public void init() {
        userDao = new UserDaoImpl();    
    }    
    @Test
    public void testLogin() {                                         
    String username = "admin";                                    
    String password = "11111";                                                                            
    User user = userDao.login(username, password);                
    if (user != null) {                                           
        System.out.println("恭喜,[" + username + "]登录成功~");         
    } else {                                                      
        System.out.println("遗憾,[" + username + "]登录失败~");         
    }                                                             
}

用户服务类UserService

public class UserService {
    private UserDao userDao;
    public UserService() {
        userDao = new UserDaoImpl();
    }
    public int addUser(User user) {
        return userDao.insert(user);
    }
    public int deleteUser(int id) {
        return userDao.delete(id);
    }
    public int updateUser(User user) {
        return userDao.update(user);
    }
    public User findUserById(int id) {
        return userDao.findById(id);
    }
    public List<User> findAllUsers() {
        return userDao.findAll();
    }
    public User login(String username, String password) {
        return userDao.login(username, password);
    }
}

用户登录界面LoginFrame

public class LoginFrame extends JFrame {
    private String username;
    private String password;
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JTextField txtUsername;
    private JPasswordField txtPassword;
    private JButton btnOK;
    private JButton btnCancel;
    private JPanel panel, panel1, panel2, panel3;
    /**
     * 有参构造方法
     *
     * @param title
     */
    public LoginFrame(String title) {
        super(title);
        initGUI(); // 调用初始化图形用户界面方法
    }
    /**
     * 初始化图形用户界面方法
     */
    private void initGUI() {
        // 实例化组件(面板与控件)
        panel = (JPanel) getContentPane();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        lblUsername = new JLabel("用户名:");
        lblPassword = new JLabel("密    码:");
        txtUsername = new JTextField(15);
        txtPassword = new JPasswordField(15);
        btnOK = new JButton("确定[O]");
        btnCancel = new JButton("取消[C]");
        // 将控件添加到三个小面板
        panel1.add(lblUsername);
        panel1.add(txtUsername);
        panel2.add(lblPassword);
        panel2.add(txtPassword);
        panel3.add(btnOK);
        panel3.add(btnCancel);
        // 设置主面板为三行一列的网格布局
        panel.setLayout(new GridLayout(3, 1));
        // 将三个小面板依次添加到主面板
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        // 设置按钮热键字母
        btnOK.setMnemonic(KeyEvent.VK_O);
        btnCancel.setMnemonic(KeyEvent.VK_C);
        // 设置密码框回显字符
        txtPassword.setEchoChar('*');
        // 设置窗口大小
        setSize(250, 200);
        // 设置窗口屏幕居中
        setLocationRelativeTo(null);
        // 设置窗口不可调整大小
        setResizable(false);
        // 设置窗口刚好容纳组件
        pack();
        // 设置窗口可见
        setVisible(true);
        // 设置窗口默认关闭操作
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args) {
        new LoginFrame("用户登录");
    }
}

用户登录功能LoginFrame

public class LoginFrame extends JFrame {
    private String username;
    private String password;
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JTextField txtUsername;
    private JPasswordField txtPassword;
    private JButton btnOK;
    private JButton btnCancel;
    private JPanel panel, panel1, panel2, panel3;
    /**
     * 有参构造方法
     *
     * @param title
     */
    public LoginFrame(String title) {
        super(title);
        initGUI(); // 调用初始化图形用户界面方法
        eventsHandling();// 调用事件处理方法
    }
    /**
     * 事件处理
     */
    private void eventsHandling() {
        // 给【确定】按钮注册监听器
        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取用户输入的用户名和密码
                username = txtUsername.getText().trim();
                password = new String(txtPassword.getPassword());
                // 创建用户服务对象
                UserService userService = new UserService();
                // 调用服务对象的登录方法
                User user = userService.login(username, password);
                // 判断用户登录是否成功
                if (user != null) {
                    // 弹出消息框提示用户
                    JOptionPane.showMessageDialog(null, "恭喜,[" + username + "]登录成功!");
                    dispose(); // 关闭登录窗口
                } else {
                    // 弹出消息框提示用户
                    JOptionPane.showMessageDialog(null, "遗憾,用户名或密码错误!");
                    // 清空两个文本框
                    txtUsername.setText("");
                    txtPassword.setText("");
                    // 让姓名文本框获取焦点
                    txtUsername.requestFocus();
                }
            }
        });
        // 给【取消】按钮注册监听器
        btnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0); // 退出应用程序
            }
        });
    }
    /**
     * 初始化图形用户界面方法
     */
    private void initGUI() {
        // 实例化组件(面板与控件)
        panel = (JPanel) getContentPane();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        lblUsername = new JLabel("用户名:");
        lblPassword = new JLabel("密码:");
        txtUsername = new JTextField(15);
        txtPassword = new JPasswordField(15);
        btnOK = new JButton("确定[O]");
        btnCancel = new JButton("取消[C]");
        // 将控件添加到三个小面板
        panel1.add(lblUsername);
        panel1.add(txtUsername);
        panel2.add(lblPassword);
        panel2.add(txtPassword);
        panel3.add(btnOK);
        panel3.add(btnCancel);
        // 设置主面板为三行一列的网格布局
        panel.setLayout(new GridLayout(3, 1));
        // 将三个小面板依次添加到主面板
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        // 设置按钮热键字母
        btnOK.setMnemonic(KeyEvent.VK_O);
        btnCancel.setMnemonic(KeyEvent.VK_C);
        // 设置密码框回显字符
        txtPassword.setEchoChar('*');
        // 设置窗口大小
        setSize(250, 200);
        // 设置窗口屏幕居中
        setLocationRelativeTo(null);
        // 设置窗口不可调整大小
        setResizable(false);
        // 设置窗口刚好容纳组件
        pack();
        // 设置窗口可见
        setVisible(true);
        // 设置窗口默认关闭操作
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args) {
        new LoginFrame("用户登录");
    }
}

注册账号Register

public class Register extends JFrame {
    JLabel username;
    JLabel password;
    JLabel passwordAgain;
    JTextField usernameFile;
    JPasswordField passwordFile;
    JPasswordField PasswordFileAgain;
    JButton sign;
    JPanel p;
    JPanel p1;
    final int WIDTH = 310;
    final int HEIGHT = 510;
    public Register() {
        init();
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        validate();
        setVisible(true);
        Toolkit kit = Toolkit.getDefaultToolkit();
        int width = screenSize.width;
        int height = screenSize.height;
        int x = (width - WIDTH) / 2;
        int y = (height - HEIGHT) / 2;
        setBounds(x, y, WIDTH, HEIGHT);
        setTitle("账号注册");
    }
    void init() {
        p = new JPanel();
        p.setLayout(null);
        p.setVisible(true);
        p1 = new JPanel();
        p1.setVisible(true);
        p1.setLayout(null);
        p1.setBounds(0, 0, 400, 600);
        username = new JLabel("账号:");
        password = new JLabel("密码:");
        passwordAgain = new JLabel("确认密码:");
        usernameFile = new JTextField(15);
        passwordFile = new JPasswordField(15);
        PasswordFileAgain = new JPasswordField(15);
        passwordFile.setEchoChar('*');
        PasswordFileAgain.setEchoChar('*');
        sign = new JButton("注册");
        sign.setBounds(160, 300, 100, 50);
        username.setBounds(130, 150, 40, 15);
        password.setBounds(130, 200, 40, 15);
        passwordAgain.setBounds(130, 250, 120, 15);
        usernameFile.setBounds(200, 150, 100, 15);
        passwordFile.setBounds(200, 200, 100, 15);
        PasswordFileAgain.setBounds(200, 250, 100, 15);
        p1.add(username);
        p1.add(password);
        p1.add(passwordAgain);
        p1.add(usernameFile);
        p1.add(passwordFile);
        p1.add(PasswordFileAgain);
        p1.add(sign);
        p.add(p1);
        this.add(p);
        sign.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String strusername = usernameFile.getText();
                String strpassword = new String(passwordFile.getPassword());
                String strpasswordAgain = new String(PasswordFileAgain.getPassword());
                UserDaoImpl userDaoImpl = new UserDaoImpl();
                try {
                    User users = sqlHelp.userpassweord(strusername); 
                    if (users == null) {
                        if (strpassword.equals(strpasswordAgain)) {
                            System.out.println("注册成功!");
                            String username = strusername;
                            String password = strpassword;
                            sqlHelp.addManeger(username,password);
                            JOptionPane.showMessageDialog(null, "注册成功!", "点击确定后返回登录界面", JOptionPane.INFORMATION_MESSAGE); 
                        } else {
                            System.out.println("注册失败!");
                            JOptionPane.showMessageDialog(null, "注册失败,请检查两次密码是否相同!", "登录失败提示窗口", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }else{
                        System.out.println("已有账号无法注册!");
                        JOptionPane.showMessageDialog(null,"注册失败了,已拥有该账号,请检查密码是否输入错误,目前不支持找回密码","账号已有窗口",JOptionPane.INFORMATION_MESSAGE);
//                        dispose();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] args) {
        new Register();
    }

}

查询窗口Query

public class Query extends JFrame {
    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;
    private JTextField searchField;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    QueryFrame frame = new QueryFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Query() {
        setResizable(false);
        setTitle("查询信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);
        JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel.setBounds(71, 0, 208, 29);
        panel.add(lblNewLabel);
        JLabel lblNewLabel_1 = new JLabel("学号:");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1.setBounds(71, 105, 50, 30);
        panel.add(lblNewLabel_1);
        idField = new JTextField();
        idField.setEditable(false);
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);
        JLabel lblNewLabel_1_1 = new JLabel("姓名:");
        lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1.setBounds(71, 160, 50, 30);
        panel.add(lblNewLabel_1_1);
        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);
        JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
        lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
        panel.add(lblNewLabel_1_1_1);
        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);
        JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
        lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
        panel.add(lblNewLabel_1_1_1_1);
        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);
        JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
        lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
        panel.add(lblNewLabel_1_1_1_2);
        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);
        searchField = new JTextField();
        searchField.setBounds(71, 39, 208, 45);
        panel.add(searchField);
        searchField.setColumns(10);
        JButton searchButton = new JButton("查找");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int id = Integer.parseInt(searchField.getText());
                try {
                    UserDaoImpl userDaoImpl = new UserDaoImpl();
                    Student student = userDaoImpl.queryStudent(id);
                    if(student != null) {
                        idField.setText(String.valueOf(id));
                        nameField.setText(student.getName());
                        genderField.setText(student.getGender());
                        dobField.setText(student.getDob());
                        batchField.setText(String.valueOf(student.getBatch()));
                    } else {
                        JOptionPane.showMessageDialog(QueryFrame.this, "查无此人!");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
        searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
        searchButton.setBounds(289, 42, 97, 39);
        panel.add(searchButton);
    }
}

修改信息 Change

public class ChangeFrame extends JFrame {
    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;
    private JTextField searchField;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChangeFrame frame = new ChangeFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public ChangeFrame() {
        setResizable(false);
        setTitle("修改信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);
        JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel.setBounds(71, 0, 208, 29);
        panel.add(lblNewLabel);
        JLabel lblNewLabel_1 = new JLabel("学号:");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1.setBounds(71, 105, 50, 30);
        panel.add(lblNewLabel_1);
        idField = new JTextField();
        idField.setEditable(false);
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);
        JLabel lblNewLabel_1_1 = new JLabel("姓名:");
        lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1.setBounds(71, 160, 50, 30);
        panel.add(lblNewLabel_1_1);
        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);
        JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
        lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
        panel.add(lblNewLabel_1_1_1);
        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);
        JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
        lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
        panel.add(lblNewLabel_1_1_1_1);
        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);
        JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
        lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
        panel.add(lblNewLabel_1_1_1_2);
        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);
        JButton changeButton = new JButton("修改");
        changeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int id = Integer.parseInt(searchField.getText());
                String nameString = nameField.getText();
                String gendeString = genderField.getText();
                String dobString = dobField.getText();
                String batch = batchField.getText();
                UserDaoImpl userDaoImpl = new UserDaoImpl();
                try {
                    userDaoImpl.changeStudent(id, nameString, gendeString, dobString, batch);
                    JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");
                    e.printStackTrace();
                }
            }
        });
        changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
        changeButton.setBounds(182, 389, 97, 33);
        panel.add(changeButton);
        searchField = new JTextField();
        searchField.setBounds(71, 39, 208, 45);
        panel.add(searchField);
        searchField.setColumns(10);
        JButton searchButton = new JButton("查找");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                int id = Integer.parseInt(searchField.getText());
                try {
                    UserDaoImpl userDaoImpl = new UserDaoImpl();
                    Student student = userDaoImpl.queryStudent(id);
                    if(student != null) {
                        idField.setText(String.valueOf(id));
                        nameField.setText(student.getName());
                        genderField.setText(student.getGender());
                        dobField.setText(student.getDob());
                        batchField.setText(String.valueOf(student.getBatch()));
                    } else {
                        JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
        searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
        searchButton.setBounds(289, 42, 97, 39);
        panel.add(searchButton);
    }
}

添加信息Add

public class Add extends JFrame {
    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Add frame = new Add();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Add() {
        setResizable(false);
        setTitle("添加学生");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);
        JLabel TitleLabel = new JLabel("请输入新学生的信息:");
        TitleLabel.setFont(new Font("宋体", Font.BOLD, 20));
        TitleLabel.setBounds(71, 34, 220, 45);
        panel.add(TitleLabel);
        JLabel idLabel = new JLabel("学号:");
        idLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        idLabel.setBounds(71, 105, 50, 30);
        panel.add(idLabel);
        idField = new JTextField();
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);
        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        nameLabel.setBounds(71, 160, 50, 30);
        panel.add(nameLabel);
        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);
        JLabel genderLabel = new JLabel("性别:");
        genderLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        genderLabel.setBounds(71, 215, 50, 30);
        panel.add(genderLabel);
        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);
        JLabel dobLabel = new JLabel("出生日期:");
        dobLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        dobLabel.setBounds(41, 270, 80, 30);
        panel.add(dobLabel);
        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);
        JLabel batchLabel = new JLabel("班级:");
        batchLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        batchLabel.setBounds(71, 325, 50, 30);
        panel.add(batchLabel);
        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);
        JButton addButton = new JButton("添加");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int id = Integer.parseInt(idField.getText());
                String nameString = nameField.getText();
                String genderString = genderField.getText();
                String dobfieldString = dobField.getText();
                int batch = Integer.parseInt(batchField.getText());
                System.out.println(id + "\t" + nameString + "\t" + genderString + "\t" + dobfieldString + "\t" + batch);
                UserDaoImpl userDaoImpl = new UserDaoImpl();
                try {
                    userDaoImpl.addStudent(id, nameString, genderString, dobfieldString, batch);
                    JOptionPane.showMessageDialog(AddFrame.this, "添加成功!");
                } catch (SQLException e1) {
                   
                   
                    if(e1.getSQLState().equals("23000")) {
                        JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在");
                    }
                    e1.printStackTrace();
                }

            }
        });
        addButton.setFont(new Font("宋体", Font.PLAIN, 18));
        addButton.setBounds(182, 389, 97, 33);
        panel.add(addButton);
    }
}


  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章           查看所有文章
加:2022-06-14 22:21:09  更:2022-06-14 22:28:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 18:31:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码