赞
踩
首先用JFrame类创建初界面、登录界面、教师用户注册界面、学生用户注册界面、教师功能界面、学生功能界面、管理员功能界面。
然后各界面添加各自的按钮,并对按钮进行事件监听,通过类方法或jdbc连接数据库实现相应的功能。
再用JFrame类装JTable,JTable装数据库中的数据。
最后通过更改背景颜色、添加图片或图标丰富界面内容。
学生宿舍信息表以学籍号作为主码
用户信息表以用户名作为主码
- //创建学生宿舍数据库
- create database stu-dor;
-
- //进入学生宿舍数据库
- use stu_dor;
-
- //创建学生宿舍信息表(学籍号作为主码)
- create table Dinfo(name char(20) not null,
- -> sno char(9) check(sno>200000000 and sno<203000000)primary key,
- -> sex char(2) check(sex in('男','女'))not null,
- -> age smallint,
- -> bno char(4) check(bno in('左一','左二','右一','右二')),
- -> Dno int check(Dno>99 and Dno<700)not null,
- -> Sapb smallint check(Sapb>00 and Sapb<31)not null
- -> );
-
- //创建用户信息表(用户名作为主码)
- create table user(username char(200)primary key,password char(200)not null);
-
- //插入管理员用户名和密码数据
- insert into user values('Admin','123456');
-
- //插入学生宿舍信息数据
- insert into Dinfo values('李勇','202105122','男',19,'左一',100,01);
- insert into Dinfo values('江城','202105123','男',19,'左二',100,01);
- insert into Dinfo values('王弘文','202105124','男',19,'右一',100,01);
- insert into Dinfo values('刘烨伟','202105125','男',19,'右二',100,01);
- insert into Dinfo values('沈希文','202105126','男',19,'左一',101,01);
- insert into Dinfo values('宁鸿恩','202105127','男',19,'左二',101,01);
- insert into Dinfo values('孙玄华','202105128','男',19,'右一',101,01);
- insert into Dinfo values('秦哲瀚','202105129','男',19,'右二',101,01);
-
- insert into Dinfo values('王梦琪','202105130','女',21,'左一',401,18);
- insert into Dinfo values('梨香巧','202105131','女',21,'左二',401,18);
- insert into Dinfo values('刘代云','202105132','女',20,'右一',401,18);
- insert into Dinfo values('李海莲','202105133','女',21,'右二',401,18);
- insert into Dinfo values('周千萍','202105134','女',20,'左一',402,18);
- insert into Dinfo values('曹芷巧','202105135','女',20,'左二',402,18);
- insert into Dinfo values('钱冬云','202105136','女',19,'右一',402,18);
- insert into Dinfo values('沈笑白','202105137','女',22,'右二',402,18);
- //注意把包名更改为自己创建的
- package an;
-
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTable;
- //身份选择界面窗口
- public class Cs extends JFrame{
- public Cs() {
- //创建组件
- setTitle("学生寝室管理系统初界面");
- JButton btn1=new JButton("注册教师用户");
- JButton btn2=new JButton("注册学生用户");
- JButton btn3=new JButton("用户登录");
- JLabel jbl1=new JLabel("请选择您的操作");
-
- ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\教师.png");
- ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\学生.png");
- ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\用户登录.png");
- JLabel jbl2=new JLabel(im1);
- JLabel jbl3=new JLabel(im2);
- JLabel jbl4=new JLabel(im3);
- //注册教师用户按钮事件监听
- btn1.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- New_t t1=new New_t();
- setEnabled(false); //设置初界面窗口不可操作
- setVisible(false); //设置初界面窗口不可见
- t1.addWindowListener(new WindowAdapter() { //监听教师注册窗口是否关闭
- @Override
- public void windowClosed(WindowEvent e) {
- setEnabled(true);
- setVisible(true);
- }
- });
- }
- });
- //注册学生用户按钮事件监听
- btn2.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- New_s s1=new New_s();
- setEnabled(false);
- setVisible(false);
- s1.addWindowListener(new WindowAdapter() { //监听学生注册窗口是否关闭
- @Override
- public void windowClosed(WindowEvent e) {
- setEnabled(true);
- setVisible(true);
- }
- });
- }
- });
- //用户登录按钮事件监听
- btn3.addActionListener(new ActionListener() {
-
-
- @Override
- public void actionPerformed(ActionEvent e) {
- Dl d1=new Dl();
- setEnabled(false);
- setVisible(false);
- d1.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosed(WindowEvent e) { //监听登录窗口是否关闭
- setEnabled(true);
- setVisible(true);
- }
- });
- }
- });
-
-
- jbl2.setBounds(40, 40, 150, 130);
- btn1.setBounds(40, 180, 150, 25);
- jbl3.setBounds(300, 40, 150, 130);
- btn2.setBounds(300, 180, 150, 25);
- jbl4.setBounds(195, 200, 100, 80);
- btn3.setBounds(195, 260, 100, 25);
- jbl1.setBounds(200, 10, 150, 100);
- //设置窗口属性
- setSize(500,400);
- setLocation(800, 400);
- setResizable(false);
- getContentPane().setBackground(Color.ORANGE);
- setLayout(null);
- //设置按钮字体颜色
- btn2.setForeground(Color.BLUE);
- btn3.setForeground(Color.green);
- btn3.setBackground(Color.blue);
- //设置字体线框消失
- btn1.setFocusPainted(false);
- btn2.setFocusPainted(false);
- btn3.setFocusPainted(false);
-
- getContentPane().add(btn1);
- getContentPane().add(btn2);
- getContentPane().add(btn3);
- getContentPane().add(jbl1);
- getContentPane().add(jbl2);
- getContentPane().add(jbl3);
- getContentPane().add(jbl4);
-
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
-
- }
- }
教师用户的注册码为123456
- //注意把包名更改为自己创建的
- package an;
-
- import java.awt.BorderLayout;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
-
- public class New_t extends JFrame{
- public New_t() {
-
- setSize(500, 400);
- setLocation(600, 300);
- setTitle("教师用户注册界面");
- setLayout(null);
- setResizable(false);
-
- JLabel lb1=new JLabel("新用户名");
- JLabel lb2=new JLabel(" 新密码");
- JLabel lb4=new JLabel("确认密码");
- JLabel lb3=new JLabel("教师注册码");
- JLabel lb5=new JLabel("请用'T'或't'开头");
- JTextField user = new JTextField(200);
- JPasswordField pass = new JPasswordField(200);
- JPasswordField repass = new JPasswordField(200);
- JTextField yzm = new JTextField(8);
- JButton btn1=new JButton("注册");
- JButton btn2=new JButton("清空");
-
- ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户名.png");
- ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\密码.png");
- ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\确认密码.png");
- ImageIcon im4=new ImageIcon("C:\\Users\\Desktopy\\images\\注册码.png");
- JLabel bl1=new JLabel(im1);
- JLabel bl2=new JLabel(im2);
- JLabel bl3=new JLabel(im3);
- JLabel bl4=new JLabel(im4);
-
- //清空按钮事件监听
- btn2.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- user.setText("");
- pass.setText("");
- yzm.setText("");
- repass.setText("");
-
- }
- });
- //注册按钮事件监听
- btn1.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String username=user.getText().trim(); //获取用户名
- String zcm=yzm.getText().trim(); //获取注册码
- String password=pass.getText().trim(); //获取密码
- String repassword=repass.getText().trim(); //获取确认密码
-
- if( yzm == null|| !("123456".equals(zcm)))
- {
- JOptionPane.showMessageDialog(null, "错误:注册码为空或不匹配");
- yzm.setText("");
- }
- else if(user == null|| "".equals(username))
- {
- JOptionPane.showMessageDialog(null, "错误:新用户名不能为空");
- }
- else if(username.charAt(0)!='t'&&username.charAt(0)!='T')
- {
- JOptionPane.showMessageDialog(null, "新用户名不符合要求");
- user.setText("");
- }
- else if(pass == null|| "".equals(password))
- {
- JOptionPane.showMessageDialog(null, "错误:新密码不能为空");
- }
- else if(repass == null|| "".equals(repassword))
- {
- JOptionPane.showMessageDialog(null, "错误:确认密码不能为空");
- }
- else if( !(password.equals(repassword))) //检测密码与确认密码是否一致
- {
- JOptionPane.showMessageDialog(null, "错误:两次密码不一致");
- pass.setText("");
- repass.setText("");
- }
- else { //用户名和密码以及确认密码和注册码都输入正确才可进入
- PreparedStatement psts=null;
- PreparedStatement psti=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库,进入user表
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找用户名是否存在
- String sqlselect="select * from user where username='"+username+"'";
- psts=con.prepareStatement(sqlselect);
- rss=psts.executeQuery(sqlselect);
- if(rss.next()) {
- JOptionPane.showMessageDialog(null, "用户名已存在,请重新注册");
- return;
- }
- else {
- psti=con.prepareStatement("insert into user values(?,?)"); //SQL中占位符会自动加上单引号
- psti.setString(1, username);
- psti.setString(2, password);
- int rsi=psti.executeUpdate(); //括号内可不写语句
- if(rsi<0) {
- JOptionPane.showMessageDialog(null, "用户注册失败");
- }
- else {
- JDialog zct=new JDialog();
- zct.setSize(350, 150);
- zct.setLocation(800,380);
- zct.setTitle("提示");
- zct.getContentPane().add(new Label(" 教师用户注册成功"),BorderLayout.CENTER);
- zct.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- zct.setVisible(true);
- zct.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosed(WindowEvent e) {
- dispose(); //关闭教师注册窗口
- }
- });
- }
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(psti);
- Way.release(con);
- }
- }
- }
- });
-
-
- lb3.setBounds(100, 60, 80, 25);
- bl4.setBounds(170, 60, 60, 25);
- yzm.setBounds(210, 60, 80, 25);
- lb1.setBounds(70, 100, 60, 25);
- bl1.setBounds(110, 100, 60, 25);
- user.setBounds(150, 100, 200, 25);
- lb5.setBounds(350, 100, 100, 25);
- lb2.setBounds(70, 140, 60, 25);
- bl2.setBounds(110, 140, 60, 25);
- pass.setBounds(150, 140, 200, 25);
- lb4.setBounds(70, 175, 60, 25);
- bl3.setBounds(110, 175, 60, 25);
- repass.setBounds(150, 175, 200, 25);
-
- btn1.setBounds(150, 220, 70, 25);
- btn2.setBounds(250, 220, 70, 25);
- btn1.setFocusPainted(false);
- btn2.setFocusPainted(false);
-
- getContentPane().add(lb3);
- getContentPane().add(bl4);
- getContentPane().add(yzm);
- getContentPane().add(lb1);
- getContentPane().add(bl1);
- getContentPane().add(user);
- getContentPane().add(lb5);
- getContentPane().add(lb2);
- getContentPane().add(bl2);
- getContentPane().add(pass);
- getContentPane().add(lb4);
- getContentPane().add(bl3);
- getContentPane().add(repass);
- getContentPane().add(btn1);
- getContentPane().add(btn2);
-
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
- //注意将包名修改为自己创建的
- package an;
-
- import java.awt.BorderLayout;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
-
- public class New_s extends JFrame{
- public New_s() {
-
- setSize(500, 400);
- setLocation(1000, 300);
- setTitle("学生用户注册界面");
- setLayout(null);
- setResizable(false);
-
- JLabel lb1=new JLabel("新用户名");
- JLabel lb2=new JLabel(" 新密码");
- JLabel lb4=new JLabel("确认密码");
- JLabel lb3=new JLabel("请用'S'或's'开头");
- JTextField user = new JTextField(200);
- JPasswordField pass = new JPasswordField(200);
- JPasswordField repass = new JPasswordField(200);
- JButton btn1=new JButton("注册");
- JButton btn2=new JButton("清空");
-
- ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户名.png");
- ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\密码.png");
- ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\确认密码.png");
- JLabel bl1=new JLabel(im1);
- JLabel bl2=new JLabel(im2);
- JLabel bl3=new JLabel(im3);
-
- btn2.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- user.setText("");
- pass.setText("");
- repass.setText("");
- }
- });
-
-
- btn1.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String username=user.getText().trim(); //获取用户名
- String password=pass.getText().trim(); //获取密码
- String repassword=repass.getText().trim(); //获取确认密码
- if(user == null|| "".equals(username))
- {
- JOptionPane.showMessageDialog(null, "错误:新用户名不能为空");
- }
- else if(username.charAt(0)!='s'&&username.charAt(0)!='S') //用逻辑连接词'并',用'或'会有逻辑错误
- {
- JOptionPane.showMessageDialog(null, "新用户名不符合要求");
- user.setText("");
- }
- else if(pass == null|| "".equals(password))
- {
- JOptionPane.showMessageDialog(null, "错误:新密码不能为空");
- }
- else if(repass == null|| "".equals(repassword))
- {
- JOptionPane.showMessageDialog(null, "错误:确认密码不能为空");
- }
- else if( !(password.equals(repassword))) //检测密码与确认密码是否一致
- {
- JOptionPane.showMessageDialog(null, "错误:两次密码不一致");
- pass.setText("");
- repass.setText("");
- }
- else { //用户名和密码以及确认密码都输入正确才可进入
- PreparedStatement psts=null;
- PreparedStatement psti=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库,进入user表
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找用户名是否存在
- String sqlselect="select * from user where username='"+username+"'";
- psts=con.prepareStatement(sqlselect);
- rss=psts.executeQuery(sqlselect);
- if(rss.next()) {
- JOptionPane.showMessageDialog(null, "用户名已存在,请重新注册");
- return;
- }
- else {
- psti=con.prepareStatement("insert into user values(?,?)"); //SQL中占位符会自动加上单引号
- psti.setString(1, username);
- psti.setString(2, password);
- int rsi=psti.executeUpdate(); //括号内可不写语句
- if(rsi<0) {
- JOptionPane.showMessageDialog(null, "用户注册失败");
- }
- else {
- JDialog zct=new JDialog();
- zct.setSize(350, 150);
- zct.setLocation(800,380);
- zct.setTitle("提示");
- zct.getContentPane().add(new Label(" 学生用户注册成功"),BorderLayout.CENTER);
- zct.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- zct.setVisible(true);
- zct.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosed(WindowEvent e) {
- dispose(); //关闭学生注册窗口
- }
- });
- }
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(psti);
- Way.release(con);
- }
- }
- }
- });
-
- lb1.setBounds(70, 100, 60, 25);
- bl1.setBounds(110, 100, 60, 25);
- user.setBounds(150, 100, 200, 25);
- lb3.setBounds(350, 100, 100, 25);
- lb2.setBounds(70, 140, 60, 25);
- bl2.setBounds(110, 140, 60, 25);
- pass.setBounds(150, 140, 200, 25);
- lb4.setBounds(70, 175, 60, 25);
- bl3.setBounds(110, 175, 60, 25);
- repass.setBounds(150, 175, 200, 25);
- btn1.setBounds(150, 220, 70, 25);
- btn2.setBounds(250, 220, 70, 25);
- btn1.setFocusPainted(false);
- btn2.setFocusPainted(false);
-
- getContentPane().add(lb1);
- getContentPane().add(bl1);
- getContentPane().add(user);
- getContentPane().add(lb3);
- getContentPane().add(lb2);
- getContentPane().add(bl2);
- getContentPane().add(pass);
- getContentPane().add(lb4);
- getContentPane().add(bl3);
- getContentPane().add(repass);
- getContentPane().add(btn1);
- getContentPane().add(btn2);
-
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
- //注意将包名修改为自己创建的
- package an;
-
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- //登录界面窗口
- public class Dl extends JFrame{
- public Dl(){
-
- //设置窗口属性
- setSize(500, 400);
- setLocation(700, 300);
- getContentPane().setBackground(Color.gray); //设置背景颜色
- setTitle("学生寝室管理系统用户登录界面");
- setLayout(null); //设置空布局
- setResizable(false);
- //创建标签,文本框,按钮
- JLabel lb1=new JLabel("用户名");
- JLabel lb2=new JLabel(" 密码");
- JTextField user = new JTextField(200);
- JPasswordField pass = new JPasswordField(200);
- JButton btn1=new JButton("登录");
- JButton btn2=new JButton("清空");
- ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户名.png");
- ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\密码.png");
- JLabel lb3=new JLabel(im1);
- JLabel lb4=new JLabel(im2);
- //清空按钮事件监听
- btn2.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- user.setText(""); //设置文本框内为空字符(清空)
- pass.setText("");
-
- }
- });
-
- //登录按钮事件监听
- btn1.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String username=user.getText().trim(); //获取用户名 //.trim()方法可以排除空格
- String password=pass.getText().trim(); //获取密码
- if(user == null|| "".equals(username))
- {
- JOptionPane.showMessageDialog(null, "错误:用户名不能为空");
- }
- else if(pass == null|| "".equals(password))
- {
- JOptionPane.showMessageDialog(null, "错误:密码不能为空");
- }
- else { //用户名和密码都输入正确才可进入
- PreparedStatement psts=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库,进入user表
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找用户名是否存在
- String sqlselect="select * from user where username=? and password=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, username);
- psts.setString(2, password);
- rss=psts.executeQuery();
- if(rss.next()) {
- //检测用户名首字母是否为's'或'S'
- if(username.charAt(0)=='s'||username.charAt(0)=='S')
- {
- Stuwindow stu=new Stuwindow();
- setVisible(false);
- stu.addWindowListener(new WindowAdapter() {
- public void windowClosed(WindowEvent e) {
- dispose();
- }
- });
- }
- //检测用户名首字母是否为't'或'T'
- else if(username.charAt(0)=='t'||username.charAt(0)=='T')
- {
- Tcwindow tc=new Tcwindow();
- setVisible(false);
- tc.addWindowListener(new WindowAdapter() {
- public void windowClosed(WindowEvent e) {
- dispose();
- }
- });
- }
- else if("Admin".equals(username))
- {
- Mawindow ma=new Mawindow();
- setVisible(false);
- ma.addWindowListener(new WindowAdapter() {
- public void windowClosed(WindowEvent e) {
- dispose();
- }
- });
- }
- else {JOptionPane.showMessageDialog(null, "非法用户,无法登陆");}
- }
- else {
- JOptionPane.showMessageDialog(null, "用户名或密码错误");
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(con);
- }
-
- }
- }
- });
-
- //设置标签,文本框,按钮位置
- lb1.setBounds(80, 100, 60, 25);
- lb2.setBounds(80, 175, 60, 25);
- lb3.setBounds(110, 100, 60, 25);
- lb4.setBounds(110, 175, 60, 25);
- user.setBounds(150, 100, 200, 25);
- pass.setBounds(150, 175, 200, 25);
- btn1.setBounds(150, 220, 70, 25);
- btn2.setBounds(250, 220, 70, 25);
- btn1.setFocusPainted(false);
- btn2.setFocusPainted(false);
- //添加组件到面板上
- getContentPane().add(lb3);
- getContentPane().add(lb1);
- getContentPane().add(user);
- getContentPane().add(lb4);
- getContentPane().add(lb2);
- getContentPane().add(pass);
- getContentPane().add(btn1);
- getContentPane().add(btn2);
-
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //单独关闭此窗口(不会关闭所有窗口)
- setVisible(true);
-
- }
- }
-
-
学生用户与教师用户调用mysql中的表的内容。建立一个Vector<String>类型的变量attr<列名>与Vector<vector>类型的变量d,再建立一个JTable表格通过jdbc方法输出ResultSet通过循环将它的数据依次加入d变量中。在JTable表中加入attr与d。
数据无法全部显示的时候我将JTable表放入Scrollpane中,通过滚动面板显示所有数据。
学生功能界面窗口我使用的是东西南北中布局,东西南北分别用四个空标签填充,剩下的中部就用一个按钮铺满,详细代码如下:
- //注意将包名修改为自己创建的
- package an;
-
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Label;
- import java.awt.ScrollPane;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Vector;
-
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JTable;
-
- public class Stuwindow extends JFrame{
-
- public Stuwindow() {
- Button btn1=new Button("查看功能");
- Label l1=new Label();
- Label l2=new Label();
- Label l3=new Label();
- Label l4=new Label();
-
- l1.setBackground(Color.CYAN);
- l2.setBackground(Color.CYAN);
- l3.setBackground(Color.CYAN);
- l4.setBackground(Color.CYAN);
- //查看功能事件监听
- btn1.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- PreparedStatement pst=null;
- ResultSet rs=null;
- Connection con=null;
-
- JFrame jf=new JFrame("学生寝室信息");
- ScrollPane sp1=new ScrollPane();
- jf.setLocation(500, 200);
- jf.setSize(1000, 800);
- jf.setLayout(new BorderLayout());
-
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- String sql="select * from dinfo";
- pst=con.prepareStatement(sql);
- rs=pst.executeQuery();
- Vector<String> attr= new Vector<>(); //定义列名
- attr.add("姓名");
- attr.add("学籍号");
- attr.add("性别");
- attr.add("年龄");
- attr.add("床位号");
- attr.add("寝室门号");
- attr.add("学生公寓楼栋");
-
- Vector<Vector<Object>> data=new Vector<Vector<Object>>();
- while(rs.next()) {
- Vector<Object> d=new Vector<Object>();
- d.add(rs.getString("name"));
- d.add(rs.getString("sno"));
- d.add(rs.getString("sex"));
- d.add(rs.getInt("age"));
- d.add(rs.getString("bno"));
- d.add(rs.getInt("Dno"));
- d.add(rs.getInt("Sapb"));
- data.add(d);
- }
- JTable jt=new JTable(data, attr);
- jt.setEnabled(false);
- jt.setRowHeight(50);
- sp1.add(jt);
- jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
- jf.getContentPane().add(jt.getTableHeader(),BorderLayout.NORTH);
- jf.getContentPane().add(sp1,BorderLayout.CENTER);
- jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- jf.setVisible(true);
-
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rs);
- Way.release(pst);
- Way.release(con);
- }
-
- }
- });
-
- setSize(500, 400);
- setLocation(800, 600);
- setLayout(new BorderLayout());
- setTitle("学生功能界面");
- setResizable(false);
-
- getContentPane().add(l1,BorderLayout.EAST);
- getContentPane().add(l2,BorderLayout.WEST);
- getContentPane().add(l3,BorderLayout.NORTH);
- getContentPane().add(l4,BorderLayout.SOUTH);
- getContentPane().add(btn1,BorderLayout.CENTER);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
-
-
- }
- }
教师功能界面窗口的设计:我沿用了学生功能界面窗口的布局,中部加了个面板p1替代大按钮,p1使用的是网格布局,然后又添加了四个标签分别用来装四张图片,四个标签和四个功能按钮放在p1上调整位置使其呈现上图的效果。详细代码如下:
- //注意将包名修改为自己创建的
- package an;
-
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.Label;
- import java.awt.Panel;
- import java.awt.ScrollPane;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Vector;
-
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTable;
- //教师功能界面窗口
- public class Tcwindow extends JFrame{
- public Tcwindow() {
-
- //创建组件
- ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\查看.png");
- ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\添加.png");
- ImageIcon im3=new ImageIcon("C:\\Users\\Desktop\\images\\修改.png");
- ImageIcon im4=new ImageIcon("C:\\Users\\Desktop\\images\\删除.png");
- JLabel lb1=new JLabel(im1);
- JLabel lb2=new JLabel(im2);
- JLabel lb3=new JLabel(im3);
- JLabel lb4=new JLabel(im4);
- Button btn1=new Button("查看功能");
- Button btn2=new Button("添加功能");
- Button btn3=new Button("修改功能");
- Button btn4=new Button("删除功能");
- Panel p1=new Panel(new GridLayout(4,2)); //面板p1用网格布局装下四个按钮
- Label l1=new Label(); //用四个空标签填补东西南北四个区域
- Label l2=new Label();
- Label l3=new Label();
- Label l4=new Label();
- //设置标签背景颜色
- l1.setBackground(Color.CYAN);
- l2.setBackground(Color.CYAN);
- l3.setBackground(Color.CYAN);
- l4.setBackground(Color.CYAN);
- //查看功能事件监听
- btn1.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- PreparedStatement pst=null;
- ResultSet rs=null;
- Connection con=null;
-
- JFrame jf=new JFrame("学生寝室信息");
- ScrollPane sp1=new ScrollPane();
- jf.setLocation(500, 200);
- jf.setSize(1000, 800);
- jf.setLayout(new BorderLayout());
-
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- String sql="select * from dinfo";
- pst=con.prepareStatement(sql);
- rs=pst.executeQuery();
- Vector<String> attr= new Vector<>(); //定义列名
- attr.add("姓名");
- attr.add("学籍号");
- attr.add("性别");
- attr.add("年龄");
- attr.add("床位号");
- attr.add("寝室门号");
- attr.add("学生公寓楼栋");
-
- Vector<Vector<Object>> data=new Vector<Vector<Object>>();
- while(rs.next()) {
- Vector<Object> d=new Vector<Object>();
- d.add(rs.getString("name"));
- d.add(rs.getString("sno"));
- d.add(rs.getString("sex"));
- d.add(rs.getInt("age"));
- d.add(rs.getString("bno"));
- d.add(rs.getInt("Dno"));
- d.add(rs.getInt("Sapb"));
- data.add(d);
- }
- JTable jt=new JTable(data, attr);
- jt.setEnabled(false);
- jt.setRowHeight(50);
- sp1.add(jt);
- jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
- jf.getContentPane().add(jt.getTableHeader(),BorderLayout.NORTH);
- jf.getContentPane().add(sp1,BorderLayout.CENTER);
- jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- jf.setVisible(true);
-
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rs);
- Way.release(pst);
- Way.release(con);
- }
- }
- });
- //添加功能事件监听
- btn2.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- function fc=new function();
- fc.setTitle("添加功能");
- fc.bo.setText("添加");
- fc.bo.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String gna=fc.tf1.getText().trim(); //获取姓名
- String gsn=fc.tf2.getText().trim(); //获取学籍号
- String gse=fc.tf3.getText().trim(); //获取性别
- String gag=fc.tf4.getText().trim(); //获取年龄
- String gbn=fc.tf5.getText().trim(); //获取床位号
- String gdo=fc.tf6.getText().trim(); //获取寝室门号
- String gsap=fc.tf7.getText().trim(); //获取学生公寓楼栋
- if("".equals(gna)||"".equals(gsn)||"".equals(gse)||"".equals(gag)||"".equals(gbn)||"".equals(gdo)||"".equals(gsap)) {
- JOptionPane.showMessageDialog(null, "错误:不可以为空");
- return;
- }
- else {
- Integer gae=Integer.parseInt(gag);
- Integer gdn=Integer.parseInt(gdo);
- Integer gsab=Integer.parseInt(gsap);
- PreparedStatement psts=null;
- PreparedStatement psti=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找学籍号是否存在
- String sqlselect="select sno from dinfo where sno=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, gsn);
- rss=psts.executeQuery();
- if(rss.next()) {
- JOptionPane.showMessageDialog(null, "该学籍号已存在,请重新添加");
- return;
- }
- else {
- String sqlinsert="insert into dinfo values(?,?,?,?,?,?,?)";
- psti=con.prepareStatement(sqlinsert);
- psti.setString(1, gna);
- psti.setString(2, gsn);
- psti.setString(3, gse);
- psti.setInt(4, gae);
- psti.setString(5, gbn);
- psti.setInt(6, gdn);
- psti.setInt(7, gsab);
- int rsi=psti.executeUpdate();
- if(rsi<0) {
- JOptionPane.showMessageDialog(null, "信息添加失败");
- }
- else {
- JOptionPane.showMessageDialog(null, "信息添加成功");
- }
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "错误:添加的数据不合法");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(psti);
- Way.release(con);
- }
- }
- }
- });
-
- }
- });
- //修改功能事件监听
- btn3.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- smwin sw=new smwin();
- sw.setTitle("修改学生信息");
- sw.bt.setText("查找");
- sw.bt.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
- String snop=sw.jt.getText().trim();
- PreparedStatement psts=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找学籍号是否存在
- String sqlselect="select * from dinfo where sno=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, snop);
- rss=psts.executeQuery();
-
- if(rss.next()) {
- String name=rss.getString("name");
- String sno=rss.getString("sno");
- String sex=rss.getString("sex");
- String age=rss.getString("age");
- String bno=rss.getString("bno");
- String Dno=rss.getString("Dno");
- String Sapb=rss.getString("Sapb");
-
- function fct=new function();
- fct.setTitle("修改功能");
- fct.bo.setText("修改");
- fct.tf1.setText(""+name+"");
- fct.tf2.setText(""+sno+"");
- fct.tf3.setText(""+sex+"");
- fct.tf4.setText(""+age+"");
- fct.tf5.setText(""+bno+"");
- fct.tf6.setText(""+Dno+"");
- fct.tf7.setText(""+Sapb+"");
- fct.bo.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String gna=fct.tf1.getText().trim(); //获取姓名
- String gsn=fct.tf2.getText().trim(); //获取学籍号
- String gse=fct.tf3.getText().trim(); //获取性别
- String gag=fct.tf4.getText().trim(); //获取年龄
- String gbn=fct.tf5.getText().trim(); //获取床位号
- String gdo=fct.tf6.getText().trim(); //获取寝室门号
- String gsap=fct.tf7.getText().trim(); //获取学生公寓楼栋
- if("".equals(gna)||"".equals(gsn)||"".equals(gse)||"".equals(gag)||"".equals(gbn)||"".equals(gdo)||"".equals(gsap)) {
- JOptionPane.showMessageDialog(null, "错误:不可以为空");
- return;
- }
- else {
- Integer gae=Integer.parseInt(gag);
- Integer gdn=Integer.parseInt(gdo);
- Integer gsab=Integer.parseInt(gsap);
- PreparedStatement pstu=null;
- Connection conp=null;
-
- try{
- Class.forName("com.mysql.cj.jdbc.Driver");
- conp=DriverManager.getConnection(Way.url, Way.us, Way.ps);
-
- String sqlupdate="update dinfo set name=?,sno=?,sex=?,age=?,bno=?,Dno=?,Sapb=? where sno=?";
- pstu=conp.prepareStatement(sqlupdate);
- pstu.setString(1, gna);
- pstu.setString(2, gsn);
- pstu.setString(3, gse);
- pstu.setInt(4, gae);
- pstu.setString(5, gbn);
- pstu.setInt(6, gdn);
- pstu.setInt(7, gsab);
- pstu.setString(8, snop);
- int rsu=pstu.executeUpdate();
- if(rsu<0) {
- JOptionPane.showMessageDialog(null, "修改失败");
- }else
- {
- JOptionPane.showMessageDialog(null, "修改成功");
- fct.dispose();
- }
-
- }catch(Exception e1) {
- JOptionPane.showMessageDialog(null, "错误:修改的数据不合法");
- }finally {
- Way.release(pstu);
- Way.release(conp);
- }
- }
- }
-
- });
- }
- else {JOptionPane.showMessageDialog(null, "该学籍号未找到");
- sw.jt.setText("");
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(con);
- }
- }
- });
- }
- });
- //删除功能事件监听
- btn4.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- smwin sw=new smwin();
- sw.setTitle("删除学生信息");
- sw.bt.setText("删除");
- sw.bt.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
- String sno=sw.jt.getText().trim();
- PreparedStatement psts=null;
- PreparedStatement pstd=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找学籍号是否存在
- String sqlselect="select * from dinfo where sno=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, sno);
- rss=psts.executeQuery();
- if(rss.next()) {
- int p=JOptionPane.showConfirmDialog(null, "确认删除该学生信息吗?", "提示",
- JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
- if(p==JOptionPane.YES_OPTION) {
- String sqlinsert="delete from dinfo where sno=?";
- pstd=con.prepareStatement(sqlinsert);
- pstd.setString(1, sno);
- int rsd=pstd.executeUpdate();
- if(rsd<0) {
- JOptionPane.showMessageDialog(null, "删除数据失败");
- }
- else {
- JOptionPane.showMessageDialog(null, "删除数据成功");
- }
- }
- }
- else {
- JOptionPane.showMessageDialog(null, "错误:该学籍号未找到");
- return;
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(pstd);
- Way.release(con);
- }
-
- }
- });
-
- }
- });
- //设置窗口属性
- setSize(500, 400);
- setLocation(600, 500);
- setTitle("教师功能界面");
- setLayout(new BorderLayout()); //中部区域为东西南北占用后剩下的区域
- setResizable(false);
-
- getContentPane().add(l1,BorderLayout.NORTH);
- getContentPane().add(l2,BorderLayout.SOUTH);
- getContentPane().add(l3,BorderLayout.EAST);
- getContentPane().add(l4,BorderLayout.WEST);
- p1.add(btn1);
- p1.add(btn2);
- p1.add(lb1);
- p1.add(lb2);
- p1.add(btn3);
- p1.add(btn4);
- p1.add(lb3);
- p1.add(lb4);
- getContentPane().add(p1,BorderLayout.CENTER);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
由于我当时做课设的时候没有学懂直接在表格里面修改删除,所以我用了比较复杂的方法来实现修改和删除功能。
学生宿舍信息的修改和删除功能都是通过学籍号搜索来找到对应的数据,删除功能找到学籍号后就会直接删除,不会显示删除的数据。修改窗口沿用了添加窗口,只是多了个数据回显。
这里我展示的是修改用户信息的相应窗口,和搜索学籍号的窗口是差不多的,我就不再多展示了。
管理员不仅可以对学生宿舍的信息进行操作,还能对用户的账号和密码进行操作。详细代码如下:
- //注意将包名修改为自己创建的
- package an;
-
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
-
- public class Mawindow extends JFrame{
- public Mawindow() {
- setTitle("管理员功能界面");
- setSize(500, 550);
- setLocation(650, 300);
- setLayout(null);
- this.getContentPane().setBackground(Color.DARK_GRAY);
- ImageIcon im1=new ImageIcon("C:\\Users\\Desktop\\images\\用户管理.png");
- ImageIcon im2=new ImageIcon("C:\\Users\\Desktop\\images\\寝室管理.png");
- JLabel lb1=new JLabel(im1);
- JLabel lb2=new JLabel(im2);
- JButton usm=new JButton("用户信息管理");
- JButton dom=new JButton("寝室信息管理");
- lb1.setBounds(70, 80, 150, 130);
- lb2.setBounds(270, 80, 150, 130);
- usm.setFocusPainted(false);
- dom.setFocusPainted(false);
- usm.setBounds(80, 250, 120, 120);
- dom.setBounds(280, 250, 120, 120);
- //用户信息管理事件监听
- usm.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- Usermanage u1=new Usermanage();
- setVisible(false);
- u1.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosed(WindowEvent e) {
- setVisible(true);
- }
- });
-
- }
- });
- //寝室信息管理事件监听
- dom.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- Tcwindow t1=new Tcwindow();
- t1.setTitle("寝室信息管理功能界面");
- setVisible(false);
- t1.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosed(WindowEvent e) {
- setVisible(true);
- }
- });
-
- }
- });
- getContentPane().add(usm);
- getContentPane().add(dom);
- getContentPane().add(lb1);
- getContentPane().add(lb2);
-
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
由于学生宿舍信息操作和教师功能界面大同小异,故我就只展示用户信息的操作。
详细代码如下:
- //注意将包名修改为自己创建的
- package an;
-
- import java.awt.BorderLayout;
- import java.awt.ScrollPane;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Vector;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTable;
- import javax.swing.plaf.BorderUIResource;
-
- public class Usermanage extends JFrame{
- public Usermanage() {
- PreparedStatement psts=null;
- ResultSet rs=null;
- Connection con=null;
-
- setTitle("用户信息");
- JButton ref=new JButton("刷新");
- JButton upd=new JButton("修改");
- JButton apd=new JButton("添加");
- JButton del=new JButton("删除");
- ScrollPane scp=new ScrollPane();
- JPanel jp=new JPanel();
- setLocation(500, 200);
- setSize(1000, 800);
- setLayout(new BorderLayout());
-
- ref.setFocusPainted(false);
- upd.setFocusPainted(false);
- apd.setFocusPainted(false);
- del.setFocusPainted(false);
- //刷新按钮事件监听
- ref.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- setVisible(false);;
- Usermanage m1=new Usermanage();
- m1.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosed(WindowEvent e) {
- dispose();
- }
- });
-
- }
- });
- //修改按钮事件监听
- upd.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- usersrch usch =new usersrch();
- usch.setTitle("修改用户信息");
- usch.bt.setText("搜索");
- usch.bt.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
- String susname=usch.jt.getText().trim();
- PreparedStatement psts=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找用户名是否存在
- String sqlselect="select * from user where username=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, susname);
- rss=psts.executeQuery();
- if(rss.next()) {
- String usname=rss.getString("username");
- String psword=rss.getString("password");
-
- admwin adw=new admwin();
- adw.setTitle("修改功能");
- adw.bo.setText("修改");
- adw.tf1.setText(""+usname+"");
- adw.tf2.setText(""+psword+"");
- adw.bo.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String usn=adw.tf1.getText().trim(); //获取用户名
- String psw=adw.tf2.getText().trim(); //获取密码
- if("".equals(usn)||"".equals(psw)) {
- JOptionPane.showMessageDialog(null, "错误:不可以为空");
- return;
- }
- else {
- PreparedStatement pstu=null;
- Connection conp=null;
-
- try{
- Class.forName("com.mysql.cj.jdbc.Driver");
- conp=DriverManager.getConnection(Way.url, Way.us, Way.ps);
-
- String sqlupdate="update user set username=?,password=? where username=?";
- pstu=conp.prepareStatement(sqlupdate);
- pstu.setString(1, usn);
- pstu.setString(2, psw);
- pstu.setString(3, susname);
- int rsu=pstu.executeUpdate();
- if(rsu<0) {
- JOptionPane.showMessageDialog(null, "修改失败");
- }else
- {
- JOptionPane.showMessageDialog(null, "修改成功");
- adw.dispose();
- usch.dispose();
- }
-
- }catch(Exception e1) {
- JOptionPane.showMessageDialog(null, "错误:修改的用户名已存在");
- }finally {
- Way.release(pstu);
- Way.release(conp);
- }
- }
- }
-
- });
- }
- else {JOptionPane.showMessageDialog(null, "该用户名未找到");
- usch.jt.setText("");
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(con);
- }
- }
- });
-
- }
- });
- //添加按钮事件监听
- apd.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- admwin adw=new admwin();
- adw.setTitle("添加功能");
- adw.bo.setText("添加");
- adw.bo.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- String usn=adw.tf1.getText().trim(); //获取用户名
- String psw=adw.tf2.getText().trim(); //获取密码
- if("".equals(usn)||"".equals(psw)) {
- JOptionPane.showMessageDialog(null, "错误:不可以为空");
- return;
- }
- else {
- PreparedStatement psts=null;
- PreparedStatement psti=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找用户名是否存在
- String sqlselect="select username from user where username=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, usn);
- rss=psts.executeQuery();
- if(rss.next()) {
- JOptionPane.showMessageDialog(null, "该用户名已存在,请重新添加");
- return;
- }
- else {
- String sqlinsert="insert into user values(?,?)";
- psti=con.prepareStatement(sqlinsert);
- psti.setString(1, usn);
- psti.setString(2, psw);
- int rsi=psti.executeUpdate();
- if(rsi<0) {
- JOptionPane.showMessageDialog(null, "用户信息添加失败");
- }
- else {
- JOptionPane.showMessageDialog(null, "用户信息添加成功");
- adw.dispose();
- }
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "错误:添加的数据不合法");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(psti);
- Way.release(con);
- }
- }
- }
- });
-
- }
- });
- //删除按钮事件监听
- del.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- usersrch usch=new usersrch();
- usch.setTitle("删除用户信息");
- usch.bt.setText("删除");
- usch.bt.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
- String susname=usch.jt.getText().trim();
- PreparedStatement psts=null;
- PreparedStatement pstd=null;
- ResultSet rss=null;
- Connection con=null;
- try {
- //连接数据库
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- //查找用户名是否存在
- String sqlselect="select * from user where username=?";
- psts=con.prepareStatement(sqlselect);
- psts.setString(1, susname);
- rss=psts.executeQuery();
- if(rss.next()) {
- int p=JOptionPane.showConfirmDialog(null, "确认删除该用户信息吗?", "提示",
- JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
- if(p==JOptionPane.YES_OPTION) {
- String sqlinsert="delete from user where username=?";
- pstd=con.prepareStatement(sqlinsert);
- pstd.setString(1, susname);
- int rsd=pstd.executeUpdate();
- if(rsd<0) {
- JOptionPane.showMessageDialog(null, "删除数据失败");
- }
- else {
- JOptionPane.showMessageDialog(null, "删除数据成功");
- usch.dispose();
- }
- }
- }
- else {
- JOptionPane.showMessageDialog(null, "错误:该用户名未找到");
- return;
- }
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rss);
- Way.release(psts);
- Way.release(pstd);
- Way.release(con);
- }
-
- }
- });
-
- }
- });
-
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- con=DriverManager.getConnection(Way.url, Way.us, Way.ps);
- String sql="select * from user";
- psts=con.prepareStatement(sql);
- rs=psts.executeQuery();
- Vector<String> attr= new Vector<>(); //定义列名
- attr.add("用户名");
- attr.add("密码");
-
- Vector<Vector<Object>> data=new Vector<Vector<Object>>();
- while(rs.next()) {
- Vector<Object> d=new Vector<Object>();
- d.add(rs.getString("username"));
- d.add(rs.getString("password"));
- data.add(d);
- }
- JTable jt=new JTable(data, attr);
- jt.setEnabled(false);
- jt.setRowHeight(50);
- scp.add(jt);
- jp.add(ref);
- jp.add(upd);
- jp.add(apd);
- jp.add(del);
- jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
- getContentPane().add(jt.getTableHeader(),BorderLayout.NORTH);
- getContentPane().add(jp,BorderLayout.SOUTH);
- getContentPane().add(scp,BorderLayout.CENTER);
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setVisible(true);
-
-
- }catch(Exception e1){
- JOptionPane.showMessageDialog(null, "系统异常");
- }finally {
- Way.release(rs);
- Way.release(psts);
- Way.release(con);
- }
- }
- }
此类包括连接数据库以及释放数据库资源的方法,还有搜索窗口等。
数据库连接的时候要改为自己电脑上的数据库用户名和密码!!!
详细代码如下:
- //注意将包名修改为自己创建的
- package an;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
-
- /**
- * @author iujth
- *
- */
-
- //主类
- public class Way{
- //释放数据库资源方法
- public static void release(Object stream) {
- if (stream != null) {
- try {
- if (stream instanceof Connection) {
- ((Connection) stream).close();
- }
- if (stream instanceof PreparedStatement) {
- ((PreparedStatement) stream).close();
- }
- if (stream instanceof ResultSet) {
- ((ResultSet) stream).close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- stream=null;
- }
- }
-
-
-
- //定义常量连接数据库路径及用户名和密码(注意改为自己的数据库用户名和密码!!!)
- public static final String url="jdbc:mysql://localhost:3306/stu_dor"+"?serverTimezone=GMT%2B8&useSSL=false";
- public static final String us="root";
- public static final String ps="001224";
- }
-
-
-
-
- //教师功能方法
- //修改和删除功能的查找学籍号界面
- class smwin extends JFrame{
- @Override
- public void setTitle(String title) {
- // TODO Auto-generated method stub
- super.setTitle(title);
- }
- JLabel jb=new JLabel("请输入学籍号:");
- JTextField jt=new JTextField(9);
- JButton bt=new JButton();
- smwin(){
- setSize(320, 150);
- setLocation(800, 500);
- setResizable(false);
- setLayout(null);
-
- jb.setBounds(20, 50, 100, 20);
- jt.setBounds(110, 50, 100, 20);
- bt.setBounds(220, 50, 60, 20);
- bt.setFocusPainted(false);
-
- getContentPane().add(jb);
- getContentPane().add(jt);
- getContentPane().add(bt);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
- //寝室信息添加和修改功能的编辑界面
- class function extends JFrame{
- @Override
- public void setTitle(String title) {
- // TODO Auto-generated method stub
- super.setTitle(title);
- }
- JLabel lb1=new JLabel("姓名");
- JLabel lb2=new JLabel("学籍号");
- JLabel lb3=new JLabel("性别");
- JLabel lb4=new JLabel("年龄");
- JLabel lb5=new JLabel("床位号");
- JLabel lb6=new JLabel("寝室门号");
- JLabel lb7=new JLabel("学生寝室楼栋");
- JLabel tip1=new JLabel("请填写9位数");
- JLabel tip2=new JLabel("请填写'左一'或'左二'或'右一'或'右二'");
- JLabel tip3=new JLabel("请填写100-699内的数");
- JLabel tip4=new JLabel("请填写1-30内的数");
- JTextField tf1=new JTextField(20);
- JTextField tf2=new JTextField(20);
- JTextField tf3=new JTextField(20);
- JTextField tf4=new JTextField(20);
- JTextField tf5=new JTextField(20);
- JTextField tf6=new JTextField(20);
- JTextField tf7=new JTextField(20);
- JButton bo=new JButton();
- function(){
- setSize(800, 500);
- setLocation(650, 500);
- setResizable(false);
- setLayout(null);
-
- lb1.setBounds(20, 30, 50, 20);
- tf1.setBounds(80, 30, 120, 20);
- lb2.setBounds(15, 80, 80, 20);
- tf2.setBounds(80, 80, 120, 20);
- tip1.setBounds(210, 80, 90, 20);
- lb3.setBounds(20, 130, 80, 20);
- tf3.setBounds(80, 130, 20, 20);
- lb4.setBounds(20, 180, 40, 20);
- tf4.setBounds(80, 180, 40, 20);
-
- lb5.setBounds(300, 50, 50, 20);
- tf5.setBounds(370, 50, 120, 20);
- tip2.setBounds(500, 50, 220, 20);
- lb6.setBounds(300, 100, 100, 20);
- tf6.setBounds(370, 100, 120, 20);
- tip3.setBounds(500, 100, 150, 20);
- lb7.setBounds(280, 150, 120, 20);
- tf7.setBounds(370, 150, 120, 20);
- tip4.setBounds(500, 150, 120, 20);
-
- bo.setFocusPainted(false);
- bo.setBounds(320, 300, 80, 20);
-
- getContentPane().add(lb1);
- getContentPane().add(tf1);
- getContentPane().add(lb2);
- getContentPane().add(tf2);
- getContentPane().add(tip1);
- getContentPane().add(lb3);
- getContentPane().add(tf3);
- getContentPane().add(tip3);
- getContentPane().add(lb4);
- getContentPane().add(tf4);
- getContentPane().add(tip4);
-
- getContentPane().add(lb5);
- getContentPane().add(tf5);
- getContentPane().add(tip2);
- getContentPane().add(lb6);
- getContentPane().add(tf6);
- getContentPane().add(lb7);
- getContentPane().add(tf7);
- getContentPane().add(bo);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
-
-
-
-
- //管理员功能方法
- //用户信息添加和修改功能的编辑界面
- class admwin extends JFrame{
- @Override
- public void setTitle(String title) {
- // TODO Auto-generated method stub
- super.setTitle(title);
- }
- JLabel lb1=new JLabel("用户名");
- JLabel lb2=new JLabel("密码");
- JTextField tf1=new JTextField(200);
- JTextField tf2=new JTextField(200);
- JButton bo=new JButton();
- public admwin() {
- setSize(500, 400);
- setLocation(700, 300);
- setLayout(null);
- setResizable(false);
- lb1.setBounds(100, 100, 60, 25);
- lb2.setBounds(100, 175, 60, 25);
- tf1.setBounds(150, 100, 200, 25);
- tf2.setBounds(150, 175, 200, 25);
- bo.setBounds(200, 220, 70, 25);
- bo.setFocusPainted(false);
-
- getContentPane().add(lb1);
- getContentPane().add(tf1);
- getContentPane().add(lb2);
- getContentPane().add(tf2);
- getContentPane().add(bo);
-
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
-
- //修改和删除功能的查找用户名界面
- class usersrch extends JFrame{
- @Override
- public void setTitle(String title) {
- // TODO Auto-generated method stub
- super.setTitle(title);
- }
- JLabel jb=new JLabel("请输入用户名:");
- JTextField jt=new JTextField(9);
- JButton bt=new JButton();
- usersrch(){
- setSize(320, 150);
- setLocation(800, 500);
- setResizable(false);
- setLayout(null);
-
- jb.setBounds(20, 50, 100, 20);
- jt.setBounds(110, 50, 100, 20);
- bt.setBounds(220, 50, 60, 20);
- bt.setFocusPainted(false);
-
- getContentPane().add(jb);
- getContentPane().add(jt);
- getContentPane().add(bt);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- }
运行初始界面的的类:
- //注意将包名修改为自己创建的
- package an;
-
- public class Start {
-
- public static void main(String[] args) {
- new Cs();
- }
- }
-
本人使用的是MySQL8.0.31版本,eclipse是2018-09(4.9.0)版本。
下载jar包和具体连接教程请参考:Eclipse连接MySQL数据库(补充8.0)_Lime-W的博客-CSDN博客
图片素材库可以参考此网站:iconfont-阿里巴巴矢量图标库
放图片的时候请记得改对路径。
由于该课程设计是本人在大二上学期期末敲了12天才完成的,故代码有些冗余和繁多。这篇文章是大概过了半年才写的,所以对相应的细节也有些记不得了,如果本人有讲错的地方欢迎指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。