用数据库做一个简单的实验室安排系统
要求如下:
为《算法与数据结构设计》课程开发实验室安排系统。该课程开课时间为2周,在两周内需要上6次课,每次连续4个学时。
每天上午下午可各安排一次课,周末不安排上课。该系统包含两个程序:管理员程序和教师申请程序:
在教师申请界面中:
教师录入所授课班级ID、班级人数、不希望的时间安排(例如不安排周一上午和周四下午);信息提交后将被存储到后台数据库中。
在管理员界面中:
管理员录入现有的实验室信息,包括实验室地址、实验室所能容纳学生数量。管理员可以选择“课表生成”,实验室安排算法读入已有
申请和实验室信息,生成总课程安排,并写入数据库。
当总课程安排生成后,教师再次进入教师申请程序时,可以看到自己的课表。
请使用C/C++或Java实现。数据库可随意选择。
基本要求:
(1) 两个程序可以由同一个界面进行入口,或者分别两个界面,不要求身份验证;用户只需要输入ID即可,ID事先已写入数据库。
(2) 算法生成的课程安排,要求尽可能为每个老师安排6次课,所安排实验室能容纳所上课班级;按照教师申请的时间次序,尽可能
优先满足先申请教师的期望实验安排;当申请较多而管理员录入的实验室较少,允许出现有的教师申请无法满足的情况;
(3) 实物演示时要求能够说明所采用算法思想;
(4) 程序操作友好、健壮;操作界面简洁美观。
提高要求:
(1) 完善程序功能。例如身份验证、教师与程序员可以修改自己的录入信息等。
(2) 先进而高效的算法。
(3) 界面美观,课表整齐美观。
准备工作:
工具:eclipse
MySQL5.6
MySQL连接驱动:mysql-connector-java-5.1.27.jar
加载驱动:
1. 在工程目录中创建lib文件夹,将下载好的JDBC放到该文件夹下,如下图所示:
2. 右键工程名,在java build path中的Libraries分页中选择Add JARs...,选择刚才添加的JDBC,如下图:
实现步骤:
1,系统主界面的设计
用户输入账号密码后选择教师或者管理员身份进行登录(默认密登录码均为1),若果账户名或者密码输入错误,则无法登录,
重新输入,效果图如下:
代码:
package 实验室安排系统; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MainUI extends JFrame implements ActionListener { //定义组件 JButton jb1,jb2,jb3=null; JRadioButton jrb1,jrb2=null; JPanel jp1,jp2,jp3,jp4=null; JTextField jtf=null; JLabel jlb1,jlb2,jlb3=null; JPasswordField jpf=null; ButtonGroup bg=null; //设定用户名和密码 final String gly_name="GLY"; final String gly_pwd="1"; String tername=null; final String tea_pwd="1"; public static void main(String[] args) { MainUI mUI=new MainUI(); } public MainUI() { //创建组件 jb1=new JButton("登录"); jb1.setBounds(91, 5, 83, 29); jb2=new JButton("退出"); jb2.setBounds(225, 5, 83, 29); //设置监听 jb1.addActionListener(this); jb2.addActionListener(this); jrb1=new JRadioButton("教师"); jrb1.setBounds(145, 5, 69, 29); jrb2=new JRadioButton("\u7BA1\u7406\u5458"); jrb2.setBounds(258, 5, 87, 29); bg=new ButtonGroup(); bg.add(jrb1); bg.add(jrb2); jrb2.setSelected(true); //初始页面默认选择权限为管理员 jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); jlb1=new JLabel("\u7528 \u6237 \u540D\uFF1A"); jlb1.setBounds(43, 8, 90, 21); jlb2=new JLabel(" \u5BC6 \u7801 \uFF1A"); jlb2.setBounds(44, 8, 95, 21); jlb3=new JLabel("\u6743 \u9650\uFF1A"); jlb3.setBounds(54, 9, 72, 21); jtf=new JTextField(10); jtf.setBounds(144, 5, 197, 27); jpf=new JPasswordField(10); jpf.setBounds(146, 5, 195, 27); jp1.setLayout(null); //加入到JPanel中 jp1.add(jlb1); jp1.add(jtf); jp2.setLayout(null); jp2.add(jlb2); jp2.add(jpf); jp3.setLayout(null); jp3.add(jlb3); //添加标签 jp3.add(jrb1); jp3.add(jrb2); jp4.setLayout(null); jp4.add(jb1); //添加按钮 jp4.add(jb2); //加入JFrame中 getContentPane().add(jp1); getContentPane().add(jp2); getContentPane().add(jp3); getContentPane().add(jp4); getContentPane().setLayout(new GridLayout(4,1)); //选择GridLayout布局管理器 this.setTitle("实验室安排系统"); this.setSize(400,200); this.setLocation(600, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置当关闭窗口时,保证JVM也退出 this.setVisible(true); this.setResizable(true); } public void actionPerformed(ActionEvent e) { //事件判断 if(e.getActionCommand()=="登录") { //如果选中教师登录 if(jrb1.isSelected()) { tealogin(); //连接到教师的方法 页面 }else if(jrb2.isSelected()) //学生在登录系统 { glylogin(); //连接到管理员的方法 页面 } }else if(e.getActionCommand()=="退出") { dispose(); } } //管理员登录判断方法 public void glylogin() { if(gly_name.equals(jtf.getText())&&gly_pwd.equals(jpf.getText())) { JOptionPane.showMessageDialog(null,"登录成功!","提示消息",JOptionPane.WARNING_MESSAGE); dispose(); clear(); GlyUI ui=new GlyUI(); //创建新界面 }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else { JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE); //清空输入框 clear(); } } //教师登录判断方法 public void tealogin() { if(tea_pwd.equals(jpf.getText())) { tername=jtf.getText(); JOptionPane.showMessageDialog(null,"登录成功!","提示消息",JOptionPane.WARNING_MESSAGE); clear(); dispose(); TerUI ui=new TerUI(tername); //创建一个新界面 }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else { JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE); clear(); //清空输入框 } } //清空文本框和密码框 public void clear() { jtf.setText(""); jpf.setText(""); } }
2,管理员界面设计
以管理员身份登录成功后,可以进行信息(实验室地址,实验室容纳量)录入,信息修改,生成课表等操作,效果图如下:
代码:
package 实验室安排系统; import java.awt.*; import java.awt.event.*; import java.sql.SQLException; import javax.swing.*; import 实验室安排系统.MainUI; public class GlyUI extends JFrame implements ActionListener { //定义组件 JButton jb1,jb2,jb3=null; JPanel jp3,jp4=null; JLabel jlb1,jlb3,jlb4,jlb6=null; public static void main(String[] args) { GlyUI ui=new GlyUI(); } public GlyUI() { jp3=new JPanel(); jp3.setBounds(0, 220, 378, 23); getContentPane().setLayout(null); jlb1=new JLabel("\u4F60\u597D\uFF0C\u7BA1\u7406\u5458"); jlb1.setBounds(68, 15, 231, 49); getContentPane().add(jlb1); jb2=new JButton("\u4FE1\u606F\u5F55\u5165"); jb2.setBounds(15, 79, 105, 61); getContentPane().add(jb2); jb2.setForeground(Color.BLUE); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); new GlyInputUI(); } }); //创建组件 jb3=new JButton("信息修改"); jb3.setForeground(Color.BLUE); jb3.setBounds(135, 79, 105, 61); getContentPane().add(jb3); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new glyxg(); } }); jb1=new JButton("\u751F\u6210\u8BFE\u8868"); jb1.setBounds(255, 79, 105, 61); getContentPane().add(jb1); jb1.setForeground(Color.BLUE); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { GlykbUI gl=new GlykbUI(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //生成课表 JOptionPane.showMessageDialog(null,"生成成功","提示消息",JOptionPane.WARNING_MESSAGE); dispose(); new GlyUI(); } }); getContentPane().add(jp3); jlb3=new JLabel("最新公告:"); jlb3.setBounds(68, 180, 90, 21); getContentPane().add(jlb3); jlb3.setForeground(Color.red); jlb4=new JLabel("\u8BF7\u5C3D\u5FEB\u5B8C\u5584\u6559\u5B66\u4FE1\u606F"); jlb4.setBounds(173, 180, 162, 21); getContentPane().add(jlb4); this.setTitle("实验室安排系统"); this.setSize(400,300); this.setLocation(500, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e) { } }
2.1 管理员进入信息录入界面,输入实验室ID,实验室人数后点提交,将数据长传至数据库,成功后点击返回,返回到管理员界面:
其中,实验室的数据表表结构如下:
代码:
package 实验室安排系统; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.TableColumn; import javax.swing.JPanel; import javax.swing.JPasswordField; import java.awt.BorderLayout; import java.awt.GridLayout; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.lang.model.type.NullType; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class GlyInputUI extends JFrame implements ActionListener { //定义组件 JPanel jp1,jp2,jp4=null; JButton jb1=null,jb2=null; JLabel jlb1,jlb2,jlb3=null; JTextField jtf1,jtf2=null; public static void main(String[] args) { new GlyInputUI(); } public static void addroom(String romid,int romnum){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from administrator"; ResultSet rs = statement.executeQuery(sql); int rowcount=0; while(rs.next()){ rowcount=rs.getInt(1); } String sql1 = "insert into administrator values(?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) con.prepareStatement(sql1); pstmt.setInt(1, rowcount+1); pstmt.setString(2, romid); pstmt.setInt(3, romnum); pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } rs.close(); con.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public GlyInputUI() { //创建组件 jb1=new JButton("提交"); jb1.setBounds(40, 0, 145, 40); jb1.addActionListener(this); jb2=new JButton("返回"); jb2.setBounds(209, 0, 139, 40); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new GlyUI(); } }); jp1=new JPanel(); jp2=new JPanel(); jp4=new JPanel(); jlb1=new JLabel("\u5B9E\u9A8C\u5BA4ID"); jlb1.setBounds(43, 8, 90, 21); jlb2=new JLabel(" \u5B9E\u9A8C\u5BA4\u4EBA\u6570"); jlb2.setBounds(37, 8, 114, 21); jtf1=new JTextField(10); jtf1.setBounds(152, 5, 197, 27); jtf2=new JTextField(10); jtf2.setBounds(155, 5, 195, 27); jp1.setLayout(null); //加入到JPanel中 jp1.add(jlb1); jp1.add(jtf1); jp2.setLayout(null); jp2.add(jlb2); jp2.add(jtf2); jp4.setLayout(null); jp4.add(jb1); //添加按钮 jp4.add(jb2); //加入JFrame中 getContentPane().add(jp1); getContentPane().add(jp2); getContentPane().add(jp4); getContentPane().setLayout(new GridLayout(3,1)); //选择GridLayout布局管理器 this.setTitle("实验室安排系统"); this.setSize(400,200); this.setLocation(400, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置当关闭窗口时,保证JVM也退出 this.setVisible(true); this.setResizable(true); } public void actionPerformed(ActionEvent e) { //事件判断 if(e.getActionCommand()=="提交") { String romid=jtf1.getText(); String snum=jtf2.getText(); if(jtf1.getText().isEmpty()||jtf2.getText().isEmpty()){ JOptionPane.showMessageDialog(null,"无效的输入","提示消息",JOptionPane.WARNING_MESSAGE); } else{ int num=Integer.parseInt(snum); addroom(romid, num); JOptionPane.showMessageDialog(null,"添加成功!","提示消息",JOptionPane.WARNING_MESSAGE); } } } }
2.2管理员修改信息设计
管理员可以修改实验室的容量,输入后点击修改,将更新数据库中的记录信息
代码:
package 实验室安排系统; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; public class glyxg extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ public static void main(String[] args) { new glyxg(); } /** * Create the frame. */ public glyxg() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u4FEE\u6539\u5B9E\u9A8C\u5BA4\u5BB9\u91CF"); lblNewLabel.setBounds(161, 32, 252, 21); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u8BF7\u8F93\u5165\u8981\u4FEE\u6539\u7684\u5B9E\u9A8C\u5BA4ID:"); lblNewLabel_1.setBounds(40, 69, 246, 21); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(301, 66, 96, 27); contentPane.add(textField); textField.setColumns(10); JLabel lblNewLabel_2 = new JLabel("\u8BF7\u8F93\u5165\u4FEE\u6539\u540E\u7684\u5B9E\u9A8C\u5BA4\u5BB9\u91CF\uFF1A"); lblNewLabel_2.setBounds(40, 133, 234, 21); contentPane.add(lblNewLabel_2); textField_1 = new JTextField(); textField_1.setBounds(301, 130, 96, 27); contentPane.add(textField_1); textField_1.setColumns(10); JButton btnNewButton = new JButton("\u786E\u8BA4\u4FEE\u6539"); btnNewButton.setBounds(40, 186, 163, 29); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String rid=textField.getText(); String ru=textField_1.getText(); int romnum=Integer.parseInt(ru); try { xg(rid, romnum); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } JOptionPane.showMessageDialog(null,"修改成功!","提示消息",JOptionPane.WARNING_MESSAGE); } });; contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u8FD4\u56DE\u4E0A\u4E00\u7EA7"); btnNewButton_1.setBounds(252, 186, 145, 29); btnNewButton_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new GlyUI(); } }); contentPane.add(btnNewButton_1); this.setVisible(true); this.setLocation(500, 300); } public static void xg(String s1,int n) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rs=0; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); PreparedStatement stmt=null; String sql="update administrator set roomnum = "+String.valueOf(n)+" where roomid = "+s1; stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.3管理员生成课表算法
排课表算法的大致思路:
1) 同一班级的学生在同一时间不能安排在两个实验室
2) 同一教师在同一时间不能教授两个班级
3) 同一实验室在同一时间不能安排两个班级
4) 某一实验室上课的人数不应大于实验室的容纳量
5) 课程在一周上多次时, 要有一定的间隔性
6) 对同一教师, 同一上课对象应尽量选择相对固定的几个实验室
7) 优先满足一些特殊要求(比如有些老师喜欢上上午的课,可以优先满足,有些老师不喜欢上某个时间片的课,我们尽量避免给他们在这个时间段排课)
因此,我将两周的时间分为20个时间片,以老师的喜好时间为基础来给他们安排合适的时间片来开展教学活动。时间片的排布如下:
我们这样定义教师的数据表:
(注:id为记录在数据表中的位置,与优先级有关,name为教师姓名,classid为教师所授班名称,classnum为班级人数,badtime1为不想上课的时间片1,
badtime2位不想上课的时间片2,count为授课次数是6次)
定义课程表的表结构:
(注:id为记录在数据表中的位置,与优先级有关,name为教师姓名,room为上课地点,bd1为不想上课的时间片1,bd2位不想上课的时间2,t1~t6为教师
上课的时间片)
生成课表算法:要生成A老师的课表,先通过访问数据库获得A老师不想被安排上课的时间片,和A老师要上课的实验室,在A老师的可用时间片里,为A老师
生成6个上课的时间片,并写入到数据库课程表表单中,供老师查询,算法的流程图如下;
代码:
package 实验室安排系统; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import java.util.Random; import java.util.Set; import java.util.TreeSet; import javax.swing.JOptionPane; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class GlykbUI { public static void main(String[] args) throws SQLException { new GlykbUI(); } public GlykbUI() throws SQLException{ int ternum=Getternum(); int timetabenum=Gettimetablenum(); for(int i=timetabenum+1;i<=ternum;i++){ String tname=null,roomid=null; int tb1=0,tb2=0; ResultSet rs1=find(i, "teacher"); while(rs1.next()){ tname=rs1.getString(2); tb1=rs1.getInt(5); tb2=rs1.getInt(6); } if(i<=10){ ResultSet rs2=find(i, "administrator"); while(rs2.next()){ roomid=rs2.getString(2); } }else{ ResultSet rs2=find(i-10, "administrator"); while(rs2.next()){ roomid=rs2.getString(2); } } //System.out.println(tname+roomid); Set<Integer> set=getrandom(i,tb1, tb2); int time[]=new int[6]; int j=0; Iterator<Integer> it=set.iterator(); while(it.hasNext()){ time[j++]=it.next(); } //System.out.println(Arrays.toString(time)); addtimetable(i, tname, roomid, tb1, tb2, time); } } public static int Getternum(){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rowcount=0; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from teacher"; ResultSet rs = statement.executeQuery(sql); while(rs.next()){ rowcount=rs.getInt(1); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rowcount; } public static int Gettimetablenum(){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rowcount=0; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from timetable"; ResultSet rs = statement.executeQuery(sql); while(rs.next()){ rowcount=rs.getInt(1); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rowcount; } public static ResultSet find(int i,String s) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; ResultSet rs=null; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); //if(!con.isClosed()) //System.out.println("Succeeded connecting to the Database!"); PreparedStatement stmt=null; String sql="select * from "+s+" where id = "+String.valueOf(i); stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeQuery(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public static Set<Integer> getrandom(int i,int a,int b) throws SQLException{ Random ran=new Random(i); Set<Integer> set =new TreeSet<>(); if(i<=10){ while(true){ int r1=ran.nextInt(3)+1; if(r1!=a&&r1!=b) set.add(r1); if(set.size()==1) break; } while(true){ int r2=ran.nextInt(3)+4; if(r2!=a&&r2!=b) set.add(r2); if(set.size()==2) break; } while(true){ int r3=ran.nextInt(4)+7; if(r3!=a&&r3!=b) set.add(r3); if(set.size()==3) break; } System.out.println(set); while(true){ int r4=ran.nextInt(3)+11; if(r4!=(a+10)&&r4!=(b+10)) set.add(r4); if(set.size()==4) break; } while(true){ int r5=ran.nextInt(3)+14; if(r5!=(a+10)&&r5!=(b+10)) set.add(r5); if(set.size()==5) break; } while(true){ int r6=ran.nextInt(4)+17; if(r6!=(a+10)&&r6!=(b+10)) set.add(r6); if(set.size()==6) break; } System.out.println(set); }else{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; ResultSet rs=null; int t1=0; int t2=0; int t3=0; int t4=0; int t5=0; int t6=0; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); PreparedStatement stmt=null; String sql="select * from timetable where id = "+String.valueOf(i-10); stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeQuery(); } catch (ClassNotFoundException e) { e.printStackTrace(); } while(rs.next()){ t1=rs.getInt(6); t2=rs.getInt(7); t3=rs.getInt(8); t4=rs.getInt(9); t5=rs.getInt(10); t6=rs.getInt(11); } int n[]=new int[21]; int m[]=new int [21]; int num=0; for(int j=0;j<20;j++){ n[j]=1; } System.out.println(t1); System.out.println(t2); System.out.println(t3); System.out.println(t4); System.out.println(t5); System.out.println(t6); n[a]=0;n[b]=0;n[t1]=0;n[t2]=0;n[t3]=0;n[t4]=0;n[t5]=0;n[t6]=0; for(int j=0;j<20;j++){ if(n[j]!=0){ m[num++]=j; } } int r1=ran.nextInt(2)+0; set.add(m[r1]); int r2=ran.nextInt(2)+2; set.add(m[r2]); int r3=ran.nextInt(2)+4; set.add(m[r3]); int r4=ran.nextInt(2)+6; set.add(m[r4]); int r5=ran.nextInt(2)+8; set.add(m[r5]); int r6=ran.nextInt(num-10)+10; set.add(m[r6]); } return set; } public static void addtimetable(int id,String name,String room,int bd1,int bd2,int time[]){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); String sql = "insert into timetable values(?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) con.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setString(3, room); pstmt.setInt(4, bd1); pstmt.setInt(5, bd2); pstmt.setInt(6, time[0]); pstmt.setInt(7, time[1]); pstmt.setInt(8, time[2]); pstmt.setInt(9, time[3]); pstmt.setInt(10, time[4]); pstmt.setInt(11, time[5]); pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } con.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3,教师界面的设计:
代码:
package 实验室安排系统; import java.awt.*; import java.awt.event.*; import java.sql.SQLException; import javax.swing.*; import javax.swing.JButton; import 实验室安排系统.MainUI; public class TerUI extends JFrame implements ActionListener { //定义组件 JButton jb1,jb2,jb3=null; JPanel jp3,jp4=null; JLabel jlb1,jlb3,jlb4,jlb6=null; public static void main(String[] args) { String tername = null; TerUI ui=new TerUI(tername); } public TerUI(String s) { String tname=s; jp3=new JPanel(); jp3.setBounds(0, 220, 378, 23); getContentPane().setLayout(null); String welcome=tname+"老师好,欢迎使用本系统"; jlb1=new JLabel(welcome); jlb1.setBounds(68, 15, 231, 49); getContentPane().add(jlb1); jb3=new JButton("修改信息"); jb3.setBounds(135, 79, 105, 47); getContentPane().add(jb3); jb3.setForeground(Color.BLUE); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub new terxg(s); } }); jb2=new JButton("\u4FE1\u606F\u5F55\u5165"); jb2.setBounds(15, 79, 105, 47); getContentPane().add(jb2); jb2.setForeground(Color.BLUE); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new TerInputUI(tname); } }); //创建组件 jb1=new JButton("\u67E5\u770B\u8BFE\u8868"); jb1.setBounds(253, 78, 120, 48); getContentPane().add(jb1); jb1.setForeground(Color.BLUE); //jb1.addActionListener(this); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); try { new TerLookUI(tname); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); getContentPane().add(jp3); jlb3=new JLabel("最新公告:"); jlb3.setBounds(68, 180, 90, 21); getContentPane().add(jlb3); jlb3.setForeground(Color.red); jlb4=new JLabel("\u8BF7\u5C3D\u5FEB\u5B8C\u5584\u6559\u5B66\u4FE1\u606F"); jlb4.setBounds(173, 180, 162, 21); getContentPane().add(jlb4); this.setTitle("实验室安排系统"); this.setSize(400,300); this.setLocation(700, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
3.1 教师插入信息设计
教师填写好相关数据后,点击提交将数据上传至数据库,点击返回回到教师界面
代码:
package 实验室安排系统; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JPanel; import java.awt.GridLayout; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class TerInputUI extends JFrame implements ActionListener { //定义组件 JPanel jp1,jp2,jp3,jp4=null; JButton jb1=null,jb2=null; JLabel jlb1,jlb2,jlb3=null; JTextField jtf1,jtf2,jtf3,jtf4=null; public static void main(String[] args) { String s=null; new TerInputUI(s); } public static void addter(String name,String classid,int classnum,int badtime1,int badtime2){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from teacher"; ResultSet rs = statement.executeQuery(sql); int rowcount=0; while(rs.next()){ rowcount=rs.getInt(1); } String sql1 = "insert into teacher values(?,?,?,?,?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) con.prepareStatement(sql1); pstmt.setInt(1, rowcount+1); pstmt.setString(2, name); pstmt.setString(3, classid); pstmt.setInt(4,classnum); pstmt.setInt(5,badtime1); pstmt.setInt(6,badtime2); pstmt.setInt(7,6); pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } rs.close(); con.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public TerInputUI(String s) { //创建组件 jb1=new JButton("提交"); jb1.setBounds(39, 0, 127, 29); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(jtf1.getText().isEmpty()||jtf2.getText().isEmpty()||jtf3.getText().isEmpty()||jtf4.getText().isEmpty()){ JOptionPane.showMessageDialog(null,"无效的输入","提示消息",JOptionPane.WARNING_MESSAGE); }else{ String name=s; String clasid=jtf1.getText(); String cnum=jtf2.getText(); int clsnum=Integer.parseInt(cnum); String b1=jtf3.getText(); String b2=jtf4.getText(); int bdtime1=Integer.parseInt(b1); int bdtime2=Integer.parseInt(b2); addter(name, clasid, clsnum, bdtime1, bdtime2); JOptionPane.showMessageDialog(null,"添加成功!","提示消息",JOptionPane.WARNING_MESSAGE); } }}); jb2=new JButton("返回"); jb2.setBounds(224, 0, 127, 29); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new TerUI(s); } }); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); jlb1=new JLabel("班级ID"); jlb1.setBounds(43, 8, 90, 21); jlb2=new JLabel(" 班级人数"); jlb2.setBounds(37, 8, 95, 21); jlb3=new JLabel("\u4E0D\u60F3\u4E0A\u8BFE\u65F6\u95F4"); jlb3.setBounds(43, 8, 108, 21); jtf1=new JTextField(10); jtf1.setBounds(152, 5, 197, 27); jtf2=new JTextField(10); jtf2.setBounds(155, 5, 195, 27); jtf3=new JTextField(10); jtf3.setBounds(154, 5, 84, 27); jtf4=new JTextField(10); jtf4.setBounds(268, 5, 84, 27); jp1.setLayout(null); //加入到JPanel中 jp1.add(jlb1); jp1.add(jtf1); jp2.setLayout(null); jp2.add(jlb2); jp2.add(jtf2); jp3.setLayout(null); jp3.add(jlb3); //添加标签 jp3.add(jtf3); jp3.add(jtf4); jp4.setLayout(null); jp4.add(jb1); //添加按钮 jp4.add(jb2); //加入JFrame中 getContentPane().add(jp1); getContentPane().add(jp2); getContentPane().add(jp3); getContentPane().add(jp4); getContentPane().setLayout(new GridLayout(4,1)); //选择GridLayout布局管理器 this.setTitle("实验室安排系统"); this.setSize(400,200); this.setLocation(400, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置当关闭窗口时,保证JVM也退出 this.setVisible(true); this.setResizable(true); } public void actionPerformed(ActionEvent e) { //事件判断 } }
3.2教师修改信息设计
教师可以修改自己所教授的班级名称,如图:
代码:
package 实验室安排系统; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; public class terxg extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ public static void main(String[] args) { String str=null; new terxg(str); } /** * Create the frame. */ public terxg(String s) { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u4FEE\u6539\u6240\u6559\u73ED\u7EA7"); lblNewLabel.setBounds(161, 15, 252, 21); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u8BF7\u8F93\u5165\u8981\u4FEE\u6539\u7684\u73ED\u7EA7ID:"); lblNewLabel_1.setBounds(40, 69, 246, 21); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(301, 66, 96, 27); contentPane.add(textField); textField.setColumns(10); JLabel lblNewLabel_2 = new JLabel("\u8BF7\u8F93\u5165\u4FEE\u6539\u540E\u7684\u73ED\u7EA7ID\uFF1A"); lblNewLabel_2.setBounds(40, 133, 234, 21); contentPane.add(lblNewLabel_2); textField_1 = new JTextField(); textField_1.setBounds(301, 130, 96, 27); contentPane.add(textField_1); textField_1.setColumns(10); JButton btnNewButton = new JButton("\u786E\u8BA4\u4FEE\u6539"); btnNewButton.setBounds(40, 186, 163, 29); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String fid=textField.getText(); String sid=textField_1.getText(); try { xg(fid, sid); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } JOptionPane.showMessageDialog(null,"修改成功!","提示消息",JOptionPane.WARNING_MESSAGE); } });; contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u8FD4\u56DE\u4E0A\u4E00\u7EA7"); btnNewButton_1.setBounds(252, 186, 145, 29); btnNewButton_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new TerUI(s); } }); contentPane.add(btnNewButton_1); this.setVisible(true); this.setLocation(500, 300); } public static void xg(String s1,String s2) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rs=0; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); PreparedStatement stmt=null; String sql="update teacher set classid = "+s2+" where classid = "+s1; stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3.3教师查看课表设计
待管理员生成课表后,教师可以查看自己的教学课程表
代码:
package 实验室安排系统; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import javax.swing.JButton; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class TerLookUI extends JFrame { public static void main(String[] args) throws SQLException { String tname=null; new TerLookUI(tname); } public TerLookUI(String s) throws SQLException { String tname=s; int id=0; ResultSet rs=findid(s,"teacher"); while(rs.next()){ id=rs.getInt(1); } //System.out.println(id); String roomid=null; int t1=0,t2=0,t3=0,t4=0,t5=0,t6=0; ResultSet rs1=find(id, "timetable"); while(rs1.next()){ roomid=rs1.getString(3); t1=rs1.getInt(6); t2=rs1.getInt(7); t3=rs1.getInt(8); t4=rs1.getInt(9); t5=rs1.getInt(10); t6=rs1.getInt(11); } //System.out.println(t1); //System.out.println(t2); //System.out.println(t3); intiComponent(s,roomid,t1,t2,t3,t4,t5,t6); } private void intiComponent(String tname,String roomid,int t1,int t2,int t3,int t4,int t5,int t6) { String[] columnNames = { "课节数","星期一", "星期二", "星期三", "星期四", "星期五"}; Object[][] obj=new Object[4][6]; for (int i=0;i<4;i++) { for(int j=0;j<6;j++) { obj[i][j]=""; } } obj[0][0]="第一周上午"; obj[1][0]="第一周下午"; obj[2][0]="第二周上午"; obj[3][0]="第二周下午"; int t[]=new int[6]; t[0]=t1; t[1]=t2; t[2]=t3; t[3]=t4; t[4]=t5; t[5]=t6; for(int i=0;i<6;i++){ switch(t[i]){ case 1: obj[0][1]=roomid+"上课"; break; case 2: obj[1][1]=roomid+"上课"; break; case 3: obj[0][2]=roomid+"上课"; break; case 4: obj[1][2]=roomid+"上课"; break; case 5: obj[0][3]=roomid+"上课"; break; case 6: obj[1][3]=roomid+"上课"; break; case 7: obj[0][4]=roomid+"上课"; break; case 8: obj[1][4]=roomid+"上课"; break; case 9: obj[0][5]=roomid+"上课"; break; case 10: obj[1][5]=roomid+"上课"; break; case 11: obj[2][1]=roomid+"上课"; break; case 12: obj[3][1]=roomid+"上课"; break; case 13: obj[2][2]=roomid+"上课"; break; case 14: obj[3][2]=roomid+"上课"; break; case 15: obj[2][3]=roomid+"上课"; break; case 16: obj[3][3]=roomid+"上课"; break; case 17: obj[3][4]=roomid+"上课"; break; case 18: obj[3][4]=roomid+"上课"; break; case 19: obj[2][5]=roomid+"上课"; break; case 20: obj[3][5]=roomid+"上课"; break; } } JTable table=new JTable(obj, columnNames); TableColumn column=null; int colunms = table.getColumnCount(); for(int i=0;i<colunms;i++) { column = table.getColumnModel().getColumn(i); column.setPreferredWidth(80); } getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setRowHeight(50); JScrollPane scroll = new JScrollPane(table); scroll.setSize(300, 50); getContentPane().add(scroll); this.setLocation(450, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); } public static ResultSet find(int i,String s) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; ResultSet rs=null; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); //if(!con.isClosed()) //System.out.println("Succeeded connecting to the Database!"); PreparedStatement stmt=null; String sql="select * from "+s+" where id = "+String.valueOf(i); stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeQuery(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public static ResultSet findid(String name,String s) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; ResultSet rs=null; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); PreparedStatement stmt=null; String sql="select * from "+s+" where name = '"+name+"'"; stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeQuery(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } }