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

📄 masteradd.java

📁 创建一个数据库dbstu,其中包括下列表: 1.student(学号
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class MasterAdd 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("取消");
  String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbstu";
     Connection con;
        Statement stmt;
        ResultSet rs;
  MasterAdd(){     
    setTitle("添加master窗口");     ;
    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[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);
        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()==ok){
       	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 sex;         
         
         //txtNo=rs.getSting(1);
         if(radioSexM.isSelected()) sex="男";
         else sex="女";
        // if(checkIsMember.isSelected()) isMember="Y";
        // else isMember="N";
         String sql;
         
        // if(No==-1)
         {              //添加记录Integer.parseInt()
           sql="insert into master(班主任编号,班主任姓名,性别,电话)";
           sql=sql+" values("+txtNo.getText()+",'" +txtName.getText()+"','"+sex;
           sql=sql+"','"+txtPhone.getText()+"')";
           
         System.out.println(sql);
         }
        /* else{                   //修改记录
           sql="update xsda set classID="+classID;
           sql=sql+",Name='"+txtName.getText();
           sql=sql+"',sex='"+sex+"',Age='"+txtAge.getText();
           sql=sql+"',isMember='"+isMember;
           sql=sql+"',address='"+txtAddress.getText();
           sql=sql+"',resume='"+txtResume.getText()+"' where no="+No;
         }*/
         stmt.executeUpdate(sql);  //执行SQL语句
       } 
       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);
     }     
	 new MasterAdd();    //-1添加新记录的调用方法
	// new MasterAdd(2);        //修改学号为2的记录
   }*/
}

⌨️ 快捷键说明

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