Java项目开发:学生社团管理系统
@author:Mr.Gu
@date:2021/5/31
文章目录
- Java项目开发:学生社团管理系统
- 项目需求
- 开发环境
- 项目效果
- 二、项目源码
- 1.创建登录Jframe
- 2.创建普通用户接口类
- 3.创建查看社团信息类JFrame
- 4.创建其他功能JFrame与上述方法同,因代码过于多,不一一展示
项目需求
要求制作一个社团管理系统,要求链接数据库,实现普通用户注册社团,正确登录社团,实现用户可以浏览社团信息,创建社团,且只能创建一个社团,可以申请加入社团,团长
可以进入社团管理系统,对社团进行管理,发布活动等等。
开发环境
eclipse2018版
mysql5.7
jdk1.8
windowsbuilder
项目效果
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、项目源码
1.创建登录Jframe
public class LogFrm extends JFrame implements ActionListener {
boolean flag;
private JPanel contentPane;
private JTextField nametf;
private JPasswordField passwordField;
User user = new User();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LogFrm frame = new LogFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LogFrm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 596, 771);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("编辑注册");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("注册账号");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Regist1 r = new Regist1();
r.setVisible(true);
}
});
menu.add(menuItem);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("社团管理系统");
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 37));
lblNewLabel.setBounds(162, 154, 368, 122);
contentPane.add(lblNewLabel);
nametf = new JTextField();
nametf.setBounds(162, 331, 223, 27);
contentPane.add(nametf);
nametf.setColumns(10);
JLabel lblNewLabel_1 = new JLabel(" 学号 :");
lblNewLabel_1.setBounds(40, 334, 81, 21);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("密码 :");
lblNewLabel_2.setBounds(50, 411, 81, 21);
contentPane.add(lblNewLabel_2);
JButton button = new JButton("重置");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rest();
}
});
button.setBounds(88, 539, 123, 29);
contentPane.add(button);
JButton btnNewButton = new JButton("登录");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
login();
} catch (HeadlessException | SQLException e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(340, 539, 123, 29);
contentPane.add(btnNewButton);
passwordField = new JPasswordField();
passwordField.setBounds(162, 408, 223, 27);
contentPane.add(passwordField);
this.setLocationRelativeTo(null);
}
protected void login() throws HeadlessException, SQLException {
boolean xyz = false;
Mysql1 mysql = new Mysql1();
mysql.connectSQL();
String nameText = nametf.getText();
String pwdText = new String(passwordField.getText());
User user = new User(nameText, pwdText);
/**
* 用于数据库连接
*/
Connection connection = null;
if (StringUtil.isEmpty(nameText)) {
JOptionPane.showMessageDialog(null, "用户名为空");
} else {
if (StringUtil.isEmpty(pwdText)) {
JOptionPane.showMessageDialog(null, "密码不能为空");
} else {
try {
this.flag = mysql.loginMatch(nameText, pwdText);
} catch (SQLException e2) {
e2.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
} // 清空输入栏
rest();
// 如果用户名判断成功则关闭login窗口并打开Spider窗口,否则状态栏显示loadfail;
if (flag) {
this.dispose();
setall(nametf.getText());
MainFrm mainFrm = new MainFrm(setall(nameText));
mainFrm.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
}
}
}
}
private User setall(String id) throws SQLException {
String url = "jdbc:mysql://localhost:3306/student?useSSL=false";// url是固定的
// jdbc:mysql://localhost:3036/数据库名
String user3 = "root";// 这里一般是root
String password = "root";// 这是登陆数据库的密码,我的密码为空
String word3 = "select * from userinfo";
ResultSet resultSet = null;
boolean x = false;
String anameString = new String();
Connection con = (Connection) DriverManager.getConnection(url, user3, password);// 创建连接对象,来连接数据库
PreparedStatement ps0 = con.prepareStatement(word3);// 创建执行对象
resultSet = ps0.executeQuery(word3);
while (resultSet.next()) {
if (resultSet.getString("Id").equals(id)) {
user.setSex(resultSet.getString("sex"));
user.setName(resultSet.getString("name"));
user.setId(resultSet.getString("Id"));
user.setActivityID(resultSet.getString("activityID"));
user.setManage(resultSet.getString("isManage"));
}
}
return user;
}
/**
* 选择shift tab 整体往前捻 重置事件
*/
protected void rest() {
nametf.setText("");
passwordField.setText("");
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
2.创建普通用户接口类
public class MainFrm extends JFrame {
private JPanel contentPane;
private JDesktopPane desktopPane;
public static User user;
/**
* Launch the application.[[
*/
/**
* Create the frame.
*/
MainFrm() {
super();
}
public MainFrm(User user1) throws SQLException {
this.user = user1;
applyhave();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 750, 580);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("\u7F16\u8F91");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("退出");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// setDefaultCloseOperation(3); //直接关闭程序
System.exit(0);
}
});
menu.add(menuItem);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.WHITE);
contentPane.add(desktopPane, BorderLayout.CENTER);
JLabel label = new JLabel("用户界面");
label.setFont(new Font("宋体", Font.BOLD, 24));
label.setBounds(268, 103, 200, 118);
desktopPane.add(label);
JButton button = new JButton("查看社团信息");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Show show = new Show(user);
show.setVisible(true);
}
});
button.setBounds(15, 311, 178, 76);
desktopPane.add(button);
JButton button_1 = new JButton("创建社团");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean s = false;
CreatAct act = new CreatAct();
act.setVisible(true);
}
});
button_1.setBounds(243, 311, 178, 76);
desktopPane.add(button_1);
JButton button_2 = new JButton("进入管理员界面");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (user.getManage().equals("1")) {
Manager manager;
try {
manager = new Manager(user);
manager.setVisible(true);
dispose();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "您没有权限");
}
}
});
button_2.setBounds(483, 311, 178, 76);
desktopPane.add(button_2);
this.setLocationRelativeTo(null);
}
private void applyhave() throws SQLException {
String url = "jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=utf8";// url是固定的
String user3 = "root";// 这里一般是root
String password = "root";// 这是登陆数据库的密码,我的密码为空
String word3 = "select * from applyhave";
ResultSet resultSet = null;
String anameString = new String();
Connection con = (Connection) DriverManager.getConnection(url, user3, password);// 创建连接对象,来连接数据库
PreparedStatement ps0 = con.prepareStatement(word3);// 创建执行对象
resultSet = ps0.executeQuery(word3);
while (resultSet.next()) {
if (resultSet.getString("Stuname").equals(user.getName())) {
if (resultSet.getString("result").equals("1")) {
JOptionPane.showMessageDialog(null, "您申请加入的社团申请成功");
String word5 = "DELETE FROM applyhave WHERE Stuname = '" + user.getName() + "'";
PreparedStatement ps4 = con.prepareStatement(word5);// 创建执行对象
ps4.executeUpdate();
ps4.close();
}
}
}
}
}
}
3.创建查看社团信息类JFrame
public class Show extends JFrame {
private JTable table;
private User user;
public Show(User user) {
this.user=user;
setFocusable(true);
setBounds(100, 100, 1002, 697);
JScrollPane scrollPane = new JScrollPane();
JButton btnNewButton = new JButton("申请加入社团");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ApplyAct applyAct =new ApplyAct(user);
applyAct.setVisible(true);
}
});
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(180)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 612, GroupLayout.PREFERRED_SIZE)
.addContainerGap(188, Short.MAX_VALUE))
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addContainerGap(771, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addGap(86))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(125)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 348, GroupLayout.PREFERRED_SIZE)
.addGap(57)
.addComponent(btnNewButton)
.addContainerGap(82, Short.MAX_VALUE))
);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\u793E\u56E2\u540D", "\u793E\u56E2\u4FE1\u606F", "\u793E\u56E2ID", "\u56E2\u957F", "\u662F\u5426\u6EE1\u5458"
}
));
scrollPane.setViewportView(table);
getContentPane().setLayout(groupLayout);
loadData();//传入数据
this.setLocationRelativeTo(null);
}
// 将数据库中数据传入中可以发挥
private void loadData() {
// 获取表格数据
DefaultTableModel model = (DefaultTableModel) table.getModel();
// 每次添加的时候清空表格的原始数据
model.setRowCount(0);
// 模拟数据
// model.addRow("社团名称,社团人数,团长,是否满员");
model.setRowCount(0);
String url="jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=utf8";//url是固定的 jdbc:mysql://localhost:3036/数据库名
String user3="root";//这里一般是root
String password="root";//这是登陆数据库的密码,我的密码为空
String word="select * from activity";
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动类
Connection con= DriverManager.getConnection(url, user3, password);//创建连接对象,来连接数据库
ResultSet resultSet =null;
PreparedStatement ps=con.prepareStatement(word);//创建执行对象
resultSet=ps.executeQuery();
while (resultSet.next()) {
Object[] objs1 = {resultSet.getString("ActInfo"),resultSet.getString("ActId"),resultSet.getString("ManagerId"),resultSet.getString("Adtname"),"否"};
model.addRow(objs1);
}
ps.close();
con.close();
} catch (ClassNotFoundException e2) { //这里是错误捕捉,如果出错,计算机就会执行这段代码,下面的也是
e2.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
dispose();
}
}
4.创建其他功能JFrame与上述方法同,因代码过于多,不一一展示