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

📄 user_login.java

📁 课程设计-学生成绩管理系统
💻 JAVA
字号:
import java.awt.*;
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 java.util.Scanner;
import java.sql.PreparedStatement;
import javax.swing.*;
public class User_login extends JFrame implements ActionListener
{
	    static String mingzi;
	    static String pass;
	    static String table_name;
	    static String mima_name;
	    static String ming_name;
		private static final int DEFAULT_WIDTH = 400;
	    private static final int DEFAULT_HEIFHT = 300;    
	    ResultSet rs;
		Connection con;
		PreparedStatement ps;	    
	    String name,pwd,user;
	    GridBagLayout gbl=new GridBagLayout();
	    GridBagConstraints gbc=new GridBagConstraints();
	    JButton queding;
	    JButton quxiao;
	    JTextField xingming=new JTextField(10);
	    JPasswordField mima=new JPasswordField(10);
	    JLabel biaoti=new JLabel("----登陆窗口----");
	    JLabel j_name=new JLabel("用户名:");
	    JLabel j_pwd=new JLabel("密 码:");
	   
	    	 	   
	    ButtonGroup group=new ButtonGroup();
	    JRadioButton guanliyuan=new JRadioButton("管理员",true);
	    JRadioButton jiaoshi=new JRadioButton("教  师");
	    JRadioButton xuesheng=new JRadioButton("学  生");
	     public  User_login()
	    {
	    	
	    	super.setSize(DEFAULT_WIDTH,DEFAULT_HEIFHT);
	    	super.setTitle("欢迎光临学生成绩管理系统!");
	    	//super.add(panel);
	    	setLayout(gbl);
	    	mima.setEchoChar('*');  //设置密码的格式.
	    	queding=new JButton("登陆");
	    	queding.addActionListener(this);
	    	quxiao=new JButton("取消");
	    	quxiao.addActionListener(this);
	    	 
	    	group.add(guanliyuan);
	    	group.add(jiaoshi);
	    	group.add(xuesheng);
	    	set(1,0,2,1,biaoti);
	    	set(0,1,1,1,guanliyuan);
	    	set(0,2,1,1,jiaoshi);
	    	set(0,3,1,1,xuesheng);
	    	set(1,1,1,1,j_name);
	    	set(1,2,1,1,j_pwd);
	    	set(2,1,2,1,xingming);
	    	set(2,2,2,1,mima);
	    	set(2,4,1,1,queding);
	    	set(3,4,1,1,quxiao);
	    	super.setLocation(300,80); //设置框架出现在位置.
	    	//panel.setBackground(Color.PINK);
	   
	    }
	     public void set(int x,int y,int w,int h,Component com)
	     {
	    	 gbc.anchor=GridBagConstraints.CENTER;
	    	 gbc.gridx=x;
	    	 gbc.gridy=y;
	    	 gbc.gridwidth=w;
	    	 gbc.gridheight=h;
	    	 gbl.setConstraints(com, gbc);
	    	 add(com);
	     }
	     String get_user()      //  用来接收接单选按钮的值.
	     {
	    	 String user;
	    	 if(guanliyuan.isSelected())
	    	 {
	    		 user="guanliyuan";
	    		 table_name="administrator";
	    		 mima_name="admin_password";
	    		 ming_name="admin_no";
	    	 }
	    	 else if(jiaoshi.isSelected())
	    	 {
	    		 user="jiaoshi";
	    		 table_name="teacher";
	    		 mima_name="teacher_password";
	    		 ming_name="teacher_no";
	    	 }
	    	 else
	    	 {
	    		    user="xuesheng";
	    		    table_name="student";
		    		 mima_name="student_password";
		    		 ming_name="student_no";
	    	 }
	    	 return user;
	     }
	     public void actionPerformed(ActionEvent e)
	     {
	    	 String sql_pwd;
	    	 Object obj=e.getSource(); 
	    	     name=new String(xingming.getText()).trim();
	    		 pwd=new String(mima.getPassword()).trim();
	    		 user=get_user();
	    		 mingzi=name;      //////////////
	    		 pass=pwd;         ///////////////
	    	 if(obj==queding)
	    	 {
	    		
	    		 if(name.length()==0)
	    		 {
	    			 JOptionPane.showMessageDialog((Component)null,"请输入您的用户名!","提示信息",JOptionPane.ERROR_MESSAGE);
	    			 xingming.requestFocus();
	    		 }
	    		 else if(pwd.length()==0)
	    		 {
	    			 JOptionPane.showMessageDialog((Component)null,"请输入您的密码!","提示信息",JOptionPane.ERROR_MESSAGE);
	    			 mima.requestFocus();
	    		 }
	    		 else
	    		 {
	    			 try
	    			 {
	    				 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    				 con=DriverManager.getConnection("jdbc:odbc:StudentScore","sa","");
	    				 if(user=="guanliyuan")
	    				 { 
	    					 ps=con.prepareStatement("select * from administrator where admin_no=?");
	    					 ps.setString(1,name);
	    					 rs=ps.executeQuery(); 
	    					 rs.next();
	    					 sql_pwd=rs.getString(3).trim();
	    					 if(!rs.next())
	    					 {
	    						 if(pwd.equals(sql_pwd))
	    						 {
	    							 System.out.println("guanliyuan...");
	    							 JOptionPane.showMessageDialog((Component)null,"登陆成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
	    							 this.dispose();
	    							 Admin_manage manage1 = new Admin_manage();
	    							 manage1.setVisible(true);
	    						 }
	    						 else
	    						 {
	    							 JOptionPane.showMessageDialog((Component)null,"密码错误!","提示信息",JOptionPane.ERROR_MESSAGE);
	    							 mima.requestFocus();
	    						 }
	    					 }
	    				 }
	    				 if(user=="jiaoshi")
	    				 {
	    					 ps=con.prepareStatement("select * from teacher where teacher_no=?");
	    					 ps.setString(1,name);
	    					 rs=ps.executeQuery();
	    					 rs.next();
	    					 sql_pwd=rs.getString(3).trim();
	    					 if(!rs.next())
	    					 {
	    						 if(pwd.equals(sql_pwd))
	    						 {
	    							 System.out.println("jiaoshi...");
	    							 JOptionPane.showMessageDialog((Component)null,"登陆成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
	    							 this.dispose();
	    						     Teacher_manage manage2=new Teacher_manage();
	    						     manage2.setVisible(true);
	    						 }
	    						 else
	    						 {
	    							 JOptionPane.showMessageDialog((Component)null,"密码错误!","提示信息",JOptionPane.ERROR_MESSAGE);
	    							 mima.requestFocus();
	    						 }
	    					 }
	    				 }
	    				 if(user=="xuesheng")
	    				 {
	    					 ps=con.prepareStatement("select * from student where student_no=?");
	    					 ps.setString(1,name);
	    					 rs=ps.executeQuery();
	    					 rs.next();
	    					 sql_pwd=rs.getString(3).trim();
	    					 if(!rs.next())
	    					 {
	    						 if(pwd.equals(sql_pwd))
	    						 { 
	    							 JOptionPane.showMessageDialog((Component)null,"登陆成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
	    						     this.setVisible(false);
	    						     Student_manage manage=new Student_manage();
	    						     manage.setVisible(true);
	    							 System.out.println("xuesheng...");
	    						 }
	    						 else
	    						 {
	    							 JOptionPane.showMessageDialog((Component)null,"密码错误!","提示信息",JOptionPane.ERROR_MESSAGE);
	    							 mima.requestFocus();
	    						 }
	    					 }
	    				 }
	    			 }
	    			 catch(Exception  ee)
	    			 {
	    				 JOptionPane.showMessageDialog((Component)null,"用户名不存在!","提示信息",JOptionPane.ERROR_MESSAGE);
	    				 xingming.requestFocus();
	    			 }
	    		 }
	    	 }
	    	 if(obj==quxiao)
	    	 {
	    		 System.exit(-1);
	    	 }
	     }
	public static void main(String[] args)
	{
		User_login login = new User_login();
		login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		login.setVisible(true);
		
	}
}

⌨️ 快捷键说明

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