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

📄 school.java

📁 用JAVA+SQL编写的一个教学管理软件
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class School extends JFrame{
	private JLabel label1,lable2;
	private JTextField txt_id;
	private JPasswordField txt_pwd;
	private JButton btn_ok,btn_exit;
	private JPanel panel,subpanel1,subpanel2;
	private JOptionPane message;
	private JRadioButton btn_status_stu,btn_status_tech,btn_status_admi;
	private ButtonGroup  statusgroup;
	private int sta;
	private String sql;
		
	public School(){
		super("欢迎登入教学管理系统");
		label1=new JLabel("用户名");
		lable2=new JLabel("密码");
		txt_id=new JTextField();
		txt_pwd=new JPasswordField();
		btn_ok=new JButton("登入");
		btn_exit=new JButton("退出");
		panel=new JPanel();
		message=new JOptionPane();
		btn_status_stu=new JRadioButton("学生",true);
		sta=1;
		btn_status_tech=new JRadioButton("教师");
		btn_status_admi=new JRadioButton("管理员");
		statusgroup=new ButtonGroup();
		
		subpanel1=new JPanel();
		subpanel1.setLayout(new GridLayout(1,3,0,10));
		subpanel1.add(btn_status_stu);
		subpanel1.add(btn_status_tech);
		subpanel1.add(btn_status_admi);
		statusgroup.add(btn_status_stu);
		statusgroup.add(btn_status_tech);
		statusgroup.add(btn_status_admi);
		subpanel1.setBorder(BorderFactory.createEmptyBorder(20,30,0,30));
		subpanel2=new JPanel();
		subpanel2.setLayout(new GridLayout(3,2,10,10));
		subpanel2.add(label1);
		subpanel2.add(txt_id);
		subpanel2.add(lable2);
		subpanel2.add(txt_pwd);
		subpanel2.add(btn_ok);
		subpanel2.add(btn_exit);
		subpanel2.setBorder(BorderFactory.createEmptyBorder(0,60,30,60));
		panel.setLayout(new GridLayout(2,1));
		panel.add(subpanel1);
		panel.add(subpanel2);
		getContentPane().add(panel);
		//窗口监听
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				dispose();
				System.exit(0);
			}
		});	
		setLocation(400,250);
		setResizable(false);
		pack();
		setVisible(true);
		
		btn_exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dispose();
				System.exit(0);
			}	
		});
		
		txt_id.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				btn_ok.doClick();
			}
		});
		
		txt_pwd.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				btn_ok.doClick();
			}
		});
		
		btn_status_stu.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e){
				if(btn_status_stu.isSelected())
				sta=1;
			}
		});
		
		btn_status_tech.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e){
				if(btn_status_tech.isSelected())
				sta=2;
			}
		});
		
		btn_status_admi.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e){
				if(btn_status_admi.isSelected())
				sta=3;
			}
		});
		
		
		
		btn_ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				try{
					if(txt_id.getText().equals("")||txt_pwd.getText().equals("")){
						JOptionPane.showMessageDialog(txt_id,"用户名或密码不能为空","警告",1);
					}
					else{
				       Connection conn=null;
					   DateSource datesor=new DateSource();
					   conn=datesor.getconnection();
					   if(sta==1){
						sql="select * from students where id=? and password=?";
					}
				  	else if(sta==2){
						sql="select * from teacher where id=? and password=?";
					}
					else if(sta==3){
						sql="select * from admi where id=? and password=?";
					}
				
					PreparedStatement stmt=conn.prepareStatement(sql);
					stmt.setInt(1,Integer.parseInt(txt_id.getText()));
					stmt.setString(2,txt_pwd.getText());
					ResultSet rs=stmt.executeQuery();
					if(rs.next()){
							if(sta==1){
								dispose();
								Cmenu user1=new Cmenu("同学",rs.getString(3),Integer.parseInt(txt_id.getText()));
								rs.close();
								conn.close();
							}
							else if(sta==2){
								dispose();
								Cmenu user2=new Cmenu("老师",rs.getString(3),Integer.parseInt(txt_id.getText()));
						    	rs.close();
								conn.close();
								}
							else if(sta==3){
								dispose();
								
								Cmenu user3=new Cmenu("管理员",rs.getString(3),Integer.parseInt(txt_id.getText()));
						    	rs.close();
								conn.close();
								}
					  }	
					  else{
					  	rs.close();
					  conn.close();
					  JOptionPane.showMessageDialog(txt_id,"用户不存在或密码错误","警告",1);
					  }
					  		
				}
			}catch(NumberFormatException NFE){
					JOptionPane.showMessageDialog(txt_id,"请正确输入用户名","警告",1);
			}
			catch(Exception exc){
				System.out.println(exc.getClass());
			}
							
		}
					
			
			
		});

	}
	

	
}

⌨️ 快捷键说明

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