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

📄 addressmodify.java

📁 创建一个数据库dbstu,其中包括下列表: 1.student(学号
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class AddressModify extends JFrame implements ActionListener{  
String title[]={"学号","宿舍名称","寝室号","楼层电话","电话","邮箱","班主任编号"};
	JTextField xh=new JTextField(10);
	JTextField ssmc=new JTextField(10);
		JTextField qshao=new JTextField(10);
		JTextField lcdh=new JTextField(10);
		JTextField dh=new JTextField(10);
		JTextField yx=new JTextField(10);
		JTextField bzrbh=new JTextField(10);
  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;//
 AddressModify(){     
     setTitle("修改adress窗口");
    try{
       Container con=getContentPane();
      con.setLayout(new BorderLayout(0,5));      //设置边界布局
           
        
        setSize(450,410);
        JPanel p[]=new JPanel[7];               //建立面板数组,
		for(int i=0;i<7;i++)   
		{	p[i]=new JPanel(new FlowLayout(FlowLayout.LEFT));//建立面板
			p[i].add(new JLabel(title[i])); //建立并添加标签
		}
		p[0].add(xh); p[0].add(okk);
		p[1].add(ssmc); 
		p[2].add(qshao);
		p[3].add(lcdh);
		p[4].add(dh);
		p[5].add(yx);
		p[6].add(bzrbh);
		
		JPanel top=new JPanel();                 //建立面板top,用于放置p[0]~p[6]
		top.setLayout(new GridLayout(7,1));   //面板top设置网格布局
		//top.setBackground(Color.magenta);
		for(int i=0;i<7;i++)                     //将面板p[0]~p[6]添加到top
			top.add(p[i]);
		JPanel center=new JPanel();//该面板用于放置centerleft,centerright
		center.setLayout(new BorderLayout()); //设置center为边界布局   
    
		JPanel bottom=new JPanel(); //建立面板bottom,用于放置按钮
		bottom.add(ok);bottom.add(cancel);
	  
		con.add(top,"Center");
   	   //	con.add(center,"");
		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{
       xh.setText(rs.getString(1));
       ssmc.setText(rs.getString(2));       
       qshao.setText(rs.getString(3));
       
       lcdh.setText(rs.getString(4));
       dh.setText(rs.getString(5));  
       yx.setText(rs.getString(6)); 
       bzrbh.setText(rs.getString(7));          
     }catch(SQLException e){e.printStackTrace();return false;}
     return true;		
   }
   public void actionPerformed(ActionEvent e){
     try{
     	if(e.getSource()==okk){
     		No=xh.getText();
     	System.out.println(No);  	//   		
     		rs=stmt.executeQuery("select * from address 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();
        
         String sql;
         System.out.println(xh.getText()+ssmc.getText()+" "+qshao.getText()+" "+lcdh.getText());
       
          if(rs.next()){
          	rs.updateString("宿舍名称",ssmc.getText());
         	rs.updateString("寝室号",qshao.getText());
         	rs.updateString("楼层电话",lcdh.getText());  
         	rs.updateString("电话",dh.getText());
         	rs.updateString("邮箱",yx.getText()); 
         	rs.updateString("班主任编号",bzrbh.getText()); 
         	
         	try{rs.updateRow();}catch(Exception eu){	}
         	}
       } 
       else if(e.getSource()==cancel) dispose();
     }catch(Exception ee){ee.printStackTrace();}
   }
  // public static void main(String args[]){ 	 new AddressModify();        //修改学号为2的记录
 // }
}

⌨️ 快捷键说明

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