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

📄 mastermodify.java

📁 创建一个数据库dbstu,其中包括下列表: 1.student(学号
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class MasterModify extends JFrame implements ActionListener{  
String title[]={"班主任编号","班主任姓名","性别","电话"};
	JTextField txtNo=new JTextField(10);
	JTextField txtName=new JTextField(10);	
	JTextField txtPhone=new JTextField(20);
	ButtonGroup group=new ButtonGroup();    //定义组
	JRadioButton radioSexM=new JRadioButton("男",true);    //定义单选钮
	JRadioButton radioSexF=new JRadioButton("女",false);   //定义单选钮
  JButton ok=new JButton("保存");
  JButton cancel=new JButton("取消");
  JButton okk=new JButton("ok");
   String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbstu";
        Connection con;
        Statement stmt;
        ResultSet rs;
  String No;//
 MasterModify(){     
     setTitle("修改学生档案窗口");
    try{
       Container con=getContentPane();
      con.setLayout(new BorderLayout(0,5));      //设置边界布局
     
        ButtonGroup bgp=new ButtonGroup();  //为单选钮分组
        bgp.add(radioSexM);bgp.add(radioSexF);
        setSize(450,410);
        JPanel p[]=new JPanel[4];          //建立面板数组,
		for(int i=0;i<4;i++)   
		{	p[i]=new JPanel(new FlowLayout(FlowLayout.LEFT));//建立面板
			p[i].add(new JLabel(title[i])); //建立并添加标签
		}
		p[0].add(txtNo); p[0].add(okk);
		p[1].add(txtName); 
		p[2].add(radioSexM);p[2].add(radioSexF);
		p[3].add(txtPhone);		
		JPanel top=new JPanel();                 //建立面板top,用于放置p[0]~p[6]
		top.setLayout(new GridLayout(4,1));   //面板top设置网格布局
		for(int i=0;i<4;i++)                     //将面板p[0]~p[6]添加到top
			top.add(p[i]);
		JPanel bottom=new JPanel(); //建立面板bottom,用于放置按钮
		bottom.add(ok);bottom.add(cancel);
	JPanel vail=new JPanel();
		con.add(top,"Center");
		con.add(vail,"North");
		con.add(bottom,"South"); 
		setVisible(true);
        ok.addActionListener(this);
         okk.addActionListener(this);
        cancel.addActionListener(this);
        
      }catch(Exception e){e.printStackTrace();}
      setVisible(true);
   }
   boolean loadData(){     //将数据填到组件
      try{
       txtNo.setText(rs.getString(1));
       txtName.setText(rs.getString(2));
       if(rs.getString(3).equals("男"))
         radioSexM.setSelected(true);
       else
         radioSexF.setSelected(true);
       txtPhone.setText(rs.getString(4));      
         
     }catch(SQLException e){e.printStackTrace();return false;}
     return true;	
   }
   public void actionPerformed(ActionEvent e){
     try{
     	if(e.getSource()==okk){
     		No=txtNo.getText();
     	System.out.println(No);  	//   		
     		rs=stmt.executeQuery("select * from master where 班主任编号="+No );//where student.txtNo+"+txtNo
         if(rs.next())
          {
          	rs.first();
          loadData();         
        }
        else//
         JOptionPane.showMessageDialog(null,"没有找到相应的记录!");
     	rs.close();	
     		}
       if(e.getSource()==ok){
         String sex;
        
        try {//
           Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
       } catch(java.lang.ClassNotFoundException ef) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(ef.getMessage());
        }
        con = DriverManager.getConnection(url, "sa", "");
			stmt = con.createStatement();

         if(radioSexM.isSelected()) sex="男";
         else sex="女";
       
         String sql;
         System.out.println(txtNo.getText()+txtName.getText()+" "+sex+" "+txtPhone.getText());
       
          if(rs.next()){
          	rs.updateString("班主任姓名",txtName.getText());
         	//rs.updateString("姓名",txtName.getText());
         	rs.updateString("性别",sex);         	
         	
         	rs.updateString("电话",txtPhone.getText());
         	
         	try{rs.updateRow();}catch(Exception eu){	}
         	}
       } 
       else if(e.getSource()==cancel) dispose();
     }catch(Exception ee){ee.printStackTrace();}
   }
  /* public static void main(String args[]){ 
     JFrame.setDefaultLookAndFeelDecorated(true);
     Font font = new Font("JFrame", Font.PLAIN, 14);
     Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
     while (keys.hasMoreElements()) {
       Object key = keys.nextElement();
       if(UIManager.get(key) instanceof Font)UIManager.put(key, font);
     }
    
	// newMasterModify(-1);    //-1添加新记录的调用方法
	 new MasterModify();        //修改学号为2的记录
   }*/
}

⌨️ 快捷键说明

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