学生信息管理系统JAVA

 

package com.fwq.car.dao


import com.fwq.car.pojo.Student;
import com.fwq.car.pojo.User;
import com.fwq.car.utils.DaoUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;



//对数据库中student表进行操作
public class studentDao {

    public QueryRunner runner = new QueryRunner(DaoUtils.dataSource);
    public static Connection connection = null;
    static {
        try {
            connection = DaoUtils.openConn();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }


    //查询所有学生

    public List<Student> selectUserList() throws SQLException {
        String sql = "select * from student";
        return runner.query(connection,sql,new BeanListHandler<>(Student.class));
    }

    //根据id查询学生
    public Student selectUserByid(String id) throws SQLException {
        String sql = "select * from student where no = ?";
        return runner.query(connection,sql,new BeanHandler<>(Student.class),id);
    }

    //更新学生
    public Boolean updateUser(String id,String name,String sex,String brithday,String policital,String address,String tel,String dormitoryNo) throws SQLException {
        String sql = "update student set name = ?,sex = ?,brithday=?,policital=?,address=?,tel=?,dormitoryNo=? where no = ?";
        return runner.update(connection,sql,name,sex,brithday,policital,address,tel,dormitoryNo,id)>0?true:false;

    }

    //删除用户
    public Boolean delUser(Integer id) throws SQLException {
        String sql = "delete from student where no = ?";
        return runner.update(connection,sql,id)>0?true:false;

    }
    //添加学生
    public Boolean addUser(Student student) throws SQLException {
        String sql = "insert into student(no,name,sex,brithday,policital,address,tel,dormitoryNo) values (?,?,?,?,?,?,?,?)";
        return runner.update(connection,sql,student.getNo(),student.getName(),student.getSex(),student.getBrithday(),student.getPolicital(),student.getAddress(),student.getTel(),student.getDormitoryNo())>0?true:false;
    }
package com.fwq.car.dao;

import com.fwq.car.pojo.User;
import com.fwq.car.utils.DaoUtils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

//对数据库中user表进行操作


public class userDao {
    public QueryRunner runner = new QueryRunner(DaoUtils.dataSource);
    public static Connection connection = null;
    static {
        try {
            connection = DaoUtils.openConn();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    //查询所有用户
    public List<User> selectUserList() throws SQLException {
        String sql = "select * from user";
        return runner.query(connection,sql,new BeanListHandler<>(User.class));
    }
    //根据id查询用户
    public User selectUserByid(Integer id) throws SQLException {
        String sql = "select * from user where id = ?";
        return runner.query(connection,sql,new BeanHandler<>(User.class),id);
    }
    //添加用户
    public Boolean addUser(User user) throws SQLException {
        String sql = "insert into user(id,name,psd,type,money) values (null,?,?,?,?)";
        return runner.update(connection,sql,user.getName(),user.getPsd(),user.getType(),user.getMoney())>0?true:false;
    }
    //登录
    //根据id查询用户
    public User selectUserBylogin(String name,String psd,Integer role) throws SQLException {
        String sql = "select * from user where name = ? and psd = ? and type = ?";
        return runner.query(connection,sql,new BeanHandler<>(User.class),name,psd,role);
    }








}
package com.fwq.car.pojo;



public class Student {

    private String no;
    private String name;
    private String sex;
    private String brithday;
    private String policital;
    private String address;
    private String tel;
    private String dormitoryNo;

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBrithday() {
        return brithday;
    }

    public void setBrithday(String brithday) {
        this.brithday = brithday;
    }

    public String getPolicital() {
        return policital;
    }

    public void setPolicital(String policital) {
        this.policital = policital;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getDormitoryNo() {
        return dormitoryNo;
    }

    public void setDormitoryNo(String dormitoryNo) {
        this.dormitoryNo = dormitoryNo;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", brithday='" + brithday + '\'' +
                ", policital='" + policital + '\'' +
                ", address='" + address + '\'' +
                ", tel='" + tel + '\'' +
                ", dormitoryNo='" + dormitoryNo + '\'' +
                '}';
    }
}
package com.fwq.car.pojo;

public class User {

    public Integer id;
    public String name;
    public String psd;
    public Integer type;
    public Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPsd() {
        return psd;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", psd='" + psd + '\'' +
                ", type=" + type +
                ", money=" + money +
                '}';
    }
}
package com.fwq.car.utils;

import java.beans.PropertyVetoException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;

//连接数据库


public class DaoUtils {
    public static final ComboPooledDataSource dataSource = new ComboPooledDataSource();

    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    static {
        try {
            // 创建Properties集合类
            Properties pro = new Properties();
            // 获取src路径下文件,使用ClassLoader类加载器
            ClassLoader classLoader = DaoUtils.class.getClassLoader();
            // URL定位了文件的绝对路径
            URL res = classLoader.getResource("jdbc.properties");
            // 获取字符串路径
            String path = res.getPath();
            // 读取文件
            pro.load(new FileReader(path));
            // 给静态变量赋值
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            // 注册驱动
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    //获取连接对象
    public static Connection openConn() throws PropertyVetoException {
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
    //关闭连接
    public static void closeAll(Connection conn,Statement st, ResultSet rs)
    {
        try {
            if(conn !=null)
                conn.close();
            if(st!=null)
                st.close();
            if(rs!=null)
                rs.close();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}
package com.fwq.car.utils;

import com.fwq.car.pojo.*;

import java.util.List;




public class ListUtils {
    public static Object[][] toUserArray(List<User> list){


        Object[][] data=new Object[list.size()][4];
        for(int i=0;i<list.size();i++){
            User user = list.get(i);
            data[i][0]=user.getId();
            data[i][1]=user.getName();
            data[i][2]=user.getType()==1?"管理员":"普通用户";
            data[i][3]=user.getMoney();
        }
        return data;
    }

    public static Object[][] toStuArray(List<Student> list){


        Object[][] data=new Object[list.size()][8];
        for(int i=0;i<list.size();i++){
            Student student = list.get(i);


            data[i][0]=student.getNo();
            data[i][1]=student.getName();
            data[i][2]=student.getSex();
            data[i][3]=student.getBrithday();
            data[i][4]=student.getPolicital();
            data[i][5]=student.getAddress();
            data[i][6]=student.getTel();
            data[i][7]=student.getDormitoryNo();
        }
        return data;
    }





}
package com.fwq.car.views;

import com.fwq.car.dao.userDao;
import com.fwq.car.pojo.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class login {

    userDao userDao = new userDao();

    public void init(){

        JFrame jFrame = new JFrame();
        jFrame.setSize(400,300);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(null);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        JLabel jLabel1 = new JLabel("请输入用户名:");
        jLabel1.setBounds(50,10,100,30);
        JTextField jTextField1 = new JTextField();
        jTextField1.setBounds(160,10,150,30);


        JLabel jLabel2 = new JLabel("请输入密码:");
        jLabel2.setBounds(50,60,100,30);
        JPasswordField jTextField2 = new JPasswordField();
        jTextField2.setBounds(160,60,150,30);

//        JRadioButton jRadioButton1 = new JRadioButton("用户");
//        jRadioButton1.setSelected(true);
//        jRadioButton1.setBounds(80,110,80,30);
//        JRadioButton jRadioButton2 = new JRadioButton("管理员");
//        jRadioButton2.setBounds(200,110,80,30);
        JButton jButton = new JButton("登录");
        jButton.setBounds(150,150,80,30);
//        JButton jButton2 = new JButton("注册");
//        jButton2.setBounds(200,150,80,30);

        jFrame.add(jLabel1);
        jFrame.add(jTextField1);
        jFrame.add(jLabel2);
        jFrame.add(jTextField2);
//        jFrame.add(jRadioButton1);
//        jFrame.add(jRadioButton2);
        jFrame.add(jButton);
//        jFrame.add(jButton2);
        jFrame.setVisible(true);


//        //选择用户时 管理员取消选择
//        jRadioButton1.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                jRadioButton2.setSelected(false);
//            }
//        });
//
//        //选择管理员时 用户取消选择
//        jRadioButton2.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                jRadioButton1.setSelected(false);
//            }
//        });

        //登录的点击事件函数
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //主界面
                Mview_manager mview_manager = new Mview_manager();
//                Mview_user mview_user = new Mview_user();
                User user=null;

                String name = jTextField1.getText();
                String psd = jTextField2.getText();
//                int role = 0;
//                //如果选择 用户
//                if(jRadioButton1.isSelected()){
//                    role = 0;
//                }else { //选择管理员
//                    role = 1;
//                }
                try {
                    user = userDao.selectUserBylogin(name, psd, 1);
                    if(user==null){
                        JOptionPane.showMessageDialog(null,"用户名密码错误!");
                    }else {
                        JOptionPane.showMessageDialog(null,"登陆成功!");
                        //跳转主页面 前 清空控件的值
                        jTextField1.setText("");
                        jTextField2.setText("");
                        jFrame.setVisible(false);

                        //管理员登录
                        mview_manager.init(jFrame);

                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }

            }
        });
//
//        //注册按钮点击事件
//        jButton2.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//
//                jTextField1.setText("");
//                jTextField2.setText("");
//                //跳转注册页面
//                jFrame.setVisible(false);
//
//                register register = new register();
//                register.init(jFrame);
//
//
//            }
//        });
    }


}
package com.fwq.car.views;

import com.fwq.car.dao.studentDao;
import com.fwq.car.pojo.Student;
import com.fwq.car.utils.ListUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class Mview_manager {

    //设置 第一次加载主页面 标志
    boolean flag = true;

    //菜单panel
    JPanel jPanel1;

    //学生信息管理panel
    JPanel jPanel2;

    // 学生修改/添加
    JPanel jPanel3;



    DefaultTableModel dft = new DefaultTableModel();
    studentDao studentDao = new studentDao();
    String[] columnNames = new String[] { "学号", "姓名", "性别", "出生日期","政治面貌","家庭住址","电话","宿舍号" };

    public void init(JFrame loginFrame) throws SQLException {


        JFrame jFrame = new JFrame();
        jFrame.setSize(1200, 500);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(null);
        jFrame.setResizable(false);
        jFrame.setTitle("学生信息管理系统");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //创建菜单框
        jPanel1 = new JPanel();
        jPanel1.setLayout(null);
        jPanel1.setBorder(BorderFactory.createTitledBorder("菜单"));
        jPanel1.setBounds(0, 0, 150, 495);

        JLabel label = new JLabel("学号");
        JTextField textField = new JTextField();

        JButton button1 = new JButton("学生查询");
        JButton button2 = new JButton("学生修改");
        JButton button3 = new JButton("学生删除");
        JButton button4 = new JButton("学生添加");
        JButton button5 = new JButton("退出登录");

        label.setBounds(25, 50, 50, 30);
        textField.setBounds(50, 50, 85, 30);


        button1.setBounds(25, 100, 110, 30);
        button2.setBounds(25, 150, 110, 30);
        button3.setBounds(25, 200, 110, 30);
        button4.setBounds(25, 250, 110, 30);
        button5.setBounds(25, 300, 110, 30);

        jPanel1.add(label);
        jPanel1.add(textField);

        jPanel1.add(button1);
        jPanel1.add(button2);
        jPanel1.add(button3);
        jPanel1.add(button4);
        jPanel1.add(button5);


        //创建用户内容框

        jPanel2 = new stuManager(dft).userPanel();
        studentInfo studentInfo = new studentInfo();
        jPanel3 = studentInfo.studentInfoPanel();
        jPanel2.setVisible(true);
        jPanel3.setVisible(false);

        jFrame.add(jPanel1);
        jFrame.add(jPanel2);
        jFrame.add(jPanel3);

        jFrame.setVisible(true);


        //学生查询
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("查询");

                //隐藏控件 清空数据
                String input_id = textField.getText();
                textField.setText("");
                jPanel2.setVisible(true);
                jPanel3.setVisible(false);


                //输入框为空
                if("".equals(input_id)){
                    //查询全部
                    try {
                        Object[][] objects = ListUtils.toStuArray(studentDao.selectUserList());
                        dft.setDataVector(objects,columnNames);
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }

                }else {
                    //按照id查询用户
                    List<Student> list = new ArrayList<>();

//                    User user = null;
                    Student student = null;
                    try {

                        student = studentDao.selectUserByid(input_id);
                        if(student!=null){
                            list.add(student);
                            Object[][] objects = ListUtils.toStuArray(list);
                            dft.setDataVector(objects,columnNames);
                        }else {
                            JOptionPane.showMessageDialog(null,"没有该用户!");
                        }

                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }


                }


            }
        });



        //学生修改
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("修改");
                String input_id = textField.getText();
                if("".equals(input_id)){
                    JOptionPane.showMessageDialog(null,"请输入用户id");
                }else{


                    Student student = null;
                    try {
                        student = studentDao.selectUserByid(input_id);


                        if(student!=null){
                            jPanel2.setVisible(false);
                            jPanel3.setVisible(true);

                            studentInfo.textField.setText(student.getNo());
                            studentInfo.textField.setEditable(false);
                            studentInfo.textField2.setText(student.getName());
                            studentInfo.cmb.setSelectedItem(student.getPolicital());
                            studentInfo.textField4.setText(student.getBrithday());
                            studentInfo.cmb2.setSelectedItem(student.getPolicital());
                            studentInfo.textField6.setText(student.getAddress());
                            studentInfo.textField7.setText(student.getTel());
                            studentInfo.textField8.setText(student.getDormitoryNo());
                            studentInfo.jButton.setText("确认修改");


                        }else {
                            JOptionPane.showMessageDialog(null,"该学生不存在");
                        }


                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }








                }


            }
        });


        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("删除信息");
                String input_id = textField.getText();
                if("".equals(input_id)){
                    JOptionPane.showMessageDialog(null,"请输入用户id");
                }else{


                    int opt = JOptionPane.showConfirmDialog(jPanel2,
                            "是否删除该学生信息?", "确认信息",
                            JOptionPane.YES_NO_OPTION);
                    if (opt == JOptionPane.YES_OPTION) {
                        //根据id删除用户
                        try {
                            Boolean flag = studentDao.delUser(Integer.valueOf(input_id));
                            if(flag){
                                JOptionPane.showMessageDialog(null,"删除成功!");
                                textField.setText("");
                                button1.doClick();

                            }else {
                                JOptionPane.showMessageDialog(null,"删除失败!");
                            }
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }


                }
            }

        });


        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("添加");


                studentInfo.textField.setText("");
                studentInfo.textField.setEditable(true);
                studentInfo.textField2.setText("");
                studentInfo.cmb.setSelectedIndex(0);
                studentInfo.textField4.setText("");
                studentInfo.cmb2.setSelectedIndex(0);
                studentInfo.textField6.setText("");
                studentInfo.textField7.setText("");
                studentInfo.textField8.setText("");

                studentInfo.jButton.setText("确认添加");

                jPanel2.setVisible(false);
                jPanel3.setVisible(true);



            }
        });



        //退出登录按钮点击事件
        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("退出登录");
                jFrame.setVisible(false);
                loginFrame.setVisible(true);

            }
        });

    }

}

package com.fwq.car.views;

import com.fwq.car.dao.studentDao;
import com.fwq.car.pojo.Student;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;


//添加学生

public class studentInfo {

    studentDao studentDao = new studentDao();


    JTextField textField = new JTextField();
    JTextField textField2 = new JTextField();
//    JTextField textField3 = new JTextField();

    JComboBox cmb = new JComboBox();    //创建JComboBox


    JTextField textField4 = new JTextField();
    //    JTextField textField5 = new JTextField();
    JComboBox cmb2 = new JComboBox();
    JTextField textField6 = new JTextField();
    JTextField textField7 = new JTextField();
    JTextField textField8 = new JTextField();

    JButton jButton = new JButton();

    public JPanel studentInfoPanel() throws SQLException {

        //创建内容框
        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setBorder(BorderFactory.createTitledBorder("学生信息"));
        jPanel.setBounds(155, 0, 1000, 495);

        JLabel label = new JLabel("学号");
//        JTextField textField = new JTextField();
        label.setBounds(50, 50, 50, 30);
        textField.setBounds(85, 50, 140, 30);

        JLabel label2 = new JLabel("姓名");
//        JTextField textField2 = new JTextField();
        label2.setBounds(50, 100, 50, 30);
        textField2.setBounds(85, 100, 140, 30);

        JLabel label3 = new JLabel("性别");
//        JTextField textField3 = new JTextField();
        label3.setBounds(50, 150, 50, 30);
        cmb.setBounds(85, 150, 140, 30);
        cmb.addItem("--请选择--");
        cmb.addItem("男");
        cmb.addItem("女");

        JLabel label4 = new JLabel("出生日期");
//        JTextField textField4 = new JTextField();
        label4.setBounds(50, 200, 100, 30);
        textField4.setBounds(115, 200, 110, 30);

        JLabel label5 = new JLabel("政治面貌");
//        JTextField textField5 = new JTextField();
        label5.setBounds(250, 50, 100, 30);
        cmb2.setBounds(325, 50, 185, 30);
        cmb2.addItem("--请选择--");
        cmb2.addItem("群众");
        cmb2.addItem("团员");
        cmb2.addItem("党员");

        JLabel label6 = new JLabel("家庭住址");
//        JTextField textField6 = new JTextField();
        label6.setBounds(250, 100, 100, 30);
        textField6.setBounds(325, 100, 185, 30);

        JLabel label7 = new JLabel("电话");
//        JTextField textField7 = new JTextField();
        label7.setBounds(250, 150, 50, 30);
        textField7.setBounds(290, 150, 220, 30);

        JLabel label8 = new JLabel("宿舍号");
//        JTextField textField8 = new JTextField();
        label8.setBounds(250, 200, 75, 30);
        textField8.setBounds(300, 200, 210, 30);


        jButton.setBounds(300, 300, 100, 40);



        jPanel.add(label);
        jPanel.add(textField);
        jPanel.add(label2);
        jPanel.add(textField2);
        jPanel.add(label3);
        jPanel.add(cmb);
        jPanel.add(label4);
        jPanel.add(textField4);
        jPanel.add(label5);
        jPanel.add(cmb2);
        jPanel.add(label6);
        jPanel.add(textField6);
        jPanel.add(label7);
        jPanel.add(textField7);
        jPanel.add(label8);
        jPanel.add(textField8);
        jPanel.add(jButton);


        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if("确认修改".equals(jButton.getText())){
                    System.out.println("确认修改");

                    String no = textField.getText();
                    String name = textField2.getText();
                    String sex = (String) cmb.getSelectedItem();
                    String brithday = textField4.getText();
                    String policital = cmb2.getSelectedItem().toString();
                    String address = textField6.getText();
                    String tel = textField7.getText();
                    String dormitoryNo = textField8.getText();

                    if (name.equals("")||sex.equals("--请选择--")||brithday.equals("")||policital.equals("--请选择--")||address.equals("")||tel.equals("")||dormitoryNo.equals("")){
                        JOptionPane.showMessageDialog(null,"您的输入有误请检查");
                    }else {
                        try {
                            Boolean flag = studentDao.updateUser(no, name, sex, brithday, policital, address, tel, dormitoryNo);

                            if(flag){
                                JOptionPane.showMessageDialog(null,"修改成功");
                            }else {
                                JOptionPane.showMessageDialog(null,"修改失败");
                            }

                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }

                }else {
                    System.out.println("确认添加");


                    String no = textField.getText();
                    String name = textField2.getText();
                    String sex =  cmb.getSelectedItem().toString();
                    String brithday = textField4.getText();
                    String policital = cmb2.getSelectedItem().toString();
                    String address = textField6.getText();
                    String tel = textField7.getText();
                    String dormitoryNo = textField8.getText();

                    if (name.equals("")||sex.equals("--请选择--")||brithday.equals("")||policital.equals("--请选择--")||address.equals("")||tel.equals("")||dormitoryNo.equals("")){
                        JOptionPane.showMessageDialog(null,"您的输入有误请检查");
                    }else {
                        Student student = new Student();
                        student.setNo(no);
                        student.setName(name);
                        student.setSex(sex);
                        student.setBrithday(brithday);
                        student.setPolicital(policital);
                        student.setAddress(address);
                        student.setTel(tel);
                        student.setDormitoryNo(dormitoryNo);

                        try {
                            Boolean flag = studentDao.addUser(student);

                            if(flag){
                                JOptionPane.showMessageDialog(null,"添加成功");
                            }else {
                                JOptionPane.showMessageDialog(null,"添加失败");
                            }

                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }



                }
            }
        });




        return jPanel;
    }

}

package com.fwq.car.views;

import com.fwq.car.dao.studentDao;
import com.fwq.car.pojo.Student;
import com.fwq.car.utils.ListUtils;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.sql.SQLException;

import java.util.List;

//修改学生

public class stuManager extends JFrame {


    JTable table;
    JScrollPane sp;
    DefaultTableModel dft;

    studentDao studentDao = new studentDao();
    String[] columnNames = new String[] { "学号", "姓名", "性别", "出生日期","政治面貌","家庭住址","电话","宿舍号" };

    public stuManager(DefaultTableModel defaultTableModel) {
        this.dft = defaultTableModel;
    }

    public JPanel userPanel() throws SQLException {

        //创建内容框
        JPanel jPanel2 = new JPanel();
        jPanel2.setLayout(null);
        jPanel2.setBorder(BorderFactory.createTitledBorder("学生信息"));
        jPanel2.setBounds(155, 0, 1000, 495);

        List<Student> students = studentDao.selectUserList();
        Object[][] users = ListUtils.toStuArray(students);


        dft.setDataVector(users,columnNames);
        table = new JTable(dft);
        table.setRowHeight(30);
        sp = new JScrollPane(table);
        sp.setBounds(50,100,900,250);


        //修改用户信息控件
        JLabel jLabel2 = new JLabel("姓名");
        jLabel2.setBounds(35,400,40,20);
        JTextField jTextField2 = new JTextField();
        jTextField2.setBounds(65,400,50,20);


        JLabel jLabel3 = new JLabel("密码");
        jLabel3.setBounds(120,400,40,20);
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setBounds(150,400,50,20);

        JLabel jLabel4 = new JLabel("余额");
        jLabel4.setBounds(200,400,40,20);
        JTextField jTextField4 = new JTextField();
        jTextField4.setBounds(230,400,50,20);



        JButton jButton10 = new JButton("确认修改");
        jButton10.setBounds(300,400,90,20);

        JButton jButton11 = new JButton("确认添加");
        jButton11.setBounds(390,400,90,20);



        jPanel2.add(sp);



        jPanel2.add(jLabel2);
        jPanel2.add(jLabel3);
        jPanel2.add(jLabel4);
        jPanel2.add(jTextField2);
        jPanel2.add(jPasswordField);
        jPanel2.add(jTextField4);
        jPanel2.add(jButton10);
        jPanel2.add(jButton11);



        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jTextField2.setVisible(false);
        jTextField4.setVisible(false);
        jPasswordField.setVisible(false);
        jButton10.setVisible(false);
        jButton11.setVisible(false);





        return jPanel2;
    }
}


package com.fwq.car;

import com.fwq.car.views.login;

public class Main {

    public static void main(String[] args) {
        login login = new login();
        login.init();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值