⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aboutcourse.java

📁 使用java编写的一个学生选课系统
💻 JAVA
字号:
package Srs;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;


import Srs.LinkDatabase;
import Srs.AfterLoad;

public class AboutCourse extends JFrame implements ActionListener{

	private JTable table_2;
	private JTextArea textArea_1;
	private JTextArea textArea;
	private JTable table_1;
	private JTable table;
	private JButton button;
	private JButton button_1;
	private String Id_0;
	private String Name_0;
	/**
	 * Launch the application
	 * @param args
	 */
	

	/**
	 * Create the frame
	 */
	public AboutCourse(String Id,String Name) {
		super();
		Id_0 = Id;
		Name_0 = Name;
		getContentPane().setLayout(null);
		setBounds(100, 100, 500, 375);
		this.setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		

		final JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(67, 198, 351, 101);
		getContentPane().add(scrollPane_1);
		
		
		
		
		String[] col = {"选课","课程编号", "课程名称", "课程说明" ,"授课教师","教师信息","上课时间",
        		"上课地点","人数上限","已选人数"};
        DefaultTableModel model = new DefaultTableModel(col,0);
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
           } catch (ClassNotFoundException e1) {
        	System.out.println("加载驱动不成功");
            e1.printStackTrace();
           }
           try{

        	   Connection conn = DriverManager.getConnection(
        		"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=mydb.mdb");
               Statement stmt = conn.createStatement();
               ResultSet rs=stmt.executeQuery("select * from Seletedcourse");
           
              while(rs.next()){
              
              String id = rs.getString("课程编号");
              String name = rs.getString("课程名称");
              String tel = rs.getString("课程说明");
              String teacher = rs.getString("授课教师");
              String teachernews = rs.getString("教师信息");
              String time = rs.getString("上课时间");
              String place = rs.getString("上课地点");
              String upper = rs.getString("人数上限");
              String selected = rs.getString("已选人数");
              JCheckBox ch = new JCheckBox();
              Object[] rowData={ch,id,name,tel,teacher,teachernews,time,place,
            		  upper,selected};
              model.addRow(rowData);//添加在表模板中
           }
           }catch(SQLException e1){
        	   System.out.println("连接数据库未成功");
           e1.getStackTrace();
           }
        table_1 = new JTable(model);  
        TableColumn column = table_1.getColumnModel().getColumn(0); 
        JCheckBox ch = new   JCheckBox();
        column.setCellEditor(new   DefaultCellEditor(ch)); 
        column.setCellRenderer(new   CheckBoxRenderer()); 
        table_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  
        scrollPane_1.setViewportView(table_1);

		final JLabel label = new JLabel();
		label.setFont(new Font("幼圆", Font.BOLD, 15));
		label.setText("必修课:");
		label.setBounds(28, 45, 66, 18);
		getContentPane().add(label);

		final JLabel label_1 = new JLabel();
		label_1.setFont(new Font("幼圆", Font.BOLD, 15));
		label_1.setText("选修课:");
		label_1.setBounds(28, 174, 66, 18);
		getContentPane().add(label_1);

		final JLabel label_2 = new JLabel();
		label_2.setFont(new Font("新宋体", Font.BOLD, 18));
		
		label_2.setText("学号:");
		label_2.setBounds(75, 21, 66, 18);
		getContentPane().add(label_2);

		final JLabel label_3 = new JLabel();
		label_3.setFont(new Font("@新宋体", Font.BOLD, 18));
		label_3.setText("姓名:");
		label_3.setBounds(250, 22, 66, 18);
		getContentPane().add(label_3);

		textArea = new JTextArea();
		textArea.setText(Id);
		textArea.setEditable(false);
		textArea.setBounds(130, 21, 114, 18);
		getContentPane().add(textArea);

		textArea_1 = new JTextArea();
		textArea_1.setText(Name);
		textArea_1.setEditable(false);
		textArea_1.setBounds(307, 21, 111, 18);
		getContentPane().add(textArea_1);
		
		
		
		

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(67, 67, 351, 101);
		getContentPane().add(scrollPane);
		
		String[] col_1 = {"课程编号", "课程名称", "课程说明" ,"授课教师","教师信息","上课时间",
        		"上课地点","课程学分"};
        DefaultTableModel model_1 = new DefaultTableModel(col_1,0);
		
		try{

     	   Connection conn_1 = DriverManager.getConnection(
     		"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=mydb.mdb");
            Statement stmt_1 = conn_1.createStatement();
            ResultSet rs_1=stmt_1.executeQuery("select * from RequiredCourse");
        
           while(rs_1.next()){
           
           String id_1 = rs_1.getString("课程编号");
           String name_1 = rs_1.getString("课程名称");
           String tel_1 = rs_1.getString("课程说明");
           String teacher_1 = rs_1.getString("授课教师");
           String teachernews_1 = rs_1.getString("教师信息");
           String time_1 = rs_1.getString("上课时间");
           String place_1 = rs_1.getString("上课地点");
           String upper_1 = rs_1.getString("课程学分");
           Object[] rowData={id_1,name_1,tel_1,teacher_1,teachernews_1,time_1,place_1,
         		  upper_1};
           model_1.addRow(rowData);//添加在表模板中
        }
        }catch(SQLException e2){
     	   System.out.println("连接数据库未成功");
        e2.getStackTrace();
        }

		table_2 = new JTable(model_1);
		table_2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  
		scrollPane.setViewportView(table_2);

		button = new JButton();
		button.setText("确认选择");
		button.setBounds(130, 305, 86, 28);
		getContentPane().add(button);
		button.addActionListener(this);

		button_1 = new JButton();
		button_1.setText("返   回 ");
		button_1.addActionListener(this);
		button_1.setBounds(245, 305, 86, 28);
		getContentPane().add(button_1);
		//
	}
	public String quotate(String content){
        return "'"+content+"'";
    }
	
	
	
public void actionPerformed(ActionEvent e){
		
		if(e.getSource() == button){
			TableModel model_1 = table_1.getModel();
			int i;
			int j;
			int row = model_1.getRowCount();
			int column = model_1.getColumnCount();
			for(i=0;i<row;i++){
				if(model_1.getValueAt(i, 0).toString().equals("true")){
					String upper = model_1.getValueAt(i, 8).toString();
					String selected_0 = model_1.getValueAt(i, 9).toString();
					int m = Integer.parseInt(upper);
					int n = Integer.parseInt(selected_0);
					if(m == n){
						JOptionPane newone = new JOptionPane();
						newone.showMessageDialog(null, "该课程已达到人数上限");
						break;
					}
					else{
					String[] st = new String[8];
					for(j=0;j<8;j++)
						st[j] = model_1.getValueAt(i,j).toString();
					LinkDatabase linkdatabase = new LinkDatabase();
					Connection con = linkdatabase.getlink();
					try{
					Statement statement = con.createStatement();
					String sql = "insert into "+Id_0+" values "+
					"("+quotate(st[1])+","+quotate(st[2])+","
		    		+quotate(st[3])+","+quotate(st[4])+
		    		","+quotate(st[5])+","+quotate(st[6])+","+
		    		quotate(st[7])+")"; 
					statement.execute(sql);
					int p = Integer.valueOf(selected_0);
					p++;
					model_1.setValueAt(String.valueOf(p),i, 9);
					String newone_0 = String.valueOf(p);
					String num = model_1.getValueAt(i, 1).toString();
					String sql_1 = "update Seletedcourse set 已选人数 =" +
					quotate(newone_0)+" where 课程编号 = "+quotate(num);
					statement.execute(sql_1);
					JOptionPane newone = new JOptionPane();
					newone.showMessageDialog(null, "选课成功");
					}catch(SQLException e1){
						JOptionPane newone = new JOptionPane();
						newone.showMessageDialog(null, "该课程已选");
					}
					
					
					}
				
				}
			}
			
		    
		}
		else if(e.getSource() == button_1){
			AfterLoad after = new AfterLoad("360601");
		    after.setVisible(true);
		    this.setVisible(false);
		}
		
	
			
	}
	
	
	
	
	class   CheckBoxRenderer   implements   TableCellRenderer{ 
        public   JCheckBox   chk; 
        public   CheckBoxRenderer(){ 
        this.chk   =   new   JCheckBox(); 
        } 
        public   Component   getTableCellRendererComponent(JTable   arg0,   Object   value,   boolean   isSelected,   boolean   arg3,   int   arg4,   int   arg5)   { 
        this.chk.setSelected(Boolean.TRUE   ==   value); 
        this.chk.setEnabled(isSelected); 
        this.chk.setHorizontalAlignment(JCheckBox.CENTER); 
        return   this.chk; 
        } 
    }
	 class CheckBoxListener implements ActionListener {
	        public void actionPerformed(ActionEvent e) {
	            JCheckBox cb = (JCheckBox)e.getSource();
	            if(cb.isSelected()){
	                System.out.println("选择到了");
	            }
	        }
	 }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -