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

📄 loginframe.java

📁 利用JAVA的界面对MS SQL进行操作,实现了学生管理系统基本功能:登陆,录入,查询,删除,修改,内置程序详细讲解.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        		Statist.setTabelDisplay(true);
        	}
        	
        	else if(cChange==3){
        		Statist.setTabelDisplay(false);  
            	Statist.clear();
        		Statist.jishu=psql.SearchStudentDetaiByAge(Integer.parseInt(jf_id.getText()),Statist.a); 
        		Statist.showNum.setText("人数为:"+Statist.jishu); 	
        		Statist.setTabelDisplay(true);
        	}

     	}      	
   	 	
     	}
     	
     void change(int i){
     	if(i==1){
     		du_info.setText("输入学号");
            cChange=1;
     	}
     	
     	else if(i==2){
     		du_info.setText("输入姓名");
            cChange=2;
     	}
     	
    	else if(i==3){
     		du_info.setText("输入年龄");
            cChange=3;
     	}
     	
 
     }	
     	
     void setValue(String id,String name,String sex,String Class,int age,String addtress,String phoneNum)
    {
    	cId=id;cName=name;cSex=sex;cClass=Class;cAge=age;cAddtress=addtress;cphoneNum=phoneNum;
    }
    
    void setDisplay(Boolean tf){
   	jdl.setVisible(tf);
   }
}


//显示信息
class Statist extends JDialog implements ActionListener
 { 
   public static JTable table;
   	Object jname[]={"序号","学号","姓名","性别","班级","年龄","家庭住址","电话","用户密码"};
    public static Object [][] a= new Object[200][9];
    public static int jishu;
    
    public static JLabel showNum=new JLabel("人数为:"+jishu);
    
    JMenuBar Dmenubar;
    JMenu fileMenu,editMenu;
    JMenuItem Ditem_exit,item_save,item_open;
    JMenuItem item_add,item_find,item_update,item_distroy,item_compositor,item_refresh,item_showClasses;
    FileDialog filedialog_save,filedialog_load;//声明2个文件对话框
    
    JPanel jpl=new JPanel();
    JButton jAdd=new JButton("添加");
    JButton jPaixu=new JButton("排序");
    JButton jUpdate=new JButton("修改");
    JButton jFind=new JButton("查找");
    JButton jDelete=new JButton("删除");
    JButton jRefresh=new JButton("刷新");
    JButton jShowclass=new JButton("班级");
    
    register rgs;
    framForCompositor ffc;//排序界面
    framForSearch ffs;//查找界面
    framForClasses ffclasses;//选择班级界面
    framForDelete ffd;//删除界面
    framForUpdate ffu;//修改界面
    proceeSQL psql;
    
   Statist(JFrame f,String s,boolean b)
   {
   	
   
   	super(f,s,b);
    rgs=new register(f,"添加学生信息",b);
   	ffc = new framForCompositor(f,"排序",b);
   	ffs = new framForSearch(f,"查找",b);
   	ffclasses = new framForClasses(f,"查看班级信息",b);
   	ffd =new framForDelete(f,"输入要删除的学号",b);
  	ffu =new framForUpdate(f,"输入要修改的学号",b);
   	psql = new proceeSQL();
   	
    setSize(800,450);setVisible(false);
    //生成200X4的表格
    table=new JTable(a,jname);
     //生成菜单条,菜单项,菜单(包括FRAME和DIALOG) 
     Dmenubar=new JMenuBar();
     fileMenu=new JMenu("文件");
     editMenu=new JMenu("编辑");
     Ditem_exit=new JMenuItem("退出");
     item_save=new JMenuItem("保存");
     item_open=new JMenuItem("打开");
     item_add=new JMenuItem("添加");
     item_find=new JMenuItem("查找学生信息");
     item_update=new JMenuItem("修改学生信息");
     item_distroy=new JMenuItem("删除学生信息");
     item_compositor=new JMenuItem("排序");
     item_refresh=new JMenuItem("刷新");
     item_showClasses=new JMenuItem("显示班级信息");
     
     fileMenu.add(item_open);
     fileMenu.add(item_save);
     fileMenu.addSeparator();
     fileMenu.add(Ditem_exit);
     editMenu.add(item_add);
     editMenu.addSeparator();
     editMenu.add(item_find);
     editMenu.add(item_update);
     editMenu.add(item_distroy);
     editMenu.add(item_compositor);
     editMenu.add(item_refresh);
     editMenu.addSeparator();
     editMenu.add(item_showClasses);
 
    jpl=new JPanel(); 
    jpl.add(jAdd); 
    jpl.add(jPaixu);
    jpl.add(jUpdate);
    jpl.add(jFind);
    jpl.add(jDelete);
    jpl.add(jRefresh);
    jpl.add(jShowclass);
    jpl.add(showNum);
    jpl.setLayout(new GridLayout(9,1,2,9));
    

     
     
     Dmenubar.add(fileMenu);
     Dmenubar.add(editMenu);
     super.setJMenuBar(Dmenubar);
     super.getContentPane().add(new JScrollPane(table));
     super.add(jpl,BorderLayout.WEST);
     
     Ditem_exit.addActionListener(this);
     item_add.addActionListener(this);
     item_find.addActionListener(this);
     item_update.addActionListener(this);
     item_distroy.addActionListener(this);
     item_compositor.addActionListener(this);
     item_refresh.addActionListener(this);
     item_showClasses.addActionListener(this);
     
     jAdd.addActionListener(this);
     jPaixu.addActionListener(this);
     jUpdate.addActionListener(this);
     jFind.addActionListener(this);
     jDelete.addActionListener(this);
     jRefresh.addActionListener(this);
     jShowclass.addActionListener(this);
    
     
     super.addWindowListener(new WindowAdapter()
      {public void windowClosing(WindowEvent e)
       {
          setVisible(false);
       }});

   }
   
   void setDisplay(Boolean tf){
   	super.setVisible(tf);
   }
   
   public static void setTabelDisplay(Boolean tf){
   	    table.setVisible(tf);
   }
   
   public void reFresh(){
   	
   	jishu=psql.SearchStudentDetail(a);
   	showNum.setText("人数为:"+jishu);
   
   }
   
   public static void clear(){
     for(int i=0;i<200;i++)
       for(int j=0;j<9;j++){
       	a[i][j]=null;
       }
      
   }
   
   public void actionPerformed(ActionEvent e){
   	if(e.getSource()==Ditem_exit){
   		this.setVisible(false);
   	}
   	
   	else if(e.getSource()==item_add||e.getSource()==jAdd){
   		rgs.btnChange("添加");
   		rgs.setDisplay(true);
   	}
   	
   	else if(e.getSource()==item_find||e.getSource()==jFind){
   		ffs.setDisplay(true);
   	}
   	
   	else if(e.getSource()==item_update||e.getSource()==jUpdate){
   		ffu.setDisplay(true);
   		
   	}
   	
   	else if(e.getSource()==item_distroy||e.getSource()==jDelete){
   		ffd.setDisplay(true);
   	}
   	
   	else if(e.getSource()==item_compositor||e.getSource()==jPaixu){
   		ffc.setDisplay(true);
   	}
   	
   	else if(e.getSource()==item_refresh||e.getSource()==jRefresh){
   		Statist.setTabelDisplay(false);   
   		this.reFresh();
   		Statist.setTabelDisplay(true);
   		
   	}
   	else if(e.getSource()==item_showClasses||e.getSource()==jShowclass){
   		ffclasses.setDisplay(true);
   	}
   	
   	}
}




//处理数据库相关功能
class proceeSQL{
	 public static String url = "jdbc:odbc:stu";
     public static  Connection con;
     public static  String sql;
     public static  Statement stmt;
       String name="",id="",sex="",classes="",homeAddress="",telNum="",password="";
       int age,state;

       
     //打开数据库  
	public static void openSQL(){
		
	try{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       } catch(java.lang.ClassNotFoundException e1) {
           System.err.print("ClassNotFoundException: ");
           System.err.println(e1.getMessage());
       }
                                      
     try{
       	  con = DriverManager.getConnection(url, "sa", "yang");
          stmt = con.createStatement();
       	} catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }   
	}
	//关闭数据库
	void closeSQL(){
	 try{
		    	//关闭连接。
        	stmt.close();
        	con.close();
     	} catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }  
	}
	
	//给类内变量赋值
	void setValue(String id,String name,String sex,String classes,int age,String address,String telNum,String password){
		this.id=id;
		this.name=name;
		this.sex=sex;
		this.age=age;
		this.classes=classes;
		this.homeAddress=address;
		this.telNum=telNum;
		this.password=password;
	}
	
	//给类外变量赋值
	void OUTsetValue(Object a[]){//String Oid,String Oname,String Osex,String Oclasses,int Oage,String Oaddress,String OtelNum,String Opassword){
	
		try{

		sql = " SELECT sname,sex,age,class,address,tel,password FROM STUDENT "+" where sno=" +"'"+a[0]+"'";                 
        	ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){    
        	      a[1] = rs.getString("sname");
        	      a[2] = rs.getString("sex");
        	      a[4] = rs.getInt("age");
        	      a[3] = rs.getString("class");
        	      a[5] = rs.getString("address");
        	      a[6] = rs.getString("tel");
        	      a[7] = rs.getString("password");
        	  
        }
    


	         }catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
	}
	
	//插入学生信息
	public int insertStudentDetail(){	
	 try {	
			sql = "INSERT INTO STUDENT " +
                            "VALUES(?,?,?,?,?,?,?,?)";
            PreparedStatement stmt0=con.prepareStatement(sql);
             stmt0.setString(1,id);
             stmt0.setString(2,name);
             stmt0.setString(3,sex);
             stmt0.setString(4,classes);
             stmt0.setInt(5,age); 
             stmt0.setString(6,homeAddress);
             stmt0.setString(7,telNum);
             stmt0.setString(8,password);
                      
             
        	stmt0.executeUpdate();
        	con.commit();

       	} catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
            	JOptionPane.showMessageDialog(null, "对象已经存在");
            	 return -1; 
        } 
        return 1;
            		
	}
	
	
	//插入密码表
	void insertIDTABLEofCustomer()
	{
		try{
		
		     sql = "INSERT INTO IDTABLE " +
            "VALUES("+"'"+id+"','"+password+"',1)";

             stmt.executeUpdate(sql);
        	con.commit();
        	      
       	} catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }   
	}
	
	//按姓名查找
	int SearchStudentDetaiByName(String Sname,Object a[][]){
		int i=0;
		try{

		sql = " SELECT sno,sname,sex,age,class,address,tel,password FROM STUDENT "+" where sname=" +"'"+Sname+"'";                 
        	ResultSet rs = stmt.executeQuery(sql);
        	
        	
            while(rs.next()){
            	 a[i][0] = i+1;
        	     a[i][1] = rs.getString("sno");
        	     a[i][2] = rs.getString("sname");
        	     a[i][3] = rs.getString("sex");
        	     a[i][5] = rs.getInt("age");
        	     a[i][4] = rs.getString("class");
        	     a[i][6] = rs.getString("address");
        	     a[i][7] = rs.getString("tel");
        	     a[i][8] = rs.getString("password");
	          i++; 
	           }
	         }catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
        return i;
	}
	
   //按学号查找
   int SearchStudentDetaiById(String Sid,Object a[][]){
		int i=0;
			try{

		sql = " SELECT sno,sname,sex,age,class,address,tel,password FROM STUDENT "+" where sno=" +"'"+Sid+"'";                 
        	ResultSet rs = stmt.executeQuery(sql);
        	
        
            while(rs.next()){
            	 a[i][0] = i+1;
        	     a[i][1] = rs.getString("sno");
        	     a[i][2] = rs.getString("sname");
        	     a[i][3] = rs.getString("sex");
        	     a[i][5] = rs.getInt("age");
        	     a[i][4] = rs.getString("class");
        	     a[i][6] = rs.getString("address");
        	     a[i][7] = rs.getString("tel");
        	     a[i][8] = rs.getString("password");
	          i++; 
	           }
	         }catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        } 
        
        return i;
            	
	}
	
	
	//按年龄查找
   int SearchStudentDetaiByAge(int sage,Object a[][]){
		int i=0,state;
		try{

		sql = " SELECT sno,sname,sex,age,class,address,tel,password FROM STUDENT "+" where age=" + sage;                 
        	ResultSet rs = stmt.executeQuery(sql);
        	
        	
            while(rs.next()){
            	 a[i][0] = i+1;
        	     a[i][1] = rs.getString("sno");
        	     a[i][2] = rs.getString("sname");
        	     a[i][3] = rs.getString("sex");
        	     a[i][5] = rs.getInt("age");
        	     a[i][4] = rs.getString("class");
        	     a[i][6] = rs.getString("address");
        	     a[i][7] = rs.getString("tel");
        	     a[i][8] = rs.getString("password");
	          i++; 
	           }
	         }catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }   
         return i;  	
	}
	
  //按班级查找
   int SearchStudentDetaiByClasses(String Sclasses,Object a[][]){
		int i=0;
		try{

		sql = "

⌨️ 快捷键说明

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