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

📄 xulei.java

📁 学生成绩管理系统,实现对学生成绩的增,删,查,改等功能,具有模糊查询的能力
💻 JAVA
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
class win extends JFrame implements ActionListener 
{ 
 JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11; 
 JButton select1,select2,update,insert,del,clear,cleartable; 
 Connection con=null;
 Statement stmt=null;
 JSplitPane split;
 PreparedStatement pstmt=null;
 ResultSet rs=null;
 Object[][] data=new Object[20][6];
 Object colnames[]={"学号","姓名","成绩"};
 JTable table;
 int selectjilu;
 
 win()
 { super("学生成绩表");
   setVisible(true);
   table=new JTable(data,colnames); 
   JScrollPane jsp=new JScrollPane(table);
   
   Toolkit tool=getToolkit();
   Dimension screen=tool.getScreenSize();
   setSize((screen.width)/2,(screen.height)/2);
   setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2);
   // 加载驱动
   try
   { 
   		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   }
   catch(ClassNotFoundException e){System.out.println(e.getMessage());}

 	//界面布局
 	t1=new JTextField(6);t2=new JTextField(6);
 	t3=new JTextField(6);t4=new JTextField(6);
 	t5=new JTextField(6);t6=new JTextField(6);
 	t7=new JTextField(6);t8=new JTextField(6);
 	t9=new JTextField(6);t10=new JTextField(6);
 	t11=new JTextField(6);
 	
    select1=new JButton("查询");
 	update=new JButton("更新");
 	select2=new JButton("查询-->");
 	insert=new JButton("添加"); 
 	del=new JButton("删除");
 	clear=new JButton("清除文本框");
 	cleartable=new JButton("清除表格");
 	
 	JPanel p1=new JPanel();JPanel p2=new JPanel();
 	JPanel p3=new JPanel();JPanel p4=new JPanel();
 	JPanel p5=new JPanel();JPanel p6=new JPanel();
 	JPanel p7=new JPanel();
 	
 	p7.setLayout(new GridLayout(5,1));
 	
 	p1.add(new JLabel("输入要查询的学号"));p1.add(t1);
 	p1.add(new JLabel("姓名"));p1.add(t2);
 	p1.add(new JLabel("成绩"));p1.add(t3);
 	
 	p1.add(select1);
 	
 	p2.add(new JLabel("输入要添加的学号"));p2.add(t4);
 	p2.add(new JLabel("姓名"));p2.add(t5);
 	p2.add(new JLabel("成绩"));p2.add(t6);
 	
 	p2.add(insert);
 	
 	p3.add(new JLabel("输入要更新的学号"));p3.add(t7);
 	p3.add(new JLabel("姓名"));p3.add(t8);
 	p3.add(new JLabel("成绩"));p3.add(t9);
 	
 	p3.add(update);
 	
 	p4.add(new JLabel("输入要删除的学号"));
 	p4.add(t10);
 	p4.add(del);
 	p4.add(clear);
 	
 	p5.add(new JLabel("模糊查询(按学号)"));
 	p5.add(t11);
 	p5.add(select2);
 	p5.add(cleartable);
 	
 	p6.add(jsp);
 	
 	p7.add(p1);
 	p7.add(p2);
 	p7.add(p3);
 	p7.add(p4);
 	p7.add(p5);
 	
 	setLayout(new BorderLayout());
 	add(p7,"North");
    add(jsp,"Center");
    
    split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,p7,jsp);
    split.setOneTouchExpandable(true);
    add(split);
    
 	// 以下 注册事件监听
 	select1.addActionListener(this);
 	update.addActionListener(this);
 	del.addActionListener(this);
 	insert.addActionListener(this);
 	clear.addActionListener(this);
 	cleartable.addActionListener(this);
 	select2.addActionListener(this);
 	
 	addWindowListener(new WindowAdapter()
 	{ public void windowClosing(WindowEvent e)
 	    {setVisible(false);System.exit(0);}});
    }
        
    // 以下 监听事件处理
 	public void actionPerformed(ActionEvent e)
 	{  if(e.getSource()==select1)
 	    {	
 	       selectjilu=0;
 	     try{ select1();}//调用精确查询
 	     catch(SQLException ee){System.out.println(ee.getMessage());}
 	    }
 	   
 	   else if(e.getSource()==insert)
 	   { try{ insert1();}//调用插入方法
 	     catch(SQLException ee){System.out.println(ee.getMessage());}
 	   }
 	   
 	   else if(e.getSource()==update)
 	   { try{ update1();}//调用更新查询
 	     catch(SQLException ee){System.out.println(ee.getMessage());}
 	   }
 	
 	   else if(e.getSource()==del)
 	   { try{ del1();}//调用删除
 	     catch(SQLException ee){System.out.println(ee.getMessage());}
       }
       
 	   else if(e.getSource()==select2)
 	   { 
 	     select2();//调用模糊查询
 	   }
 
  	  else if(e.getSource()==clear) // 清除文本框 处理
 	   { 
 	   	 t1.setText("");t2.setText("");
 	     t3.setText("");t4.setText("");
 	     t5.setText("");t6.setText("");
 	     t7.setText("");t8.setText("");
 	     t9.setText("");t10.setText("");
 	     t11.setText("");
 	     
 	   }
 	  else if(e.getSource()==cleartable)
 	   {  
 	    tableclear();
 	   }
 	
 	}
 // 精确查询方法  抛出处理异常	
   public void select1() throws SQLException
   {   
   	   String sid,sname,score;
       con=DriverManager.getConnection("jdbc:odbc:xulei","xulei","123");
       stmt=con.createStatement();
       rs=stmt.executeQuery("select * from haha");
       while(rs.next())
      { 
        sid=rs.getString("学号");
      	sname=rs.getString("姓名");
      	score=rs.getString("成绩");
      	
      
      	if(sid.equals(t1.getText().trim()))
      	{ 
      		t2.setText(sname);
      		t3.setText(score);
      		
      		selectjilu=1;
      		JOptionPane.showMessageDialog(null,"查询成功!");
      		break;
      	}
      }
      con.close();
      if(selectjilu==0)
      {  
     	
    	JOptionPane.showMessageDialog(null,"没有找到该学生!");
      }
 
 }
 // 添加方法 抛出处理异常 
    public void insert1() throws SQLException
    { String s1="'"+t4.getText().trim()+"'",
    		 s2="'"+t5.getText().trim()+"'",
    		 s3="'"+t6.getText().trim()+"'";
      String temp="insert into haha values ("+s1+","+s2+","+s3+")";
      String sid;
      int t=0;
      con=DriverManager.getConnection("jdbc:odbc:xulei","xulei","123");
      stmt=con.createStatement();
      rs=stmt.executeQuery("select * from haha");
      while(rs.next())
      { 
    	sid=rs.getString("学号");
    	if(sid.equals(t4.getText().trim()))
    	{ t=1;}
      
       }
    if(t==1)
    {
    	JOptionPane.showMessageDialog(null,"已存在,欲操作?请更新") ;
    } 
    else
    {
 		stmt.executeUpdate(temp);
    	JOptionPane.showMessageDialog(null,"添加成功!");
    }
    
     con.close();
    }
    
  //更新方法(不存在更新对象的话,则弹出提示),抛出处理异常 
    public void update1() throws SQLException  
    { 
      String s1="'"+t7.getText().trim()+"'",
      		 s2="'"+t8.getText().trim()+"'",
      		 s3="'"+t9.getText().trim()+"'";
      String sid;
      int t=0;
      String temp="update haha set 姓名 ="+s2+",成绩="+s3+" where 学号="+s1;
      con=DriverManager.getConnection("jdbc:odbc:xulei","xulei","123");
      stmt=con.createStatement();
      rs=stmt.executeQuery("select * from haha");
      while(rs.next())
      { 
    	sid=rs.getString("学号");
    	if(sid.equals(t7.getText().trim()))
    	{ t=1;}
      
      }
    if(t==1)
    {
    	stmt.executeUpdate(temp);
    	JOptionPane.showMessageDialog(null,"更新成功!");
    } 
    else
    {
 		JOptionPane.showMessageDialog(null,"不存在此学号!");
    }
       
        con.close();
	
    }

  // 删除方法(若不存在要删的学号,则弹出提示),抛出处理异常 
   public void del1() throws SQLException
   {  String s1="'"+t10.getText().trim()+"'";
      String sid;
      int t=0;
      String temp="delete from haha where 学号="+s1;
      con=DriverManager.getConnection("jdbc:odbc:xulei","xulei","123");
      stmt=con.createStatement();
      rs=stmt.executeQuery("select * from haha");
      while(rs.next())
      { 
    	sid=rs.getString("学号");
    	if(sid.equals(t10.getText().trim()))
    	{ t=1;}
      }
    
    if(t==1)
    {
    	stmt.executeUpdate(temp);
    	JOptionPane.showMessageDialog(null,"删除成功!");
    } 
    else
    {
 		JOptionPane.showMessageDialog(null,"不存在此学号!");
 	}
    con.close();
    }
    	
 //模糊查询方法  尝试不用抛出处理 用try{} catch{}处理
    public void select2() 
    {  
         int i=0;
         String s= "%"+t11.getText().trim()+ "%";
	     try{
	     		con=DriverManager.getConnection("jdbc:odbc:xulei","xulei","123");
             }
             catch(SQLException eee){System.out.println(eee.getMessage());}
   	     try
   	     {
   	     	pstmt = con.prepareStatement("select * from haha where 学号 like ?");
         	pstmt.setString(1,s);
         	rs= pstmt.executeQuery();
         	tableclear();
         	while(rs.next())
         	{   
         	   data[i][0]=rs.getString("学号");
         	   data[i][1]=rs.getString("姓名");
         	   data[i][2]=rs.getString("成绩");
         	   i++;
         	}
         	
         	con.close();
         	
          }
          catch(SQLException eee){System.out.println(eee.getMessage());}
            
         if(i==0)
           { 
           t11.setText("没找到该学生");
           JOptionPane.showMessageDialog(null,"没有找到该学生!");
           }
           
    }
    
   // 清除表格方法 	
    public void tableclear() 
 	{
       for( int j=0;j<20;j++)
         for(int k=0;k<3;k++)
         { 
         	data[j][k]=null;
         }     
         table.repaint(); 
 	}
}
	 
public class xulei
{ public static void main(String args[])
	
  { 
  	  win frame=new win();
      frame.validate();    
  }	
  
}

⌨️ 快捷键说明

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