用數據庫做一個簡單的實驗室安排系統


要求如下

為《算法與數據結構設計》課程開發實驗室安排系統。該課程開課時間為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("");  
    }  

} 
View Code

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) {  
        }  
} 
View Code

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);
            }
            
        }
        
    }  
}  
View Code

 

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();
        }
    }
}
View Code

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();
        }
        
    }

}
 
View Code

 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            
        } 
} 
View Code

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) {            //事件判斷

        
    }  
}  
View Code

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();
        }
    }
}
View Code

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;         
      } 
      




}  
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM