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

📄 regframe.java

📁 一个用Java写的简单学生成绩管理系统
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
//import java.lang.*;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class RegFrame extends JFrame
{
  private String stuid;
  private String name;
  private String password;
  private String email;
  private String sclass;
  private int department;
  String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  String source = "jdbc:odbc:srs";

  JLabel jlid = new JLabel();
  JLabel jlname = new JLabel();
  JLabel jlpwd = new JLabel();
  JLabel jlclass = new JLabel();
  JLabel jldep = new JLabel();
  JTextField jtfid = new JTextField();
  JTextField jtfname = new JTextField();
  JPasswordField jpfpwd = new JPasswordField();
  JTextField jtfclass = new JTextField();
  JComboBox jcbdep = new JComboBox();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JLabel jLemail = new JLabel();
  JTextField jtfemail = new JTextField();
  public RegFrame()
  {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    //jPanel1.setLayout(null);
    //this.getContentPane().setLayout(borderlayout1);
    Container container  = this.getContentPane();
    //container.setLayout(new GridLayout(6,2,5,5));
    container.setLayout(null);
    this.setTitle("学生注册");
    jlid.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 16));
    jlid.setForeground(Color.red);
    jlid.setText("学号");
    jlid.setBounds(new Rectangle(83, 50, 46, 20));
    jlname.setFont(new java.awt.Font("楷体_GB2312",Font.PLAIN,16));
    jlname.setForeground(Color.red);
    jlname.setText("姓名");
    jlname.setBounds(new Rectangle(83, 88, 46, 20));
    jlpwd.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 16));
    jlpwd.setForeground(Color.red);
    jlpwd.setToolTipText("d");
    jlpwd.setText("密码");
    jlpwd.setBounds(new Rectangle(83, 126, 47, 20));
    jlclass.setFont(new java.awt.Font("楷体_GB2312",Font.PLAIN,16));
    jlclass.setForeground(Color.red);
    jlclass.setText("班级");
    jlclass.setBounds(new Rectangle(81, 208, 47, 20));
    jldep.setFont(new java.awt.Font("楷体_GB2312",Font.PLAIN,16));
    jldep.setForeground(Color.red);
    jldep.setText("院系");
    jldep.setBounds(new Rectangle(82, 243, 47, 20));
    jtfid.setDisabledTextColor(Color.orange);
    jtfid.setText("33060423");
    jtfid.setBounds(new Rectangle(151, 50, 105, 20));

    jtfname.setText("邓皆斌");
    jtfname.setBounds(new Rectangle(151,88,105,20));
    jpfpwd.setText("33060423");
    jpfpwd.setBounds(new Rectangle(151,126,105,20));
    jtfclass.setText("330604");
    jtfclass.setBounds(new Rectangle(150, 206, 105, 20));
    //jtfdep.setText("computer science");
    //jtfdep.setBounds(new Rectangle(151,202,105,20));
    for (int i=1; i<22; i++)
    {
      String item = Integer.toString(i);
      jcbdep.addItem(item);
    }
    jButton1.setBounds(new Rectangle(78, 283, 73, 26));
    jButton1.setText("确定");
    jButton1.addActionListener(new RegFrame_jButton1_actionAdapter(this));
    jButton2.setBounds(new Rectangle(214, 284, 73, 26));
    jButton2.setText("重置");
    jButton2.addActionListener(new RegFrame_jButton2_actionAdapter(this)); //this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    jcbdep.setToolTipText("");
    jcbdep.setBounds(new Rectangle(150, 241, 104, 22));
    jcbdep.addItemListener(new RegFrame_jcbdep_itemAdapter(this));
    jLemail.setFont(new java.awt.Font("Arial", Font.PLAIN, 16));
    jLemail.setForeground(Color.red);
    jLemail.setText("E-mail");
    jLemail.setBounds(new Rectangle(81, 167, 50, 20));
    jtfemail.setSelectedTextColor(Color.black);
    jtfemail.setText("@");
    jtfemail.setBounds(new Rectangle(150, 165, 104, 22));
    container.add(jlid);
    container.add(jtfid);

    container.add(jlname);
    container.add(jtfname);
    container.add(jlpwd);
    container.add(jpfpwd);
    container.add(jButton1);
    container.add(jButton2);
    container.add(jldep);
    container.add(jcbdep);
    container.add(jlclass);
    container.add(jtfclass);
    container.add(jLemail);
    container.add(jtfemail);
    container.add(jcbdep);
    this.setSize(350,400);
    //this.setVisible(true);
    this.setResizable(false);
    //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getRootPane().setDefaultButton(jButton1);
  }


  public void jButton2_actionPerformed(ActionEvent e)    // 重置
  {
    jtfid.setText("");
    jtfname.setText("");
    jpfpwd.setText("");
    jtfemail.setText("@");
    jtfclass.setText("");
    //jtfdep.setText("");

  }

  public void jButton1_actionPerformed(ActionEvent e)    // 确定
  {
    char p[];
    password = "";
    stuid = jtfid.getText().trim();
    name = jtfname.getText().trim();
    p = jpfpwd.getPassword();
    for (int i = 0; i < p.length ; i++)
    {
      password = password + p[i];
    }
    //password = p.toString();
    email = jtfemail.getText().trim();
    sclass = jtfclass.getText().trim();
    try
    {
      Class.forName(driver);
    }
    catch(ClassNotFoundException ex)
    {
      JOptionPane.showMessageDialog(this,"没有发现驱动程序"+driver,"Error",1);
      ex.printStackTrace();
      System.exit(1);
    }
    try
    {
      Connection connection = DriverManager.getConnection(source);
      /*
      SQLWarning warn = connection.getWarnings();
      while (warn != null)
      {
        JOptionPane.showMessageDialog(this,warn.getMessage(),"ERROR",1);
        warn = warn.getNextWarning();
      }
      */
      Statement st = connection.createStatement();
      String createsql = "create table " + stuid + "(coursename varchar(20),xuefen int,professor varchar(20),address varchar(20),ctime varchar(20))";
      st.executeUpdate(createsql);
      /*
      System.out.println(stuid);
      System.out.println(name);
      System.out.println(password);
      System.out.println(email);
      System.out.println(sclass);
      System.out.println(department);
      */
      String insertsql = "insert into Student(id,name,password,email,class,dep,totalcourse) values('" + stuid +"','" + name + "','" +password+"','"+email+"','"+sclass+"',"+department+","+"0"+")";
      st.executeUpdate(insertsql);
      st.close();
      connection.close();
    }
    catch(Exception ex)
    {
      JOptionPane.showMessageDialog(this,"在执行数据库时出现错误!","Error",2);
      ex.printStackTrace();
      System.exit(1);
    }
    JOptionPane.showConfirmDialog(null,"注册已成功,请用您刚注册的学号及密码重新登录","注册成功",JOptionPane.YES_OPTION);
    this.dispose();
  }

  public void jcbdep_itemStateChanged(ItemEvent e)
  {
    int b;
    Object object;
    b = jcbdep.getSelectedIndex();
    //System.out.println(b+" DJB ");
    department = b + 1;
  }
}

class RegFrame_jButton1_actionAdapter
    implements ActionListener {
  private RegFrame adaptee;
  RegFrame_jButton1_actionAdapter(RegFrame adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class RegFrame_jcbdep_itemAdapter
    implements ItemListener {
  private RegFrame adaptee;
  RegFrame_jcbdep_itemAdapter(RegFrame adaptee) {
    this.adaptee = adaptee;
  }

  public void itemStateChanged(ItemEvent e) {
    adaptee.jcbdep_itemStateChanged(e);
  }
}

class RegFrame_jButton2_actionAdapter
    implements ActionListener {
  private RegFrame adaptee;
  RegFrame_jButton2_actionAdapter(RegFrame adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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