当前位置:   article > 正文

Java课程设计——学生寝室管理系统的设计与实现_学生宿舍管理系统课程设计

学生宿舍管理系统课程设计

1.基本思路

首先用JFrame类创建初界面、登录界面、教师用户注册界面、学生用户注册界面、教师功能界面、学生功能界面、管理员功能界面。

然后各界面添加各自的按钮,并对按钮进行事件监听,通过类方法或jdbc连接数据库实现相应的功能。

再用JFrame类装JTable,JTable装数据库中的数据。

最后通过更改背景颜色、添加图片或图标丰富界面内容。

2.课程设计报告中可能需要的素材

2.01功能框架图

2.02 系统E-R图

2.03数据库里创建的的用户表

2.04 数据库里创建的的学生宿舍信息表

2.05添加功能流程图

2.06修改功能流程图

2.07删除功能流程图

2.08注册流程图

2.09系统流程图

2.10管理员功能界面流程图

 3.数据库脚本

学生宿舍信息表以学籍号作为主码

用户信息表以用户名作为主码

  1. //创建学生宿舍数据库
  2. create database stu-dor;
  3. //进入学生宿舍数据库
  4. use stu_dor;
  5. //创建学生宿舍信息表(学籍号作为主码)
  6. create table Dinfo(name char(20) not null,
  7. -> sno char(9) check(sno>200000000 and sno<203000000)primary key,
  8. -> sex char(2) check(sex in('男','女'))not null,
  9. -> age smallint,
  10. -> bno char(4) check(bno in('左一','左二','右一','右二')),
  11. -> Dno int check(Dno>99 and Dno<700)not null,
  12. -> Sapb smallint check(Sapb>00 and Sapb<31)not null
  13. -> );
  14. //创建用户信息表(用户名作为主码)
  15. create table user(username char(200)primary key,password char(200)not null);
  16. //插入管理员用户名和密码数据
  17. insert into user values('Admin','123456');
  18. //插入学生宿舍信息数据
  19. insert into Dinfo values('李勇','202105122','男',19,'左一',100,01);
  20. insert into Dinfo values('江城','202105123','男',19,'左二',100,01);
  21. insert into Dinfo values('王弘文','202105124','男',19,'右一',100,01);
  22. insert into Dinfo values('刘烨伟','202105125','男',19,'右二',100,01);
  23. insert into Dinfo values('沈希文','202105126','男',19,'左一',101,01);
  24. insert into Dinfo values('宁鸿恩','202105127','男',19,'左二',101,01);
  25. insert into Dinfo values('孙玄华','202105128','男',19,'右一',101,01);
  26. insert into Dinfo values('秦哲瀚','202105129','男',19,'右二',101,01);
  27. insert into Dinfo values('王梦琪','202105130','女',21,'左一',401,18);
  28. insert into Dinfo values('梨香巧','202105131','女',21,'左二',401,18);
  29. insert into Dinfo values('刘代云','202105132','女',20,'右一',401,18);
  30. insert into Dinfo values('李海莲','202105133','女',21,'右二',401,18);
  31. insert into Dinfo values('周千萍','202105134','女',20,'左一',402,18);
  32. insert into Dinfo values('曹芷巧','202105135','女',20,'左二',402,18);
  33. insert into Dinfo values('钱冬云','202105136','女',19,'右一',402,18);
  34. insert into Dinfo values('沈笑白','202105137','女',22,'右二',402,18);

 4.详细代码及图片展示

4.1初始界面

  1. //注意把包名更改为自己创建的
  2. package an;
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JTable;
  13. //身份选择界面窗口
  14. public class Cs extends JFrame{
  15. public Cs() {
  16. //创建组件
  17. setTitle("学生寝室管理系统初界面");
  18. JButton btn1=new JButton("注册教师用户");
  19. JButton btn2=new JButton("注册学生用户");
  20. JButton btn3=new JButton("用户登录");
  21. JLabel jbl1=new JLabel("请选择您的操作");
  22. ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\教师.png");
  23. ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\学生.png");
  24. ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\用户登录.png");
  25. JLabel jbl2=new JLabel(im1);
  26. JLabel jbl3=new JLabel(im2);
  27. JLabel jbl4=new JLabel(im3);
  28. //注册教师用户按钮事件监听
  29. btn1.addActionListener(new ActionListener() {
  30. @Override
  31. public void actionPerformed(ActionEvent e) {
  32. New_t t1=new New_t();
  33. setEnabled(false); //设置初界面窗口不可操作
  34. setVisible(false); //设置初界面窗口不可见
  35. t1.addWindowListener(new WindowAdapter() { //监听教师注册窗口是否关闭
  36. @Override
  37. public void windowClosed(WindowEvent e) {
  38. setEnabled(true);
  39. setVisible(true);
  40. }
  41. });
  42. }
  43. });
  44. //注册学生用户按钮事件监听
  45. btn2.addActionListener(new ActionListener() {
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. New_s s1=new New_s();
  49. setEnabled(false);
  50. setVisible(false);
  51. s1.addWindowListener(new WindowAdapter() { //监听学生注册窗口是否关闭
  52. @Override
  53. public void windowClosed(WindowEvent e) {
  54. setEnabled(true);
  55. setVisible(true);
  56. }
  57. });
  58. }
  59. });
  60. //用户登录按钮事件监听
  61. btn3.addActionListener(new ActionListener() {
  62. @Override
  63. public void actionPerformed(ActionEvent e) {
  64. Dl d1=new Dl();
  65. setEnabled(false);
  66. setVisible(false);
  67. d1.addWindowListener(new WindowAdapter() {
  68. @Override
  69. public void windowClosed(WindowEvent e) { //监听登录窗口是否关闭
  70. setEnabled(true);
  71. setVisible(true);
  72. }
  73. });
  74. }
  75. });
  76. jbl2.setBounds(40, 40, 150, 130);
  77. btn1.setBounds(40, 180, 150, 25);
  78. jbl3.setBounds(300, 40, 150, 130);
  79. btn2.setBounds(300, 180, 150, 25);
  80. jbl4.setBounds(195, 200, 100, 80);
  81. btn3.setBounds(195, 260, 100, 25);
  82. jbl1.setBounds(200, 10, 150, 100);
  83. //设置窗口属性
  84. setSize(500,400);
  85. setLocation(800, 400);
  86. setResizable(false);
  87. getContentPane().setBackground(Color.ORANGE);
  88. setLayout(null);
  89. //设置按钮字体颜色
  90. btn2.setForeground(Color.BLUE);
  91. btn3.setForeground(Color.green);
  92. btn3.setBackground(Color.blue);
  93. //设置字体线框消失
  94. btn1.setFocusPainted(false);
  95. btn2.setFocusPainted(false);
  96. btn3.setFocusPainted(false);
  97. getContentPane().add(btn1);
  98. getContentPane().add(btn2);
  99. getContentPane().add(btn3);
  100. getContentPane().add(jbl1);
  101. getContentPane().add(jbl2);
  102. getContentPane().add(jbl3);
  103. getContentPane().add(jbl4);
  104. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105. setVisible(true);
  106. }
  107. }

4.2注册界面

教师用户的注册码为123456

 4.2.1教师用户注册界面详细代码

  1. //注意把包名更改为自己创建的
  2. package an;
  3. import java.awt.BorderLayout;
  4. import java.awt.Label;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JDialog;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21. public class New_t extends JFrame{
  22. public New_t() {
  23. setSize(500, 400);
  24. setLocation(600, 300);
  25. setTitle("教师用户注册界面");
  26. setLayout(null);
  27. setResizable(false);
  28. JLabel lb1=new JLabel("新用户名");
  29. JLabel lb2=new JLabel(" 新密码");
  30. JLabel lb4=new JLabel("确认密码");
  31. JLabel lb3=new JLabel("教师注册码");
  32. JLabel lb5=new JLabel("请用'T'或't'开头");
  33. JTextField user = new JTextField(200);
  34. JPasswordField pass = new JPasswordField(200);
  35. JPasswordField repass = new JPasswordField(200);
  36. JTextField yzm = new JTextField(8);
  37. JButton btn1=new JButton("注册");
  38. JButton btn2=new JButton("清空");
  39. ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户名.png");
  40. ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\密码.png");
  41. ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\确认密码.png");
  42. ImageIcon im4=new ImageIcon("C:\\Users\\Desktopy\\images\\注册码.png");
  43. JLabel bl1=new JLabel(im1);
  44. JLabel bl2=new JLabel(im2);
  45. JLabel bl3=new JLabel(im3);
  46. JLabel bl4=new JLabel(im4);
  47. //清空按钮事件监听
  48. btn2.addActionListener(new ActionListener() {
  49. @Override
  50. public void actionPerformed(ActionEvent e) {
  51. user.setText("");
  52. pass.setText("");
  53. yzm.setText("");
  54. repass.setText("");
  55. }
  56. });
  57. //注册按钮事件监听
  58. btn1.addActionListener(new ActionListener() {
  59. @Override
  60. public void actionPerformed(ActionEvent e) {
  61. String username=user.getText().trim(); //获取用户名
  62. String zcm=yzm.getText().trim(); //获取注册码
  63. String password=pass.getText().trim(); //获取密码
  64. String repassword=repass.getText().trim(); //获取确认密码
  65. if( yzm == null|| !("123456".equals(zcm)))
  66. {
  67. JOptionPane.showMessageDialog(null, "错误:注册码为空或不匹配");
  68. yzm.setText("");
  69. }
  70. else if(user == null|| "".equals(username))
  71. {
  72. JOptionPane.showMessageDialog(null, "错误:新用户名不能为空");
  73. }
  74. else if(username.charAt(0)!='t'&&username.charAt(0)!='T')
  75. {
  76. JOptionPane.showMessageDialog(null, "新用户名不符合要求");
  77. user.setText("");
  78. }
  79. else if(pass == null|| "".equals(password))
  80. {
  81. JOptionPane.showMessageDialog(null, "错误:新密码不能为空");
  82. }
  83. else if(repass == null|| "".equals(repassword))
  84. {
  85. JOptionPane.showMessageDialog(null, "错误:确认密码不能为空");
  86. }
  87. else if( !(password.equals(repassword))) //检测密码与确认密码是否一致
  88. {
  89. JOptionPane.showMessageDialog(null, "错误:两次密码不一致");
  90. pass.setText("");
  91. repass.setText("");
  92. }
  93. else { //用户名和密码以及确认密码和注册码都输入正确才可进入
  94. PreparedStatement psts=null;
  95. PreparedStatement psti=null;
  96. ResultSet rss=null;
  97. Connection con=null;
  98. try {
  99. //连接数据库,进入user表
  100. Class.forName("com.mysql.cj.jdbc.Driver");
  101. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  102. //查找用户名是否存在
  103. String sqlselect="select * from user where username='"+username+"'";
  104. psts=con.prepareStatement(sqlselect);
  105. rss=psts.executeQuery(sqlselect);
  106. if(rss.next()) {
  107. JOptionPane.showMessageDialog(null, "用户名已存在,请重新注册");
  108. return;
  109. }
  110. else {
  111. psti=con.prepareStatement("insert into user values(?,?)"); //SQL中占位符会自动加上单引号
  112. psti.setString(1, username);
  113. psti.setString(2, password);
  114. int rsi=psti.executeUpdate(); //括号内可不写语句
  115. if(rsi<0) {
  116. JOptionPane.showMessageDialog(null, "用户注册失败");
  117. }
  118. else {
  119. JDialog zct=new JDialog();
  120. zct.setSize(350, 150);
  121. zct.setLocation(800,380);
  122. zct.setTitle("提示");
  123. zct.getContentPane().add(new Label(" 教师用户注册成功"),BorderLayout.CENTER);
  124. zct.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  125. zct.setVisible(true);
  126. zct.addWindowListener(new WindowAdapter() {
  127. @Override
  128. public void windowClosed(WindowEvent e) {
  129. dispose(); //关闭教师注册窗口
  130. }
  131. });
  132. }
  133. }
  134. }catch(Exception e1){
  135. JOptionPane.showMessageDialog(null, "系统异常");
  136. }finally {
  137. Way.release(rss);
  138. Way.release(psts);
  139. Way.release(psti);
  140. Way.release(con);
  141. }
  142. }
  143. }
  144. });
  145. lb3.setBounds(100, 60, 80, 25);
  146. bl4.setBounds(170, 60, 60, 25);
  147. yzm.setBounds(210, 60, 80, 25);
  148. lb1.setBounds(70, 100, 60, 25);
  149. bl1.setBounds(110, 100, 60, 25);
  150. user.setBounds(150, 100, 200, 25);
  151. lb5.setBounds(350, 100, 100, 25);
  152. lb2.setBounds(70, 140, 60, 25);
  153. bl2.setBounds(110, 140, 60, 25);
  154. pass.setBounds(150, 140, 200, 25);
  155. lb4.setBounds(70, 175, 60, 25);
  156. bl3.setBounds(110, 175, 60, 25);
  157. repass.setBounds(150, 175, 200, 25);
  158. btn1.setBounds(150, 220, 70, 25);
  159. btn2.setBounds(250, 220, 70, 25);
  160. btn1.setFocusPainted(false);
  161. btn2.setFocusPainted(false);
  162. getContentPane().add(lb3);
  163. getContentPane().add(bl4);
  164. getContentPane().add(yzm);
  165. getContentPane().add(lb1);
  166. getContentPane().add(bl1);
  167. getContentPane().add(user);
  168. getContentPane().add(lb5);
  169. getContentPane().add(lb2);
  170. getContentPane().add(bl2);
  171. getContentPane().add(pass);
  172. getContentPane().add(lb4);
  173. getContentPane().add(bl3);
  174. getContentPane().add(repass);
  175. getContentPane().add(btn1);
  176. getContentPane().add(btn2);
  177. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  178. setVisible(true);
  179. }
  180. }

4.2.2学生用户注册界面详细代码

  1. ​​//注意将包名修改为自己创建的
  2. package an;
  3. import java.awt.BorderLayout;
  4. import java.awt.Label;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JDialog;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21. public class New_s extends JFrame{
  22. public New_s() {
  23. setSize(500, 400);
  24. setLocation(1000, 300);
  25. setTitle("学生用户注册界面");
  26. setLayout(null);
  27. setResizable(false);
  28. JLabel lb1=new JLabel("新用户名");
  29. JLabel lb2=new JLabel(" 新密码");
  30. JLabel lb4=new JLabel("确认密码");
  31. JLabel lb3=new JLabel("请用'S'或's'开头");
  32. JTextField user = new JTextField(200);
  33. JPasswordField pass = new JPasswordField(200);
  34. JPasswordField repass = new JPasswordField(200);
  35. JButton btn1=new JButton("注册");
  36. JButton btn2=new JButton("清空");
  37. ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户名.png");
  38. ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\密码.png");
  39. ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\确认密码.png");
  40. JLabel bl1=new JLabel(im1);
  41. JLabel bl2=new JLabel(im2);
  42. JLabel bl3=new JLabel(im3);
  43. btn2.addActionListener(new ActionListener() {
  44. @Override
  45. public void actionPerformed(ActionEvent e) {
  46. user.setText("");
  47. pass.setText("");
  48. repass.setText("");
  49. }
  50. });
  51. btn1.addActionListener(new ActionListener() {
  52. @Override
  53. public void actionPerformed(ActionEvent e) {
  54. String username=user.getText().trim(); //获取用户名
  55. String password=pass.getText().trim(); //获取密码
  56. String repassword=repass.getText().trim(); //获取确认密码
  57. if(user == null|| "".equals(username))
  58. {
  59. JOptionPane.showMessageDialog(null, "错误:新用户名不能为空");
  60. }
  61. else if(username.charAt(0)!='s'&&username.charAt(0)!='S') //用逻辑连接词'并',用'或'会有逻辑错误
  62. {
  63. JOptionPane.showMessageDialog(null, "新用户名不符合要求");
  64. user.setText("");
  65. }
  66. else if(pass == null|| "".equals(password))
  67. {
  68. JOptionPane.showMessageDialog(null, "错误:新密码不能为空");
  69. }
  70. else if(repass == null|| "".equals(repassword))
  71. {
  72. JOptionPane.showMessageDialog(null, "错误:确认密码不能为空");
  73. }
  74. else if( !(password.equals(repassword))) //检测密码与确认密码是否一致
  75. {
  76. JOptionPane.showMessageDialog(null, "错误:两次密码不一致");
  77. pass.setText("");
  78. repass.setText("");
  79. }
  80. else { //用户名和密码以及确认密码都输入正确才可进入
  81. PreparedStatement psts=null;
  82. PreparedStatement psti=null;
  83. ResultSet rss=null;
  84. Connection con=null;
  85. try {
  86. //连接数据库,进入user表
  87. Class.forName("com.mysql.cj.jdbc.Driver");
  88. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  89. //查找用户名是否存在
  90. String sqlselect="select * from user where username='"+username+"'";
  91. psts=con.prepareStatement(sqlselect);
  92. rss=psts.executeQuery(sqlselect);
  93. if(rss.next()) {
  94. JOptionPane.showMessageDialog(null, "用户名已存在,请重新注册");
  95. return;
  96. }
  97. else {
  98. psti=con.prepareStatement("insert into user values(?,?)"); //SQL中占位符会自动加上单引号
  99. psti.setString(1, username);
  100. psti.setString(2, password);
  101. int rsi=psti.executeUpdate(); //括号内可不写语句
  102. if(rsi<0) {
  103. JOptionPane.showMessageDialog(null, "用户注册失败");
  104. }
  105. else {
  106. JDialog zct=new JDialog();
  107. zct.setSize(350, 150);
  108. zct.setLocation(800,380);
  109. zct.setTitle("提示");
  110. zct.getContentPane().add(new Label(" 学生用户注册成功"),BorderLayout.CENTER);
  111. zct.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  112. zct.setVisible(true);
  113. zct.addWindowListener(new WindowAdapter() {
  114. @Override
  115. public void windowClosed(WindowEvent e) {
  116. dispose(); //关闭学生注册窗口
  117. }
  118. });
  119. }
  120. }
  121. }catch(Exception e1){
  122. JOptionPane.showMessageDialog(null, "系统异常");
  123. }finally {
  124. Way.release(rss);
  125. Way.release(psts);
  126. Way.release(psti);
  127. Way.release(con);
  128. }
  129. }
  130. }
  131. });
  132. lb1.setBounds(70, 100, 60, 25);
  133. bl1.setBounds(110, 100, 60, 25);
  134. user.setBounds(150, 100, 200, 25);
  135. lb3.setBounds(350, 100, 100, 25);
  136. lb2.setBounds(70, 140, 60, 25);
  137. bl2.setBounds(110, 140, 60, 25);
  138. pass.setBounds(150, 140, 200, 25);
  139. lb4.setBounds(70, 175, 60, 25);
  140. bl3.setBounds(110, 175, 60, 25);
  141. repass.setBounds(150, 175, 200, 25);
  142. btn1.setBounds(150, 220, 70, 25);
  143. btn2.setBounds(250, 220, 70, 25);
  144. btn1.setFocusPainted(false);
  145. btn2.setFocusPainted(false);
  146. getContentPane().add(lb1);
  147. getContentPane().add(bl1);
  148. getContentPane().add(user);
  149. getContentPane().add(lb3);
  150. getContentPane().add(lb2);
  151. getContentPane().add(bl2);
  152. getContentPane().add(pass);
  153. getContentPane().add(lb4);
  154. getContentPane().add(bl3);
  155. getContentPane().add(repass);
  156. getContentPane().add(btn1);
  157. getContentPane().add(btn2);
  158. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  159. setVisible(true);
  160. }
  161. }

4.3用户登录界面

  1. //注意将包名修改为自己创建的
  2. package an;
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPasswordField;
  18. import javax.swing.JTextField;
  19. //登录界面窗口
  20. public class Dl extends JFrame{
  21. public Dl(){
  22. //设置窗口属性
  23. setSize(500, 400);
  24. setLocation(700, 300);
  25. getContentPane().setBackground(Color.gray); //设置背景颜色
  26. setTitle("学生寝室管理系统用户登录界面");
  27. setLayout(null); //设置空布局
  28. setResizable(false);
  29. //创建标签,文本框,按钮
  30. JLabel lb1=new JLabel("用户名");
  31. JLabel lb2=new JLabel(" 密码");
  32. JTextField user = new JTextField(200);
  33. JPasswordField pass = new JPasswordField(200);
  34. JButton btn1=new JButton("登录");
  35. JButton btn2=new JButton("清空");
  36. ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户名.png");
  37. ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\密码.png");
  38. JLabel lb3=new JLabel(im1);
  39. JLabel lb4=new JLabel(im2);
  40. //清空按钮事件监听
  41. btn2.addActionListener(new ActionListener() {
  42. @Override
  43. public void actionPerformed(ActionEvent e) {
  44. user.setText(""); //设置文本框内为空字符(清空)
  45. pass.setText("");
  46. }
  47. });
  48. //登录按钮事件监听
  49. btn1.addActionListener(new ActionListener() {
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. String username=user.getText().trim(); //获取用户名 //.trim()方法可以排除空格
  53. String password=pass.getText().trim(); //获取密码
  54. if(user == null|| "".equals(username))
  55. {
  56. JOptionPane.showMessageDialog(null, "错误:用户名不能为空");
  57. }
  58. else if(pass == null|| "".equals(password))
  59. {
  60. JOptionPane.showMessageDialog(null, "错误:密码不能为空");
  61. }
  62. else { //用户名和密码都输入正确才可进入
  63. PreparedStatement psts=null;
  64. ResultSet rss=null;
  65. Connection con=null;
  66. try {
  67. //连接数据库,进入user表
  68. Class.forName("com.mysql.cj.jdbc.Driver");
  69. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  70. //查找用户名是否存在
  71. String sqlselect="select * from user where username=? and password=?";
  72. psts=con.prepareStatement(sqlselect);
  73. psts.setString(1, username);
  74. psts.setString(2, password);
  75. rss=psts.executeQuery();
  76. if(rss.next()) {
  77. //检测用户名首字母是否为's'或'S'
  78. if(username.charAt(0)=='s'||username.charAt(0)=='S')
  79. {
  80. Stuwindow stu=new Stuwindow();
  81. setVisible(false);
  82. stu.addWindowListener(new WindowAdapter() {
  83. public void windowClosed(WindowEvent e) {
  84. dispose();
  85. }
  86. });
  87. }
  88. //检测用户名首字母是否为't'或'T'
  89. else if(username.charAt(0)=='t'||username.charAt(0)=='T')
  90. {
  91. Tcwindow tc=new Tcwindow();
  92. setVisible(false);
  93. tc.addWindowListener(new WindowAdapter() {
  94. public void windowClosed(WindowEvent e) {
  95. dispose();
  96. }
  97. });
  98. }
  99. else if("Admin".equals(username))
  100. {
  101. Mawindow ma=new Mawindow();
  102. setVisible(false);
  103. ma.addWindowListener(new WindowAdapter() {
  104. public void windowClosed(WindowEvent e) {
  105. dispose();
  106. }
  107. });
  108. }
  109. else {JOptionPane.showMessageDialog(null, "非法用户,无法登陆");}
  110. }
  111. else {
  112. JOptionPane.showMessageDialog(null, "用户名或密码错误");
  113. }
  114. }catch(Exception e1){
  115. JOptionPane.showMessageDialog(null, "系统异常");
  116. }finally {
  117. Way.release(rss);
  118. Way.release(psts);
  119. Way.release(con);
  120. }
  121. }
  122. }
  123. });
  124. //设置标签,文本框,按钮位置
  125. lb1.setBounds(80, 100, 60, 25);
  126. lb2.setBounds(80, 175, 60, 25);
  127. lb3.setBounds(110, 100, 60, 25);
  128. lb4.setBounds(110, 175, 60, 25);
  129. user.setBounds(150, 100, 200, 25);
  130. pass.setBounds(150, 175, 200, 25);
  131. btn1.setBounds(150, 220, 70, 25);
  132. btn2.setBounds(250, 220, 70, 25);
  133. btn1.setFocusPainted(false);
  134. btn2.setFocusPainted(false);
  135. //添加组件到面板上
  136. getContentPane().add(lb3);
  137. getContentPane().add(lb1);
  138. getContentPane().add(user);
  139. getContentPane().add(lb4);
  140. getContentPane().add(lb2);
  141. getContentPane().add(pass);
  142. getContentPane().add(btn1);
  143. getContentPane().add(btn2);
  144. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //单独关闭此窗口(不会关闭所有窗口)
  145. setVisible(true);
  146. }
  147. }

4.4用户功能界面

学生用户与教师用户调用mysql中的表的内容。建立一个Vector<String>类型的变量attr<列名>与Vector<vector>类型的变量d,再建立一个JTable表格通过jdbc方法输出ResultSet通过循环将它的数据依次加入d变量中。在JTable表中加入attr与d。

数据无法全部显示的时候我将JTable表放入Scrollpane中,通过滚动面板显示所有数据。

4.4.1学生用户功能界面

学生功能界面窗口我使用的是东西南北中布局,东西南北分别用四个空标签填充,剩下的中部就用一个按钮铺满,详细代码如下:

  1. //注意将包名修改为自己创建的
  2. package an;
  3. import java.awt.BorderLayout;
  4. import java.awt.Button;
  5. import java.awt.Color;
  6. import java.awt.Label;
  7. import java.awt.ScrollPane;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.util.Vector;
  15. import javax.swing.JFrame;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JTable;
  18. public class Stuwindow extends JFrame{
  19. public Stuwindow() {
  20. Button btn1=new Button("查看功能");
  21. Label l1=new Label();
  22. Label l2=new Label();
  23. Label l3=new Label();
  24. Label l4=new Label();
  25. l1.setBackground(Color.CYAN);
  26. l2.setBackground(Color.CYAN);
  27. l3.setBackground(Color.CYAN);
  28. l4.setBackground(Color.CYAN);
  29. //查看功能事件监听
  30. btn1.addActionListener(new ActionListener() {
  31. @Override
  32. public void actionPerformed(ActionEvent e) {
  33. PreparedStatement pst=null;
  34. ResultSet rs=null;
  35. Connection con=null;
  36. JFrame jf=new JFrame("学生寝室信息");
  37. ScrollPane sp1=new ScrollPane();
  38. jf.setLocation(500, 200);
  39. jf.setSize(1000, 800);
  40. jf.setLayout(new BorderLayout());
  41. try {
  42. Class.forName("com.mysql.cj.jdbc.Driver");
  43. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  44. String sql="select * from dinfo";
  45. pst=con.prepareStatement(sql);
  46. rs=pst.executeQuery();
  47. Vector<String> attr= new Vector<>(); //定义列名
  48. attr.add("姓名");
  49. attr.add("学籍号");
  50. attr.add("性别");
  51. attr.add("年龄");
  52. attr.add("床位号");
  53. attr.add("寝室门号");
  54. attr.add("学生公寓楼栋");
  55. Vector<Vector<Object>> data=new Vector<Vector<Object>>();
  56. while(rs.next()) {
  57. Vector<Object> d=new Vector<Object>();
  58. d.add(rs.getString("name"));
  59. d.add(rs.getString("sno"));
  60. d.add(rs.getString("sex"));
  61. d.add(rs.getInt("age"));
  62. d.add(rs.getString("bno"));
  63. d.add(rs.getInt("Dno"));
  64. d.add(rs.getInt("Sapb"));
  65. data.add(d);
  66. }
  67. JTable jt=new JTable(data, attr);
  68. jt.setEnabled(false);
  69. jt.setRowHeight(50);
  70. sp1.add(jt);
  71. jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  72. jf.getContentPane().add(jt.getTableHeader(),BorderLayout.NORTH);
  73. jf.getContentPane().add(sp1,BorderLayout.CENTER);
  74. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  75. jf.setVisible(true);
  76. }catch(Exception e1){
  77. JOptionPane.showMessageDialog(null, "系统异常");
  78. }finally {
  79. Way.release(rs);
  80. Way.release(pst);
  81. Way.release(con);
  82. }
  83. }
  84. });
  85. setSize(500, 400);
  86. setLocation(800, 600);
  87. setLayout(new BorderLayout());
  88. setTitle("学生功能界面");
  89. setResizable(false);
  90. getContentPane().add(l1,BorderLayout.EAST);
  91. getContentPane().add(l2,BorderLayout.WEST);
  92. getContentPane().add(l3,BorderLayout.NORTH);
  93. getContentPane().add(l4,BorderLayout.SOUTH);
  94. getContentPane().add(btn1,BorderLayout.CENTER);
  95. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  96. setVisible(true);
  97. }
  98. }

4.4.2教师用户功能界面

 教师功能界面窗口的设计:我沿用了学生功能界面窗口的布局,中部加了个面板p1替代大按钮,p1使用的是网格布局,然后又添加了四个标签分别用来装四张图片,四个标签和四个功能按钮放在p1上调整位置使其呈现上图的效果。详细代码如下:

  1. //注意将包名修改为自己创建的
  2. package an;
  3. import java.awt.BorderLayout;
  4. import java.awt.Button;
  5. import java.awt.Color;
  6. import java.awt.GridLayout;
  7. import java.awt.Label;
  8. import java.awt.Panel;
  9. import java.awt.ScrollPane;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.util.Vector;
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JTable;
  22. //教师功能界面窗口
  23. public class Tcwindow extends JFrame{
  24. public Tcwindow() {
  25. //创建组件
  26. ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\查看.png");
  27. ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\添加.png");
  28. ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\修改.png");
  29. ImageIcon im4=new ImageIcon("C:\\Users\\Desktop\\images\\删除.png");
  30. JLabel lb1=new JLabel(im1);
  31. JLabel lb2=new JLabel(im2);
  32. JLabel lb3=new JLabel(im3);
  33. JLabel lb4=new JLabel(im4);
  34. Button btn1=new Button("查看功能");
  35. Button btn2=new Button("添加功能");
  36. Button btn3=new Button("修改功能");
  37. Button btn4=new Button("删除功能");
  38. Panel p1=new Panel(new GridLayout(4,2)); //面板p1用网格布局装下四个按钮
  39. Label l1=new Label(); //用四个空标签填补东西南北四个区域
  40. Label l2=new Label();
  41. Label l3=new Label();
  42. Label l4=new Label();
  43. //设置标签背景颜色
  44. l1.setBackground(Color.CYAN);
  45. l2.setBackground(Color.CYAN);
  46. l3.setBackground(Color.CYAN);
  47. l4.setBackground(Color.CYAN);
  48. //查看功能事件监听
  49. btn1.addActionListener(new ActionListener() {
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. PreparedStatement pst=null;
  53. ResultSet rs=null;
  54. Connection con=null;
  55. JFrame jf=new JFrame("学生寝室信息");
  56. ScrollPane sp1=new ScrollPane();
  57. jf.setLocation(500, 200);
  58. jf.setSize(1000, 800);
  59. jf.setLayout(new BorderLayout());
  60. try {
  61. Class.forName("com.mysql.cj.jdbc.Driver");
  62. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  63. String sql="select * from dinfo";
  64. pst=con.prepareStatement(sql);
  65. rs=pst.executeQuery();
  66. Vector<String> attr= new Vector<>(); //定义列名
  67. attr.add("姓名");
  68. attr.add("学籍号");
  69. attr.add("性别");
  70. attr.add("年龄");
  71. attr.add("床位号");
  72. attr.add("寝室门号");
  73. attr.add("学生公寓楼栋");
  74. Vector<Vector<Object>> data=new Vector<Vector<Object>>();
  75. while(rs.next()) {
  76. Vector<Object> d=new Vector<Object>();
  77. d.add(rs.getString("name"));
  78. d.add(rs.getString("sno"));
  79. d.add(rs.getString("sex"));
  80. d.add(rs.getInt("age"));
  81. d.add(rs.getString("bno"));
  82. d.add(rs.getInt("Dno"));
  83. d.add(rs.getInt("Sapb"));
  84. data.add(d);
  85. }
  86. JTable jt=new JTable(data, attr);
  87. jt.setEnabled(false);
  88. jt.setRowHeight(50);
  89. sp1.add(jt);
  90. jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  91. jf.getContentPane().add(jt.getTableHeader(),BorderLayout.NORTH);
  92. jf.getContentPane().add(sp1,BorderLayout.CENTER);
  93. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  94. jf.setVisible(true);
  95. }catch(Exception e1){
  96. JOptionPane.showMessageDialog(null, "系统异常");
  97. }finally {
  98. Way.release(rs);
  99. Way.release(pst);
  100. Way.release(con);
  101. }
  102. }
  103. });
  104. //添加功能事件监听
  105. btn2.addActionListener(new ActionListener() {
  106. @Override
  107. public void actionPerformed(ActionEvent e) {
  108. function fc=new function();
  109. fc.setTitle("添加功能");
  110. fc.bo.setText("添加");
  111. fc.bo.addActionListener(new ActionListener() {
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. String gna=fc.tf1.getText().trim(); //获取姓名
  115. String gsn=fc.tf2.getText().trim(); //获取学籍号
  116. String gse=fc.tf3.getText().trim(); //获取性别
  117. String gag=fc.tf4.getText().trim(); //获取年龄
  118. String gbn=fc.tf5.getText().trim(); //获取床位号
  119. String gdo=fc.tf6.getText().trim(); //获取寝室门号
  120. String gsap=fc.tf7.getText().trim(); //获取学生公寓楼栋
  121. if("".equals(gna)||"".equals(gsn)||"".equals(gse)||"".equals(gag)||"".equals(gbn)||"".equals(gdo)||"".equals(gsap)) {
  122. JOptionPane.showMessageDialog(null, "错误:不可以为空");
  123. return;
  124. }
  125. else {
  126. Integer gae=Integer.parseInt(gag);
  127. Integer gdn=Integer.parseInt(gdo);
  128. Integer gsab=Integer.parseInt(gsap);
  129. PreparedStatement psts=null;
  130. PreparedStatement psti=null;
  131. ResultSet rss=null;
  132. Connection con=null;
  133. try {
  134. //连接数据库
  135. Class.forName("com.mysql.cj.jdbc.Driver");
  136. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  137. //查找学籍号是否存在
  138. String sqlselect="select sno from dinfo where sno=?";
  139. psts=con.prepareStatement(sqlselect);
  140. psts.setString(1, gsn);
  141. rss=psts.executeQuery();
  142. if(rss.next()) {
  143. JOptionPane.showMessageDialog(null, "该学籍号已存在,请重新添加");
  144. return;
  145. }
  146. else {
  147. String sqlinsert="insert into dinfo values(?,?,?,?,?,?,?)";
  148. psti=con.prepareStatement(sqlinsert);
  149. psti.setString(1, gna);
  150. psti.setString(2, gsn);
  151. psti.setString(3, gse);
  152. psti.setInt(4, gae);
  153. psti.setString(5, gbn);
  154. psti.setInt(6, gdn);
  155. psti.setInt(7, gsab);
  156. int rsi=psti.executeUpdate();
  157. if(rsi<0) {
  158. JOptionPane.showMessageDialog(null, "信息添加失败");
  159. }
  160. else {
  161. JOptionPane.showMessageDialog(null, "信息添加成功");
  162. }
  163. }
  164. }catch(Exception e1){
  165. JOptionPane.showMessageDialog(null, "错误:添加的数据不合法");
  166. }finally {
  167. Way.release(rss);
  168. Way.release(psts);
  169. Way.release(psti);
  170. Way.release(con);
  171. }
  172. }
  173. }
  174. });
  175. }
  176. });
  177. //修改功能事件监听
  178. btn3.addActionListener(new ActionListener() {
  179. @Override
  180. public void actionPerformed(ActionEvent e) {
  181. smwin sw=new smwin();
  182. sw.setTitle("修改学生信息");
  183. sw.bt.setText("查找");
  184. sw.bt.addActionListener(new ActionListener() {
  185. @Override
  186. public void actionPerformed(ActionEvent e) {
  187. String snop=sw.jt.getText().trim();
  188. PreparedStatement psts=null;
  189. ResultSet rss=null;
  190. Connection con=null;
  191. try {
  192. Class.forName("com.mysql.cj.jdbc.Driver");
  193. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  194. //查找学籍号是否存在
  195. String sqlselect="select * from dinfo where sno=?";
  196. psts=con.prepareStatement(sqlselect);
  197. psts.setString(1, snop);
  198. rss=psts.executeQuery();
  199. if(rss.next()) {
  200. String name=rss.getString("name");
  201. String sno=rss.getString("sno");
  202. String sex=rss.getString("sex");
  203. String age=rss.getString("age");
  204. String bno=rss.getString("bno");
  205. String Dno=rss.getString("Dno");
  206. String Sapb=rss.getString("Sapb");
  207. function fct=new function();
  208. fct.setTitle("修改功能");
  209. fct.bo.setText("修改");
  210. fct.tf1.setText(""+name+"");
  211. fct.tf2.setText(""+sno+"");
  212. fct.tf3.setText(""+sex+"");
  213. fct.tf4.setText(""+age+"");
  214. fct.tf5.setText(""+bno+"");
  215. fct.tf6.setText(""+Dno+"");
  216. fct.tf7.setText(""+Sapb+"");
  217. fct.bo.addActionListener(new ActionListener() {
  218. @Override
  219. public void actionPerformed(ActionEvent e) {
  220. String gna=fct.tf1.getText().trim(); //获取姓名
  221. String gsn=fct.tf2.getText().trim(); //获取学籍号
  222. String gse=fct.tf3.getText().trim(); //获取性别
  223. String gag=fct.tf4.getText().trim(); //获取年龄
  224. String gbn=fct.tf5.getText().trim(); //获取床位号
  225. String gdo=fct.tf6.getText().trim(); //获取寝室门号
  226. String gsap=fct.tf7.getText().trim(); //获取学生公寓楼栋
  227. if("".equals(gna)||"".equals(gsn)||"".equals(gse)||"".equals(gag)||"".equals(gbn)||"".equals(gdo)||"".equals(gsap)) {
  228. JOptionPane.showMessageDialog(null, "错误:不可以为空");
  229. return;
  230. }
  231. else {
  232. Integer gae=Integer.parseInt(gag);
  233. Integer gdn=Integer.parseInt(gdo);
  234. Integer gsab=Integer.parseInt(gsap);
  235. PreparedStatement pstu=null;
  236. Connection conp=null;
  237. try{
  238. Class.forName("com.mysql.cj.jdbc.Driver");
  239. conp=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  240. String sqlupdate="update dinfo set name=?,sno=?,sex=?,age=?,bno=?,Dno=?,Sapb=? where sno=?";
  241. pstu=conp.prepareStatement(sqlupdate);
  242. pstu.setString(1, gna);
  243. pstu.setString(2, gsn);
  244. pstu.setString(3, gse);
  245. pstu.setInt(4, gae);
  246. pstu.setString(5, gbn);
  247. pstu.setInt(6, gdn);
  248. pstu.setInt(7, gsab);
  249. pstu.setString(8, snop);
  250. int rsu=pstu.executeUpdate();
  251. if(rsu<0) {
  252. JOptionPane.showMessageDialog(null, "修改失败");
  253. }else
  254. {
  255. JOptionPane.showMessageDialog(null, "修改成功");
  256. fct.dispose();
  257. }
  258. }catch(Exception e1) {
  259. JOptionPane.showMessageDialog(null, "错误:修改的数据不合法");
  260. }finally {
  261. Way.release(pstu);
  262. Way.release(conp);
  263. }
  264. }
  265. }
  266. });
  267. }
  268. else {JOptionPane.showMessageDialog(null, "该学籍号未找到");
  269. sw.jt.setText("");
  270. }
  271. }catch(Exception e1){
  272. JOptionPane.showMessageDialog(null, "系统异常");
  273. }finally {
  274. Way.release(rss);
  275. Way.release(psts);
  276. Way.release(con);
  277. }
  278. }
  279. });
  280. }
  281. });
  282. //删除功能事件监听
  283. btn4.addActionListener(new ActionListener() {
  284. @Override
  285. public void actionPerformed(ActionEvent e) {
  286. smwin sw=new smwin();
  287. sw.setTitle("删除学生信息");
  288. sw.bt.setText("删除");
  289. sw.bt.addActionListener(new ActionListener() {
  290. @Override
  291. public void actionPerformed(ActionEvent e) {
  292. String sno=sw.jt.getText().trim();
  293. PreparedStatement psts=null;
  294. PreparedStatement pstd=null;
  295. ResultSet rss=null;
  296. Connection con=null;
  297. try {
  298. //连接数据库
  299. Class.forName("com.mysql.cj.jdbc.Driver");
  300. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  301. //查找学籍号是否存在
  302. String sqlselect="select * from dinfo where sno=?";
  303. psts=con.prepareStatement(sqlselect);
  304. psts.setString(1, sno);
  305. rss=psts.executeQuery();
  306. if(rss.next()) {
  307. int p=JOptionPane.showConfirmDialog(null, "确认删除该学生信息吗?", "提示",
  308. JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  309. if(p==JOptionPane.YES_OPTION) {
  310. String sqlinsert="delete from dinfo where sno=?";
  311. pstd=con.prepareStatement(sqlinsert);
  312. pstd.setString(1, sno);
  313. int rsd=pstd.executeUpdate();
  314. if(rsd<0) {
  315. JOptionPane.showMessageDialog(null, "删除数据失败");
  316. }
  317. else {
  318. JOptionPane.showMessageDialog(null, "删除数据成功");
  319. }
  320. }
  321. }
  322. else {
  323. JOptionPane.showMessageDialog(null, "错误:该学籍号未找到");
  324. return;
  325. }
  326. }catch(Exception e1){
  327. JOptionPane.showMessageDialog(null, "系统异常");
  328. }finally {
  329. Way.release(rss);
  330. Way.release(psts);
  331. Way.release(pstd);
  332. Way.release(con);
  333. }
  334. }
  335. });
  336. }
  337. });
  338. //设置窗口属性
  339. setSize(500, 400);
  340. setLocation(600, 500);
  341. setTitle("教师功能界面");
  342. setLayout(new BorderLayout()); //中部区域为东西南北占用后剩下的区域
  343. setResizable(false);
  344. getContentPane().add(l1,BorderLayout.NORTH);
  345. getContentPane().add(l2,BorderLayout.SOUTH);
  346. getContentPane().add(l3,BorderLayout.EAST);
  347. getContentPane().add(l4,BorderLayout.WEST);
  348. p1.add(btn1);
  349. p1.add(btn2);
  350. p1.add(lb1);
  351. p1.add(lb2);
  352. p1.add(btn3);
  353. p1.add(btn4);
  354. p1.add(lb3);
  355. p1.add(lb4);
  356. getContentPane().add(p1,BorderLayout.CENTER);
  357. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  358. setVisible(true);
  359. }
  360. }
修改和删除功能演示

由于我当时做课设的时候没有学懂直接在表格里面修改删除,所以我用了比较复杂的方法来实现修改和删除功能。

学生宿舍信息的修改和删除功能都是通过学籍号搜索来找到对应的数据,删除功能找到学籍号后就会直接删除,不会显示删除的数据。修改窗口沿用了添加窗口,只是多了个数据回显。

这里我展示的是修改用户信息的相应窗口,和搜索学籍号的窗口是差不多的,我就不再多展示了。

 

 添加功能演示

4.4.3管理员功能界面

 管理员不仅可以对学生宿舍的信息进行操作,还能对用户的账号和密码进行操作。详细代码如下:

  1. //注意将包名修改为自己创建的
  2. package an;
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. public class Mawindow extends JFrame{
  13. public Mawindow() {
  14. setTitle("管理员功能界面");
  15. setSize(500, 550);
  16. setLocation(650, 300);
  17. setLayout(null);
  18. this.getContentPane().setBackground(Color.DARK_GRAY);
  19. ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户管理.png");
  20. ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\寝室管理.png");
  21. JLabel lb1=new JLabel(im1);
  22. JLabel lb2=new JLabel(im2);
  23. JButton usm=new JButton("用户信息管理");
  24. JButton dom=new JButton("寝室信息管理");
  25. lb1.setBounds(70, 80, 150, 130);
  26. lb2.setBounds(270, 80, 150, 130);
  27. usm.setFocusPainted(false);
  28. dom.setFocusPainted(false);
  29. usm.setBounds(80, 250, 120, 120);
  30. dom.setBounds(280, 250, 120, 120);
  31. //用户信息管理事件监听
  32. usm.addActionListener(new ActionListener() {
  33. @Override
  34. public void actionPerformed(ActionEvent e) {
  35. Usermanage u1=new Usermanage();
  36. setVisible(false);
  37. u1.addWindowListener(new WindowAdapter() {
  38. @Override
  39. public void windowClosed(WindowEvent e) {
  40. setVisible(true);
  41. }
  42. });
  43. }
  44. });
  45. //寝室信息管理事件监听
  46. dom.addActionListener(new ActionListener() {
  47. @Override
  48. public void actionPerformed(ActionEvent e) {
  49. Tcwindow t1=new Tcwindow();
  50. t1.setTitle("寝室信息管理功能界面");
  51. setVisible(false);
  52. t1.addWindowListener(new WindowAdapter() {
  53. @Override
  54. public void windowClosed(WindowEvent e) {
  55. setVisible(true);
  56. }
  57. });
  58. }
  59. });
  60. getContentPane().add(usm);
  61. getContentPane().add(dom);
  62. getContentPane().add(lb1);
  63. getContentPane().add(lb2);
  64. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  65. setVisible(true);
  66. }
  67. }

由于学生宿舍信息操作和教师功能界面大同小异,故我就只展示用户信息的操作。

 详细代码如下:

  1. //注意将包名修改为自己创建的
  2. package an;
  3. import java.awt.BorderLayout;
  4. import java.awt.ScrollPane;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.util.Vector;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JTable;
  19. import javax.swing.plaf.BorderUIResource;
  20. public class Usermanage extends JFrame{
  21. public Usermanage() {
  22. PreparedStatement psts=null;
  23. ResultSet rs=null;
  24. Connection con=null;
  25. setTitle("用户信息");
  26. JButton ref=new JButton("刷新");
  27. JButton upd=new JButton("修改");
  28. JButton apd=new JButton("添加");
  29. JButton del=new JButton("删除");
  30. ScrollPane scp=new ScrollPane();
  31. JPanel jp=new JPanel();
  32. setLocation(500, 200);
  33. setSize(1000, 800);
  34. setLayout(new BorderLayout());
  35. ref.setFocusPainted(false);
  36. upd.setFocusPainted(false);
  37. apd.setFocusPainted(false);
  38. del.setFocusPainted(false);
  39. //刷新按钮事件监听
  40. ref.addActionListener(new ActionListener() {
  41. @Override
  42. public void actionPerformed(ActionEvent e) {
  43. setVisible(false);;
  44. Usermanage m1=new Usermanage();
  45. m1.addWindowListener(new WindowAdapter() {
  46. @Override
  47. public void windowClosed(WindowEvent e) {
  48. dispose();
  49. }
  50. });
  51. }
  52. });
  53. //修改按钮事件监听
  54. upd.addActionListener(new ActionListener() {
  55. @Override
  56. public void actionPerformed(ActionEvent e) {
  57. usersrch usch =new usersrch();
  58. usch.setTitle("修改用户信息");
  59. usch.bt.setText("搜索");
  60. usch.bt.addActionListener(new ActionListener() {
  61. @Override
  62. public void actionPerformed(ActionEvent e) {
  63. String susname=usch.jt.getText().trim();
  64. PreparedStatement psts=null;
  65. ResultSet rss=null;
  66. Connection con=null;
  67. try {
  68. Class.forName("com.mysql.cj.jdbc.Driver");
  69. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  70. //查找用户名是否存在
  71. String sqlselect="select * from user where username=?";
  72. psts=con.prepareStatement(sqlselect);
  73. psts.setString(1, susname);
  74. rss=psts.executeQuery();
  75. if(rss.next()) {
  76. String usname=rss.getString("username");
  77. String psword=rss.getString("password");
  78. admwin adw=new admwin();
  79. adw.setTitle("修改功能");
  80. adw.bo.setText("修改");
  81. adw.tf1.setText(""+usname+"");
  82. adw.tf2.setText(""+psword+"");
  83. adw.bo.addActionListener(new ActionListener() {
  84. @Override
  85. public void actionPerformed(ActionEvent e) {
  86. String usn=adw.tf1.getText().trim(); //获取用户名
  87. String psw=adw.tf2.getText().trim(); //获取密码
  88. if("".equals(usn)||"".equals(psw)) {
  89. JOptionPane.showMessageDialog(null, "错误:不可以为空");
  90. return;
  91. }
  92. else {
  93. PreparedStatement pstu=null;
  94. Connection conp=null;
  95. try{
  96. Class.forName("com.mysql.cj.jdbc.Driver");
  97. conp=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  98. String sqlupdate="update user set username=?,password=? where username=?";
  99. pstu=conp.prepareStatement(sqlupdate);
  100. pstu.setString(1, usn);
  101. pstu.setString(2, psw);
  102. pstu.setString(3, susname);
  103. int rsu=pstu.executeUpdate();
  104. if(rsu<0) {
  105. JOptionPane.showMessageDialog(null, "修改失败");
  106. }else
  107. {
  108. JOptionPane.showMessageDialog(null, "修改成功");
  109. adw.dispose();
  110. usch.dispose();
  111. }
  112. }catch(Exception e1) {
  113. JOptionPane.showMessageDialog(null, "错误:修改的用户名已存在");
  114. }finally {
  115. Way.release(pstu);
  116. Way.release(conp);
  117. }
  118. }
  119. }
  120. });
  121. }
  122. else {JOptionPane.showMessageDialog(null, "该用户名未找到");
  123. usch.jt.setText("");
  124. }
  125. }catch(Exception e1){
  126. JOptionPane.showMessageDialog(null, "系统异常");
  127. }finally {
  128. Way.release(rss);
  129. Way.release(psts);
  130. Way.release(con);
  131. }
  132. }
  133. });
  134. }
  135. });
  136. //添加按钮事件监听
  137. apd.addActionListener(new ActionListener() {
  138. @Override
  139. public void actionPerformed(ActionEvent e) {
  140. admwin adw=new admwin();
  141. adw.setTitle("添加功能");
  142. adw.bo.setText("添加");
  143. adw.bo.addActionListener(new ActionListener() {
  144. @Override
  145. public void actionPerformed(ActionEvent e) {
  146. String usn=adw.tf1.getText().trim(); //获取用户名
  147. String psw=adw.tf2.getText().trim(); //获取密码
  148. if("".equals(usn)||"".equals(psw)) {
  149. JOptionPane.showMessageDialog(null, "错误:不可以为空");
  150. return;
  151. }
  152. else {
  153. PreparedStatement psts=null;
  154. PreparedStatement psti=null;
  155. ResultSet rss=null;
  156. Connection con=null;
  157. try {
  158. //连接数据库
  159. Class.forName("com.mysql.cj.jdbc.Driver");
  160. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  161. //查找用户名是否存在
  162. String sqlselect="select username from user where username=?";
  163. psts=con.prepareStatement(sqlselect);
  164. psts.setString(1, usn);
  165. rss=psts.executeQuery();
  166. if(rss.next()) {
  167. JOptionPane.showMessageDialog(null, "该用户名已存在,请重新添加");
  168. return;
  169. }
  170. else {
  171. String sqlinsert="insert into user values(?,?)";
  172. psti=con.prepareStatement(sqlinsert);
  173. psti.setString(1, usn);
  174. psti.setString(2, psw);
  175. int rsi=psti.executeUpdate();
  176. if(rsi<0) {
  177. JOptionPane.showMessageDialog(null, "用户信息添加失败");
  178. }
  179. else {
  180. JOptionPane.showMessageDialog(null, "用户信息添加成功");
  181. adw.dispose();
  182. }
  183. }
  184. }catch(Exception e1){
  185. JOptionPane.showMessageDialog(null, "错误:添加的数据不合法");
  186. }finally {
  187. Way.release(rss);
  188. Way.release(psts);
  189. Way.release(psti);
  190. Way.release(con);
  191. }
  192. }
  193. }
  194. });
  195. }
  196. });
  197. //删除按钮事件监听
  198. del.addActionListener(new ActionListener() {
  199. @Override
  200. public void actionPerformed(ActionEvent e) {
  201. usersrch usch=new usersrch();
  202. usch.setTitle("删除用户信息");
  203. usch.bt.setText("删除");
  204. usch.bt.addActionListener(new ActionListener() {
  205. @Override
  206. public void actionPerformed(ActionEvent e) {
  207. String susname=usch.jt.getText().trim();
  208. PreparedStatement psts=null;
  209. PreparedStatement pstd=null;
  210. ResultSet rss=null;
  211. Connection con=null;
  212. try {
  213. //连接数据库
  214. Class.forName("com.mysql.cj.jdbc.Driver");
  215. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  216. //查找用户名是否存在
  217. String sqlselect="select * from user where username=?";
  218. psts=con.prepareStatement(sqlselect);
  219. psts.setString(1, susname);
  220. rss=psts.executeQuery();
  221. if(rss.next()) {
  222. int p=JOptionPane.showConfirmDialog(null, "确认删除该用户信息吗?", "提示",
  223. JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  224. if(p==JOptionPane.YES_OPTION) {
  225. String sqlinsert="delete from user where username=?";
  226. pstd=con.prepareStatement(sqlinsert);
  227. pstd.setString(1, susname);
  228. int rsd=pstd.executeUpdate();
  229. if(rsd<0) {
  230. JOptionPane.showMessageDialog(null, "删除数据失败");
  231. }
  232. else {
  233. JOptionPane.showMessageDialog(null, "删除数据成功");
  234. usch.dispose();
  235. }
  236. }
  237. }
  238. else {
  239. JOptionPane.showMessageDialog(null, "错误:该用户名未找到");
  240. return;
  241. }
  242. }catch(Exception e1){
  243. JOptionPane.showMessageDialog(null, "系统异常");
  244. }finally {
  245. Way.release(rss);
  246. Way.release(psts);
  247. Way.release(pstd);
  248. Way.release(con);
  249. }
  250. }
  251. });
  252. }
  253. });
  254. try {
  255. Class.forName("com.mysql.cj.jdbc.Driver");
  256. con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
  257. String sql="select * from user";
  258. psts=con.prepareStatement(sql);
  259. rs=psts.executeQuery();
  260. Vector<String> attr= new Vector<>(); //定义列名
  261. attr.add("用户名");
  262. attr.add("密码");
  263. Vector<Vector<Object>> data=new Vector<Vector<Object>>();
  264. while(rs.next()) {
  265. Vector<Object> d=new Vector<Object>();
  266. d.add(rs.getString("username"));
  267. d.add(rs.getString("password"));
  268. data.add(d);
  269. }
  270. JTable jt=new JTable(data, attr);
  271. jt.setEnabled(false);
  272. jt.setRowHeight(50);
  273. scp.add(jt);
  274. jp.add(ref);
  275. jp.add(upd);
  276. jp.add(apd);
  277. jp.add(del);
  278. jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  279. getContentPane().add(jt.getTableHeader(),BorderLayout.NORTH);
  280. getContentPane().add(jp,BorderLayout.SOUTH);
  281. getContentPane().add(scp,BorderLayout.CENTER);
  282. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  283. setVisible(true);
  284. }catch(Exception e1){
  285. JOptionPane.showMessageDialog(null, "系统异常");
  286. }finally {
  287. Way.release(rs);
  288. Way.release(psts);
  289. Way.release(con);
  290. }
  291. }
  292. }

4.5方法类

此类包括连接数据库以及释放数据库资源的方法,还有搜索窗口等。

数据库连接的时候要改为自己电脑上的数据库用户名和密码!!!

详细代码如下:

  1. //注意将包名修改为自己创建的
  2. package an;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JTextField;
  11. /**
  12. * @author iujth
  13. *
  14. */
  15. //主类
  16. public class Way{
  17. //释放数据库资源方法
  18. public static void release(Object stream) {
  19. if (stream != null) {
  20. try {
  21. if (stream instanceof Connection) {
  22. ((Connection) stream).close();
  23. }
  24. if (stream instanceof PreparedStatement) {
  25. ((PreparedStatement) stream).close();
  26. }
  27. if (stream instanceof ResultSet) {
  28. ((ResultSet) stream).close();
  29. }
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. stream=null;
  34. }
  35. }
  36. //定义常量连接数据库路径及用户名和密码(注意改为自己的数据库用户名和密码!!!)
  37. public static final String url="jdbc:mysql://localhost:3306/stu_dor"+"?serverTimezone=GMT%2B8&useSSL=false";
  38. public static final String us="root";
  39. public static final String ps="001224";
  40. }
  41. //教师功能方法
  42. //修改和删除功能的查找学籍号界面
  43. class smwin extends JFrame{
  44. @Override
  45. public void setTitle(String title) {
  46. // TODO Auto-generated method stub
  47. super.setTitle(title);
  48. }
  49. JLabel jb=new JLabel("请输入学籍号:");
  50. JTextField jt=new JTextField(9);
  51. JButton bt=new JButton();
  52. smwin(){
  53. setSize(320, 150);
  54. setLocation(800, 500);
  55. setResizable(false);
  56. setLayout(null);
  57. jb.setBounds(20, 50, 100, 20);
  58. jt.setBounds(110, 50, 100, 20);
  59. bt.setBounds(220, 50, 60, 20);
  60. bt.setFocusPainted(false);
  61. getContentPane().add(jb);
  62. getContentPane().add(jt);
  63. getContentPane().add(bt);
  64. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  65. setVisible(true);
  66. }
  67. }
  68. //寝室信息添加和修改功能的编辑界面
  69. class function extends JFrame{
  70. @Override
  71. public void setTitle(String title) {
  72. // TODO Auto-generated method stub
  73. super.setTitle(title);
  74. }
  75. JLabel lb1=new JLabel("姓名");
  76. JLabel lb2=new JLabel("学籍号");
  77. JLabel lb3=new JLabel("性别");
  78. JLabel lb4=new JLabel("年龄");
  79. JLabel lb5=new JLabel("床位号");
  80. JLabel lb6=new JLabel("寝室门号");
  81. JLabel lb7=new JLabel("学生寝室楼栋");
  82. JLabel tip1=new JLabel("请填写9位数");
  83. JLabel tip2=new JLabel("请填写'左一'或'左二'或'右一'或'右二'");
  84. JLabel tip3=new JLabel("请填写100-699内的数");
  85. JLabel tip4=new JLabel("请填写1-30内的数");
  86. JTextField tf1=new JTextField(20);
  87. JTextField tf2=new JTextField(20);
  88. JTextField tf3=new JTextField(20);
  89. JTextField tf4=new JTextField(20);
  90. JTextField tf5=new JTextField(20);
  91. JTextField tf6=new JTextField(20);
  92. JTextField tf7=new JTextField(20);
  93. JButton bo=new JButton();
  94. function(){
  95. setSize(800, 500);
  96. setLocation(650, 500);
  97. setResizable(false);
  98. setLayout(null);
  99. lb1.setBounds(20, 30, 50, 20);
  100. tf1.setBounds(80, 30, 120, 20);
  101. lb2.setBounds(15, 80, 80, 20);
  102. tf2.setBounds(80, 80, 120, 20);
  103. tip1.setBounds(210, 80, 90, 20);
  104. lb3.setBounds(20, 130, 80, 20);
  105. tf3.setBounds(80, 130, 20, 20);
  106. lb4.setBounds(20, 180, 40, 20);
  107. tf4.setBounds(80, 180, 40, 20);
  108. lb5.setBounds(300, 50, 50, 20);
  109. tf5.setBounds(370, 50, 120, 20);
  110. tip2.setBounds(500, 50, 220, 20);
  111. lb6.setBounds(300, 100, 100, 20);
  112. tf6.setBounds(370, 100, 120, 20);
  113. tip3.setBounds(500, 100, 150, 20);
  114. lb7.setBounds(280, 150, 120, 20);
  115. tf7.setBounds(370, 150, 120, 20);
  116. tip4.setBounds(500, 150, 120, 20);
  117. bo.setFocusPainted(false);
  118. bo.setBounds(320, 300, 80, 20);
  119. getContentPane().add(lb1);
  120. getContentPane().add(tf1);
  121. getContentPane().add(lb2);
  122. getContentPane().add(tf2);
  123. getContentPane().add(tip1);
  124. getContentPane().add(lb3);
  125. getContentPane().add(tf3);
  126. getContentPane().add(tip3);
  127. getContentPane().add(lb4);
  128. getContentPane().add(tf4);
  129. getContentPane().add(tip4);
  130. getContentPane().add(lb5);
  131. getContentPane().add(tf5);
  132. getContentPane().add(tip2);
  133. getContentPane().add(lb6);
  134. getContentPane().add(tf6);
  135. getContentPane().add(lb7);
  136. getContentPane().add(tf7);
  137. getContentPane().add(bo);
  138. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  139. setVisible(true);
  140. }
  141. }
  142. //管理员功能方法
  143. //用户信息添加和修改功能的编辑界面
  144. class admwin extends JFrame{
  145. @Override
  146. public void setTitle(String title) {
  147. // TODO Auto-generated method stub
  148. super.setTitle(title);
  149. }
  150. JLabel lb1=new JLabel("用户名");
  151. JLabel lb2=new JLabel("密码");
  152. JTextField tf1=new JTextField(200);
  153. JTextField tf2=new JTextField(200);
  154. JButton bo=new JButton();
  155. public admwin() {
  156. setSize(500, 400);
  157. setLocation(700, 300);
  158. setLayout(null);
  159. setResizable(false);
  160. lb1.setBounds(100, 100, 60, 25);
  161. lb2.setBounds(100, 175, 60, 25);
  162. tf1.setBounds(150, 100, 200, 25);
  163. tf2.setBounds(150, 175, 200, 25);
  164. bo.setBounds(200, 220, 70, 25);
  165. bo.setFocusPainted(false);
  166. getContentPane().add(lb1);
  167. getContentPane().add(tf1);
  168. getContentPane().add(lb2);
  169. getContentPane().add(tf2);
  170. getContentPane().add(bo);
  171. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  172. setVisible(true);
  173. }
  174. }
  175. //修改和删除功能的查找用户名界面
  176. class usersrch extends JFrame{
  177. @Override
  178. public void setTitle(String title) {
  179. // TODO Auto-generated method stub
  180. super.setTitle(title);
  181. }
  182. JLabel jb=new JLabel("请输入用户名:");
  183. JTextField jt=new JTextField(9);
  184. JButton bt=new JButton();
  185. usersrch(){
  186. setSize(320, 150);
  187. setLocation(800, 500);
  188. setResizable(false);
  189. setLayout(null);
  190. jb.setBounds(20, 50, 100, 20);
  191. jt.setBounds(110, 50, 100, 20);
  192. bt.setBounds(220, 50, 60, 20);
  193. bt.setFocusPainted(false);
  194. getContentPane().add(jb);
  195. getContentPane().add(jt);
  196. getContentPane().add(bt);
  197. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  198. setVisible(true);
  199. }
  200. }

4.6开始类

运行初始界面的的类:

  1. //注意将包名修改为自己创建的
  2. package an;
  3. public class Start {
  4. public static void main(String[] args) {
  5. new Cs();
  6. }
  7. }

5.数据库连接和图片插入

本人使用的是MySQL8.0.31版本,eclipse是2018-09(4.9.0)版本。

下载jar包和具体连接教程请参考:Eclipse连接MySQL数据库(补充8.0)_Lime-W的博客-CSDN博客

图片素材库可以参考此网站:iconfont-阿里巴巴矢量图标库

放图片的时候请记得改对路径。

6.结尾

由于该课程设计是本人在大二上学期期末敲了12天才完成的,故代码有些冗余和繁多。这篇文章是大概过了半年才写的,所以对相应的细节也有些记不得了,如果本人有讲错的地方欢迎指正。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/740392
推荐阅读
相关标签
  

闽ICP备14008679号