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

📄 program.java

📁 这是一个java的学生管理系统
💻 JAVA
字号:
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;
public class program
{
  public static void main(String args[])
  {
	  new myFrame();
	  new access();
   }
}
class myFrame extends Frame implements ActionListener
{  TextArea ta;
   Button input,output,sort,search,count,delete,exit;
   myFrame()
   {
	   super("学生成绩管理系统 ");
	   input=new Button("输入记录");
	   output=new Button("输出记录");
	   sort=new Button("排序输出");
	   search=new Button("查找记录");
	   count=new Button("统计人数");
	   delete=new Button("删除记录");
	   exit=new Button("退出系统");
	   ta=new TextArea (300,250);
	   input.setBackground(Color.cyan);
       output.setBackground(Color.blue);
       sort.setBackground(Color.yellow);
       search.setBackground(Color.pink);
       count.setBackground(Color.orange);
       delete.setBackground(Color.green);
       exit.setBackground(Color.red);
       ta.setBackground(Color.cyan);
       Font MyFont=new Font("楷体_GB2312",Font.PLAIN,20);
       input.setFont(MyFont);
       output.setFont(MyFont);
       sort.setFont(MyFont);
       search.setFont(MyFont);
       count.setFont(MyFont);
       delete.setFont(MyFont);
       exit.setFont(MyFont);
	   setLayout(new FlowLayout());
	   add(input);
	   add(output);
	   add(sort);
	   add(search);
	   add(count);
	   add(delete);
	   add( exit);
	   add(ta);
	   input.addActionListener(this);
	   output.addActionListener(this);
	   sort.addActionListener(this);
	   search.addActionListener(this);
	   count.addActionListener(this);
	   delete.addActionListener(this);
	   exit.addActionListener(this);
	   setSize(350,250);
	   show();
    }/*初始化结束*/
   public void actionPerformed(ActionEvent e)
   {
     if(e.getActionCommand()=="输入记录")
      {
       while(true)
       {
         System.out.print("待输入记录的学号(输入“-1”退出):");
         int number =Console.readInt();
         if (number != -1)
         {
           System.out.print("  姓名:");
           String name =Console.readSting();
           System.out.print("  英语:");
           int English =Console.readInt();
           System.out.print("  数学:");
           int math =Console.readInt();
           System.out.print("  计算机:");
           int computer =Console.readInt();
           Student newData = new Student(number, name, English, math, computer);
		           if (StudentSet.addData(newData) == -1)
		           {
		             System.out.println("数据溢出!");
		             return ;
                   }
          }
        else
         {
           return ;
         }
       }/*while循环结束*/
      }/*if结束*/
    if(e.getActionCommand()=="输出记录")
      {
         if (StudentSet.getLen() == 0)
           {
              System.out.println("没有记录!");
           }
         else
          {
             for (int i = 0; i < StudentSet.getLen(); i++)
              {
                  System.out.println("学号:"+StudentSet.getData(i).getNumber()
                                  +"\n姓名:"+StudentSet.getData(i).getName()
                                  +"\n数学:"+StudentSet.getData(i).getMath()
                                  +"\n英语:"+StudentSet.getData(i).getEnglish()
                                  +"\n计算机:"+StudentSet.getData(i).getComputer()
                                  +"\n平均:"+StudentSet.getData(i).getAverage()
                                  +"\n总计:"+StudentSet.getData(i).getTotal());
             }
         }
     }/*if结束*/
 if(e.getActionCommand()=="排序输出")
   {
       StudentSet.sortData();
       if (StudentSet.getLen() == 0)
   	        {
   	           System.out.println("没有记录!");
   	        }
   	      else
   	       {

   	          for (int i = 0; i < StudentSet.getLen(); i++)
   	           {
   	               System.out.println("学号:"
   	                               +"\n姓名:"
   	                               +"\n数学:"
   	                               +"\n英语:"
   	                               +"\n计算机:"
   	                               +"\n平均:"
   	                               +"\n总计:");
   	          }
            }
     }/*if结束*/
   if(e.getActionCommand()=="查找记录")
     {
       System.out.print("请输入要查找记录的学号:");
       int number =Console.readInt();
       int i;
       if ((i = StudentSet.search(number)) != -1)
       {
                   System.out.println("学号:"+StudentSet.getData(i).getNumber()
   	                               +"\n姓名:"+StudentSet.getData(i).getName()
   	                               +"\n数学:"+StudentSet.getData(i).getMath()
   	                               +"\n英语:"+StudentSet.getData(i).getEnglish()
   	                               +"\n计算机:"+StudentSet.getData(i).getComputer()
   	                               +"\n平均:"+StudentSet.getData(i).getAverage()
   	                               +"\n总计:"+StudentSet.getData(i).getTotal());
       }
       else
       {
         System.out.println("没有找到相应的记录!");
       }
     }/*if结束*/
   if(e.getActionCommand()=="统计人数")
     {
       System.out.println("90-100: "+StudentSet.count(90, 100)+"人");
       System.out.println("80-89: "+StudentSet.count(80, 89)+"人");
       System.out.println("70-79: "+StudentSet.count(70, 79)+"人");
       System.out.println("60-69: "+StudentSet.count(60, 69)+"人");
       System.out.println("0-59: "+StudentSet.count(0, 59)+"人");
     }/*if结束*/
   if(e.getActionCommand()=="删除记录")
     {
       while(true)
       {
         System.out.print("请输入要删除记录的学号(输入“-1”退出):");
         int number =Console.readInt();
         if (number != -1)
         {
           if(StudentSet.delete(number) == -1)
           {
             System.out.println("没有找到相应的记录!");
             return;
           }
           else
           {
             System.out.println("删除记录成功!");
           }
         }
         else
         {
           return;
         }
       }/*while结束*/
     }/*if结束*/
  if(e.getActionCommand()=="退出系统")
     {    dispose();
          System.exit(0);
     }
 }
}

/*建立学生信息类*/
class Student
{
  private int number;
  private String name;
  private int english;
  private int math;
  private int computer;
  private int total;
  private int average;
  public Student(int number, String name, int english, int math, int computer)
  {
    this.number = number;
    this.name = name;
    this.english = english;
    this.math = math;
    this.computer = computer;
    total = english + math + computer;
    average = total/3;
  }//创建构造函数初始化对象
  public int getNumber()
  {
    return number;
  }
  public String getName()
  {
    return name;
  }
  public int getEnglish()
  {
    return english;
  }
  public int getMath()
  {
    return math;
  }
  public int getComputer()
  {
    return computer;
  }
  public int getTotal()
  {
    return total;
  }
  public int getAverage()
  {
      return average;
  }
}/*学生信息类结束*/

/*初始化学生信息各种方法*/
class StudentSet
{
  private static final int maxLen = 1000;
  private static int len = 0;
  private static Student[] data = new Student[maxLen];
  public static int getMaxLen()
  {
    return maxLen;/*静态初始化*/
  }
  public static int getLen()
  {
    return len;/*静态初始化*/
  }
  public static Student getData(int index)
  {
    return data[index];/*静态初始化*/
  }
  /*增加学生数据*/
  public static int addData(Student newData)
  {
    if (len < maxLen)
    { data[len] = newData;
      len++;
      return 0;
    }
    else
    {
		return -1;
	}
  }
/*按平均成绩排序学生信息*/
  public static void sortData()
  {
    for (int i = 0; i < len -1; i++)
    {
      int minIndex = i;
      for (int j = i+1; j < len; j++)
      {
        if (data[minIndex].getAverage() > data[j].getAverage())
        {
          minIndex = j;
        }
      }
      if (minIndex != i)
      {
        Student temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
      }
    }
  }
/*查询指定学号学生信息*/
  public static int search(int number)
  {
    for(int i = 0; i < len; i++)
    {
      if (data[i].getNumber() == number)
      {
        return i;
      }
    }
    return -1;
  }
/*删除学生信息*/
  public static int delete(int number)
  {
    for (int i = 0; i < len; i++)
    {
      if (data[i].getNumber() == number)
      {
        for (int j = i + 1; j < len; j++)
        {
          data[j-1] = data[j];
        }
        len--;
        return 0;
      }
    }
    return -1;
  }
/*统计各分数段的人数*/
  public static int count(int min, int max)
  {
    int sum =0;

    for (int i = 0; i < len; i++)
    {
      if ((data[i].getAverage() >= min) && (data[i].getAverage() <= max))
      {
        sum++;
      }
    }
    return sum;
  }

}/*学生信息各种方法结束*/
 class Console
{
            public static int readInt()
		 	  	    {
                      int result=0 ;
		 	  	      try{
		 	  	           String temp =new BufferedReader(new InputStreamReader(System.in)).readLine();
		 	  	           result= Integer.parseInt(temp);
		 	  	         }
		 	  	      catch (Exception e)
		 	  	         {
		 	  	           System.out.println("Error: "+e);
		 	  	         }
		 	  	      return result;
		 	  	    }/*定义处理数字和字符函数方法*/
		 	  public static  String readSting()
		 	  	    {
		 	  	      String result = "";
		 	  	      try{
		 	  	           result = new BufferedReader(new InputStreamReader(System.in)).readLine();
		 	  	         }
		 	  	      catch (Exception e)
		 	  	         {
		 	  	           System.out.println("Error: "+e);
		 	  	         }
		 	  	      return result;
	  	   }/*定义处理数字和字符函数方法*/
 }

class access extends Frame
{    private Connection con;
     private Statement stm;
     private ResultSet rs;
	 public access()
	    {
         String url = "jdbc:odbc:javadbc";
         String username = "";
         String password = "";  //加载驱动程序以连接数据库
         try {
               Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
             }
        catch ( ClassNotFoundException e )
             {
              System.out.println( "装载 JDBC/ODBC 驱动程序失败。"+e );//捕获加载驱动程序异常
             }
         try
            {
             con=DriverManager.getConnection("jdbc:odbc:javadbc","","");
             stm=con.createStatement();
             rs=stm.executeQuery("SELECT * FROM  first  ");
             while(rs.next())
              {
                System.out.println(" 学号:"+rs.getString(1));
                System.out.println( "姓名:"+rs.getString(2));
                System.out.println("英语:"+rs.getString(3));
                System.out.println("数学:"+rs.getString(4));
                System.out.println("计算机:"+rs.getString(5));
                System.out.println("总分:"+rs.getString(6));
                System.out.println("平均分:"+rs.getString(7));


              }
             con.close();
             stm.close();
            }
        catch(SQLException e)
           { System.err.println( "无法连接数据库" );
             System.exit( 1 );
           }
	}
}

⌨️ 快捷键说明

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