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

📄 addfi.java

📁 基于java的学生信息管理系统的开发与应用
💻 JAVA
字号:
package sfms;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;


public class AddFI extends JFrame implements ActionListener{
  //variables needing class scope
  JTextField STNOText,SNameText,genderText,departmentText,addressText,telText;
  JButton addButton,clearButton,closeButton;
  FileInformation student;
  String STNO,SName,Gender,Department,Address,Tel;
  mainWindow parentWindow;


  //constructor
  public AddFI(mainWindow main){
    parentWindow=main;
    Container c=this.getContentPane();
    c.setLayout(new GridLayout(3,1));
    JPanel centerPanel=new JPanel(new GridLayout(6,2));
    JPanel lowerPanel=new JPanel(new FlowLayout());

    //create logo
   JLabel logoLabel=new JLabel("",SwingConstants.CENTER);
    logoLabel.setForeground(Color.blue);
    logoLabel.setFont(new Font("TRUE",Font.TRUETYPE_FONT,20));
    logoLabel.setText("学生信息");
    c.add(logoLabel);

    //create TextField for FileInformation
    STNOText=new JTextField();
    SNameText=new JTextField();
    genderText=new JTextField();
    departmentText=new JTextField();
    addressText=new JTextField();
    telText=new JTextField();

    //add labels and textfields to the panels
    centerPanel.add(new JLabel("学号",SwingConstants.CENTER));
    centerPanel.add(STNOText);
    centerPanel.add(new JLabel("姓名",SwingConstants.CENTER));
    centerPanel.add(SNameText);
    centerPanel.add(new JLabel("性别",SwingConstants.CENTER));
    centerPanel.add(genderText);
    centerPanel.add(new JLabel("系别",SwingConstants.CENTER));
    centerPanel.add(departmentText);
    centerPanel.add(new JLabel("籍贯",SwingConstants.CENTER));
    centerPanel.add(addressText);
    centerPanel.add(new JLabel("电话",SwingConstants.CENTER));
    centerPanel.add(telText);
    c.add(centerPanel) ;
    //create & add Buttons to bottom panel
    addButton=new JButton("添加");
    clearButton=new JButton("清除");
    closeButton=new JButton("退出");
    lowerPanel.add(addButton);
    lowerPanel.add(clearButton);
    lowerPanel.add(closeButton);
    c.add(lowerPanel);


    //register frames as listener for button events
    addButton.addActionListener(this);
    clearButton.addActionListener(this);
    closeButton.addActionListener(this);

    this.setSize(400,300);
    this.setTitle("添加学生信息");
    this.setVisible(true);

    //create anonymous inner class to handle window closing event
    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent event){
        System.exit(0);
      }
    });
  }
  //actionPerformed is involked when a Button is clicked
  public void actionPerformed(ActionEvent e){
    //see which button was clicked
    if(e.getSource()==addButton)
    {addFI();}
    if(e.getSource() ==clearButton)
    {clearForm();}
    if(e.getSource() ==closeButton)
    {shutDown();}
  }
  private void addFI(){
    STNO=STNOText.getText();
    SName=SNameText.getText();
    Gender=genderText.getText();
    Department=departmentText.getText();
    Address=addressText.getText();
    Tel=telText.getText();

    if(STNO.length()==0||SName.length()==0||Gender.length()==0||Department.length()==0||Address.length()==0||Tel.length()==0)
      JOptionPane.showMessageDialog(this,"请添加完全信息");
      else
      {
        try{
        Class.forName("oracle.jdbc.OracleDriver").newInstance();
        String url="jdbc:oracle:thin:@localhost:1521:SFMS";
        Connection conn=DriverManager.getConnection(url,"df","1234");
        String sql="INSERT INTO DF.FILEINFO VALUES(?,?,?,?,?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1,STNO);
        ps.setString(2,SName);
        ps.setString(3,Gender);
        ps.setString(4,Department);
        ps.setString(5,Address);
        ps.setString(6,Tel);
        ps.executeUpdate();
       conn.close();
        JOptionPane.showMessageDialog(this,"添加成功!");
        clearForm();
      }
      catch(SQLException e){
        System.out.println("E Code"+ e.getErrorCode());
        System.out.println("E M="+e.getMessage());
      }
      catch(Exception e){
        e.printStackTrace();
      }
     }
  }
  private void clearForm(){
    STNOText.setText("");
    SNameText.setText("");
    genderText.setText("");
    departmentText.setText("");
    addressText.setText("");
    telText.setText("");
    STNOText.requestFocus();
      }
  private void shutDown(){

   parentWindow.setVisible(true);
    this.dispose();
  }
  /*    public static void main(String[] args){
      AddFI frame1=new AddFI();
     frame1.show();
}
*/
}

⌨️ 快捷键说明

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