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

📄 studentdatamodify.java

📁 创建一个数据库dbstu,其中包括下列表: 1.student(学号
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class StudentDataModify extends JFrame implements ActionListener{  
String title[]={"学号","姓名","性别","年龄","专业","系名","学院名称"};
//JComboBox txtNo=new JComboBox();
   JTextField txtNo=new JTextField(10);
  JTextField txtName=new JTextField(10);
  JTextField txtAge=new JTextField(10);
  JTextField txtZhuanYe=new JTextField(20);
  JTextField txtXiName=new JTextField(20);
  JTextField txtXueYuan=new JTextField(20);
  JRadioButton radioSexM=new JRadioButton("男",true);
  JRadioButton radioSexF=new JRadioButton("女",false);  
  JButton ok=new JButton("保存");
  JButton cancel=new JButton("取消");
  JButton okk=new JButton("ok");
  Statement stmt;
  ResultSet rs;
   String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbstu";
        Connection con;
  String No;//
  StudentDataModify(){     
     setTitle("修改学生档案窗口");
    try{
      Container con=getContentPane();
      con.setLayout(new BorderLayout(0,5));      //设置边界布局
       //stmt=ConnectServer2.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //建立可滚动并且可更新的结果集       	
      // rs=stmt.executeQuery("select * from student");    
     // while(rs.next()){                  //将班级信息添入到下拉列表中 order by 学号"
       //txtNo.addItem(rs.getString(1));}
       txtNo.addActionListener(new ActionListener(){
       	public void actionPerformed(ActionEvent ev){
       		No=txtNo.getText();
       	}
       });
        ButtonGroup bgp=new ButtonGroup();  //为单选钮分组
        bgp.add(radioSexM);bgp.add(radioSexF);
        setSize(450,410);
        JPanel p[]=new JPanel[7];
        for(int i=0;i<7;i++){
          p[i]=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
          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(txtAge);
        p[4].add(txtZhuanYe);
        p[5].add(txtXiName);
        p[6].add(txtXueYuan); 
        JPanel p1=new JPanel(new GridLayout(7,1,0,5));
        for(int i=0;i<7;i++) p1.add(p[i]);       
        JPanel p3=new JPanel();
        p3.add(ok);p3.add(cancel);
        JPanel p2=new JPanel();
        con.add(p2,"North"); con.add(p1,"Center");con.add(p3,"South");
        ok.addActionListener(this);
         okk.addActionListener(this);
        cancel.addActionListener(this);
        // if(No!="-1")
      /*{
rs=stmt.executeQuery("select * from student where 学号=200602" );//where student.txtNo+"+txtNo
         if(rs.next())
          {rs.first();
          loadData();
         // txtNo.setEditable(false);
        }
        else//
         JOptionPane.showMessageDialog(null,"没有找到相应的记录!");
        }
        rs.close(); */
      }catch(Exception e){e.printStackTrace();}
      setVisible(true);
   }
   boolean loadData(){     //将数据填到组件
     try{
     // txtNo.setSelectedItem(rs.getString(1));
        txtNo.setText(rs.getString(1));
       txtName.setText(rs.getString(2));
       if(rs.getString(3).equals("男"))
         radioSexM.setSelected(true);
       else
         radioSexF.setSelected(true);
       txtAge.setText(rs.getString(4));
       /*if(rs.getString(6).toUpperCase().equals("Y"))
         checkIsMember.setSelected(true);
       else
         checkIsMember.setSelected(false);*/
       txtZhuanYe.setText(rs.getString(5));
       txtXiName.setText(rs.getString(6));  
        txtXueYuan.setText(rs.getString(7)); 
         
     }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 student where 学号="+No );//where student.txtNo+"+txtNo
         if(rs.next())
          {
          	rs.first();
          loadData();         
        }
        else//
         JOptionPane.showMessageDialog(null,"没有找到相应的记录!");
     	rs.close();	
     		}
       if(e.getSource()==ok){
       try {
        	
           Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        } catch(java.lang.ClassNotFoundException eaa) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(eaa.getMessage());
        }

       	      
            con = DriverManager.getConnection(url, "sa", "");
         //int classID;
         Statement stmt = con.createStatement();  String sex;
        
        
        // Statement stmt=ConnectServer2.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 	
  ResultSet rs=stmt.executeQuery("select * from student where 学号="+No);//name='"+txtNo.getSelectedItem()+"'"
        // rs.first();
      
         if(radioSexM.isSelected()) sex="男";
         else sex="女";
       
         String sql;
         System.out.println(txtName.getText()+" "+sex+" "+txtAge.getText()+" "+txtZhuanYe.getText()+" "+txtXiName.getText()+" "+txtXueYuan.getText());
       
        /* {                   //修改记录
           sql="update student set ";//txtNo=+txtNo;
           sql=sql+",姓名='"+txtName.getText();
           sql=sql+"',性别='"+sex;
           sql=sql+"',年龄='"+txtAge.getText();
           sql=sql+"',专业='"+txtZhuanYe.getText();
           sql=sql+"',系名='"+txtXiName.getText();
           sql=sql+"',学院名称='"+txtXueYuan.getText()+"' where 学号="+txtNo;//
         }
         stmt.executeUpdate(sql);  //执行SQL语句*/
         if(rs.next()){
         	rs.updateString("姓名",txtName.getText());
         	rs.updateString("性别",sex);
         	rs.updateString("年龄",txtAge.getText());
         	
         	rs.updateString("专业",txtZhuanYe.getText());
         	rs.updateString("系名",txtXiName.getText());
         	rs.updateString("学院名称",txtXueYuan.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);
     }
     if(!ConnectServer2.conn("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbstu","sa","")){  
       JOptionPane.showMessageDialog(null,"数据库连接不成功!");
	   System.exit(0);
	 }
	// new StudentDataModify(-1);    //-1添加新记录的调用方法
	 new StudentDataModify();        //修改学号为2的记录
   }*/
}

⌨️ 快捷键说明

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