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

📄 modify.java

📁 java学习课件
💻 JAVA
字号:
/*修改学生信息程序Modify.java*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Modify  implements ActionListener
{ JTextField [] value=new JTextField[6]; 
  JLabel [] fieldName=new JLabel[6];
  JButton modifyButton,queryButton,nextButton;
  JPanel panel1,panel2,panel3;
  JTextField studentNo=new JTextField(11); //记录输入的学号
  StudentManager server;
  public Modify(StudentManager server,JPanel content)
  {  this.server=server;
     content.setLayout(new GridLayout(2,1));		
     fieldName[0]= new JLabel("学号");
     fieldName[1]= new JLabel("姓名");
     fieldName[2]= new JLabel("出生年月");
     fieldName[3]= new JLabel("性别");
     fieldName[4]= new JLabel("入学成绩");
     fieldName[5]= new JLabel("备注");
     for(int i=0;i<6; i++)  value[i] = new JTextField(10);
     queryButton = new JButton("查询");
     nextButton = new JButton("不修改---下一个");
     modifyButton = new JButton("修改后保存");
     panel1=new JPanel(new GridLayout(1,3));  //创建窗格对象…
     panel2=new JPanel(new GridLayout(2,6));
     panel3=new JPanel(new GridLayout(1,2));
     panel1.add(new JLabel("输入要修改内容信息的学生的学号:")); //将构件加入窗格……
     panel1.add(studentNo);
     panel1.add(queryButton);
     for(int i=0;i<6; i++) panel2.add(fieldName[i]);
     for(int i=0;i<6; i++) panel2.add(value[i]);content.add(panel1);
     panel3.add(nextButton);
     panel3.add(modifyButton);
     content.add(panel1);  //将窗格加入用户界面(框架容器)……
     content.add(panel2);
     content.add(panel3);
     panel2.setVisible(false);  //隐藏panel2 
     panel3.setVisible(false);  //隐藏panel3
     queryButton.addActionListener(this); //注册按钮的监听对象……
     modifyButton.addActionListener(this);
     nextButton.addActionListener(this);
   }
   public void actionPerformed(ActionEvent evt) //实现单击按钮的事件方法
   { Object obj = evt.getSource();
     try
     {if(obj==queryButton)   //查看要修改的记录
      {
        String sqlstr="select * from student_login where  学号='"+studentNo.getText()+"'";
        Vector rs=server.query(sqlstr);
        if(rs.size()>0)
         { for(int i=0; i<value.length; i++)
           value[i].setText((String)rs.get(i));//显示字段项
           panel2.setVisible(true);
           panel3.setVisible(true);
         }  
        else 
         { JOptionPane.showMessageDialog(null,"无此学号,请重新输入!","提示信息",JOptionPane.PLAIN_MESSAGE);}
      }
      else
      {if(obj==modifyButton) //单击了修改按钮  
       { String modiStr="update student_login set 学号=?,姓名=?,出生年月=?,性别=?,入学成绩=?,备注=? where 学号=?";
         String[] mvalues=new String[7];
         for(int i=0; i<value.length; i++) mvalues[i]=value[i].getText();
         mvalues[6]= studentNo.getText();
         int n=server.modify(modiStr,mvalues);
         String mes="遇到操作错误,记录修改失败!!!";
         if(n==1) mes="记录已被成功修改!!!"; 
         JOptionPane.showMessageDialog(null,mes,"操作提示信息",JOptionPane.PLAIN_MESSAGE);
       }
       panel2.setVisible(false);
       panel3.setVisible(false);
      } 
    }	  
    catch(Exception e) { System.out.println("Error:"+e); } 
   } //事件方法结束
}

⌨️ 快捷键说明

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