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

📄 student.java

📁 java选课系统管理端
💻 JAVA
字号:
//package myprojects.main;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Student extends JPanel implements ActionListener
{ 
  int select=0;
  String InsertQuery=null,InsertUpdate=null;
  JTextField 学号,姓名,性别,专业,出生,身高,帐号,院系;   
  JTextField 课程号,课程名,分数;                   
  JButton 查询,修改,重置;
  /////////////////////////////////////////////////////
  Connection con=null;
  Statement stmt=null;
  String url = "jdbc:odbc:java"; 
  String username = "sa"; 
  String password = ""; 
  /////////////////////////////////////////////////////
  public Student(int select)
  {  
     this.select=select;
     guiInit();
  }

/*---------------------界面布局开始-------------------------*/
  public void guiInit()
  {

/*---------------------学生管理-------------------------*/
  	if(select==10)//select=10:学生个人信息查询
  	{
  	  
      查询=new JButton("查询");
      查询.addActionListener(this);
      修改=new JButton("修改");
      修改.addActionListener(this);
      重置=new JButton("重置");
      重置.addActionListener(this);

      学号=new JTextField(10);
      姓名=new JTextField(10);
      姓名.setEditable(false);
      性别=new JTextField(10);
      性别.setEditable(true);
      专业=new JTextField(10);
      专业.setEditable(true);
      出生=new JTextField(10);
      出生.setEditable(true);
      身高=new JTextField(10);
      身高.setEditable(true);
      ////////////////////////////////////////
      JLabel label=new JLabel("学生个人基本信息维护",JLabel.CENTER);
      label.setFont(new Font("TimesRoman",Font.BOLD,20));
      label.setForeground(Color.BLUE);
      ////////////////////////////////////////
      Box box0=Box.createHorizontalBox();
      box0.add(label);
      Box box1=Box.createHorizontalBox();              
      box1.add(new JLabel("姓  名 : ",JLabel.CENTER));
      box1.add(姓名);
      Box box2=Box.createHorizontalBox();              
      box2.add(new JLabel("性  别 : ",JLabel.CENTER));
      box2.add(性别);
      Box box3=Box.createHorizontalBox();              
      box3.add(new JLabel("专  业 : ",JLabel.CENTER));
      box3.add(专业);
      Box box4=Box.createHorizontalBox();              
      box4.add(new JLabel("出  生 : ",JLabel.CENTER));
      box4.add(出生);
      Box box5=Box.createHorizontalBox();              
      box5.add(new JLabel("身  高 : ",JLabel.CENTER));
      box5.add(身高);
      Box boxH=Box.createVerticalBox();  
      boxH.add(Box.createVerticalStrut(8)); 
      boxH.add(box0); 
      boxH.add(Box.createVerticalStrut(8));           
      boxH.add(box1);
      boxH.add(Box.createVerticalStrut(8));
      boxH.add(box2);
      boxH.add(Box.createVerticalStrut(8));
      boxH.add(box3);
      boxH.add(Box.createVerticalStrut(8));
      boxH.add(box4);
      boxH.add(Box.createVerticalStrut(8));
      boxH.add(box5);
      boxH.add(Box.createVerticalGlue());          
      JPanel pCenter=new JPanel();
      pCenter.add(boxH);
      setLayout(new BorderLayout());
      add(pCenter,BorderLayout.CENTER);
      JPanel pSouth=new JPanel();
      pSouth.add(查询);
      pSouth.add(修改);
      pSouth.add(重置);
      add(pSouth,BorderLayout.SOUTH);
      validate();
      setVisible(false);
      setBounds(100,200,360,270);
  	}
/*---------------------教师管理-------------------------*/
  	if(select==20)//select=20:教师个人信息查询
  	{

      查询=new JButton("查询");
      查询.addActionListener(this);
      修改=new JButton("修改");
      修改.addActionListener(this);
      重置=new JButton("重置");
      重置.addActionListener(this);
      
  	  帐号=new JTextField(10);
  	  帐号.setEditable(false);
      姓名=new JTextField(10);
      姓名.setEditable(false);
      院系=new JTextField(10);
      院系.setEditable(true);
      ////////////////////////////////////////
      JLabel label=new JLabel("教师个人基本信息维护",JLabel.CENTER);
      label.setFont(new Font("TimesRoman",Font.BOLD,20));
      label.setForeground(Color.BLUE);
      ////////////////////////////////////////
      Box box0=Box.createHorizontalBox();
      box0.add(label);
      Box box1=Box.createHorizontalBox();              
      box1.add(new JLabel("帐  号 : ",JLabel.CENTER));
      box1.add(帐号);
      Box box2=Box.createHorizontalBox();              
      box2.add(new JLabel("姓  名 : ",JLabel.CENTER));
      box2.add(姓名);
      Box box3=Box.createHorizontalBox();              
      box3.add(new JLabel("院  系 : ",JLabel.CENTER));
      box3.add(院系);
      Box boxH=Box.createVerticalBox();  
      boxH.add(Box.createVerticalStrut(8)); 
      boxH.add(box0); 
      boxH.add(Box.createVerticalStrut(8));           
      boxH.add(box1);
      boxH.add(Box.createVerticalStrut(8));
      boxH.add(box2);
      boxH.add(Box.createVerticalStrut(8));
      boxH.add(box3);
      boxH.add(Box.createVerticalGlue());          
      JPanel pCenter=new JPanel();
      pCenter.add(boxH);
      setLayout(new BorderLayout());
      add(pCenter,BorderLayout.CENTER);
      JPanel pSouth=new JPanel();
      pSouth.add(查询);
      pSouth.add(修改);
      pSouth.add(重置);
      add(pSouth,BorderLayout.SOUTH);
      validate();
      setVisible(false);
      setBounds(100,200,360,270);
  	}  	
  }
 public void actionPerformed(ActionEvent e) 
 {
    if(e.getSource()==查询)
    {  
       if(select==10)
         try
         {
           InsertQuery="SELECT * FROM 学生基本信息表 WHERE 学号='"
                        +LoginFrame.userNameTextField.getText()+"'";
           this.InsertQuery=InsertQuery;
           inquest();
         }
         catch(SQLException ee)
         {
            JOptionPane.showMessageDialog ( this ,ee ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );	
         }
       if(select==20)
         try
         {
           InsertQuery="SELECT * FROM 教师表 WHERE 登陆帐号='"
                        +LoginFrame.userNameTextField.getText()+"'";
           this.InsertQuery=InsertQuery;
           inquest();
         }
         catch(SQLException ee)
         {
             JOptionPane.showMessageDialog ( this ,ee ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );	
         }
       if(select==21)
          try
          {  
             if(学号.getText().equals( "" )||课程号.getText().equals( "" ))
   	         {
   	             JOptionPane.showMessageDialog( this,"请填写完整学号和课程号!"  ); 
                 //setTitle( "无记录显示" ); 
                 return; 	
   	         }
   	         String InsertQuery = "SELECT 分数 FROM 成绩表 WHERE 学号 = '"+
   	                        学号.getText()+"' AND 课程号='"+课程号.getText()+"'";
   	         this.InsertQuery=InsertQuery;
             inquest();
          }
          catch(SQLException ee)
          {
              JOptionPane.showMessageDialog ( this ,ee ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );	
          }  
    }
    else  if(e.getSource()==修改)
    {  
       if(select==10)
         try
         {
       	    InsertUpdate = "update 学生基本信息表 set 性别='"+性别.getText()
                        + "',专业='"+专业.getText()+"',生日='"+出生.getText()
                        + "',身高="+身高.getText()
                        + " where 学号 = '"+LoginFrame.userNameTextField.getText()+"'";
          	this.InsertUpdate=InsertUpdate;
            update();
         }
         catch(SQLException ee)
         {
             JOptionPane.showMessageDialog ( this ,ee ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );	
         }
       if(select==20)
         try
         {
       	    InsertUpdate = "UPDATE 教师表 SET 院系='"+院系.getText()
                       + "' WHERE 登陆帐号 = '"+LoginFrame.userNameTextField.getText()+"'";
          	this.InsertUpdate=InsertUpdate;
            update();
         }
         catch(SQLException ee)
         {
             JOptionPane.showMessageDialog ( this ,ee ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );	
         }   
      /*----------------------------------------------------------------------*/
    }
    /*----------------------------------------------------------------------*/
    if(e.getSource()==重置)
    { 
        if(select==10)
        {
          性别.setText(null);
          专业.setText(null);
          出生.setText(null);
          身高.setText(null);         	
        }
        if(select==20)
        {
          帐号.setText(null);
          姓名.setText(null);
          院系.setText(null);
        }
    }
 }
 
 //查询学生个人信息
 private void inquest() throws SQLException
  {   
     // String InsertQuery="SELECT * FROM 学生基本信息表 WHERE 学号='"
                       // +LoginFrame.userNameTextField.getText()+"'";
      try
      {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      }
      catch(ClassNotFoundException cnfex)
      {
           JOptionPane.showMessageDialog ( this ,cnfex ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );	
      }
      con=DriverManager.getConnection(url,username,password); 
      stmt=con.createStatement();
      ResultSet rs=stmt.executeQuery(InsertQuery);
      boolean moreRecords=rs.next();
      if(!moreRecords)
      { 
          String warning="该帐号不存在!";
          JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
          con.close();
          return;
      }
      else
      {
      	 if(select==10)
      	 {
           姓名.setText(rs.getString("姓名"));
           性别.setText(rs.getString("性别"));
           专业.setText(rs.getString("专业"));
           出生.setText(rs.getString("生日")); 
           身高.setText(rs.getString("身高"));
           con.close();
         }
         if(select==20)
      	 {
           帐号.setText(rs.getString("登陆帐号"));
           姓名.setText(rs.getString("教师"));
           院系.setText(rs.getString("院系"));
           con.close();
         }
      }
   }
 
 //修改学生个人信息
 private void update() throws SQLException
 { 
     try 
     { 
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     } 
     //捕获加载驱动程序异常
     catch ( ClassNotFoundException cnfex ) 
     { 
       System.err.println("装载 JDBC/ODBC 驱动程序失败。" ); 
      //cnfex.printStackTrace(); 
       JOptionPane.showMessageDialog ( this ,cnfex ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE );
       System.exit( 1 );  // terminate program 
     } 
     try
     {
     	con=DriverManager.getConnection(url,username,password);  
        stmt = con.createStatement();
        int insert = stmt.executeUpdate( InsertUpdate );//executeUpdate返回一个整型值
        if (insert >=1)
        {
          JOptionPane.showMessageDialog( this,"信息修改成功!" );
        } 
        con.close();
     }
     catch( SQLException sqlex ) 
     { 
        JOptionPane.showMessageDialog ( this ,sqlex ,
          "学生选课管理系统",JOptionPane.WARNING_MESSAGE ); 
     }
   }
}

⌨️ 快捷键说明

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