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

📄 tjstu.java

📁 这个是学生考试系统,里面有完整的代码加数据库SQL server
💻 JAVA
字号:
package testsystem;
import java.sql.*;
import java.awt.*;

import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class tjStu extends JFrame {
    private ImageIcon im1 = new ImageIcon("img\\forum.gif");
    public tjStu() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        biaoti.setFont(new java.awt.Font("隶书", Font.BOLD, 18));
        getContentPane().setLayout(null);
        jt1.setBounds(new Rectangle(123, 49, 201, 31));
        this.getContentPane().setBackground(new Color(226, 154, 226));
        this.setResizable(false);
        setTitle("添加用户");
        jButton1.setBounds(new Rectangle(149, 202, 108, 39));
        jButton1.setIcon(im1);
        jButton1.setText("添加");
        jButton1.addActionListener(new tjStu_jButton1_actionAdapter(this));
        jr2.setText("学生");
        jr2.setBounds(new Rectangle(268, 150, 59, 35));
        jr1.setText("教师");
        jr1.setBounds(new Rectangle(122, 151, 76, 32));
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("用户名:");
        jLabel1.setBounds(new Rectangle(27, 44, 77, 35));
        jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel2.setText("密 码:");
        jLabel2.setBounds(new Rectangle(27, 95, 73, 34));
        jLabel3.setText("身 份:");
        jLabel3.setBounds(new Rectangle(41, 146, 59, 40));
        biaoti.setHorizontalAlignment(SwingConstants.CENTER);
        biaoti.setText("添  加  用  户");
        biaoti.setBounds(new Rectangle(116, 9, 181, 33));
        jp1.setBounds(new Rectangle(123, 95, 200, 30));
        this.getContentPane().add(jt1);
        this.getContentPane().add(jLabel1);
        this.getContentPane().add(jLabel2);
        this.getContentPane().add(jr1);
        this.getContentPane().add(jr2);
        this.getContentPane().add(jLabel3);
        this.getContentPane().add(jButton1);
        this.getContentPane().add(biaoti);
        this.getContentPane().add(jp1);
        buttonGroup1.add(jr1);
        buttonGroup1.add(jr2);
    }

    public static void main(String[] args) {
        tjStu tjstu = new tjStu();
    }

    JTextField jt1 = new JTextField();
    JRadioButton jr1 = new JRadioButton();
    JRadioButton jr2 = new JRadioButton();
    JButton jButton1 = new JButton();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel biaoti = new JLabel();
    JPasswordField jp1 = new JPasswordField();
    ButtonGroup buttonGroup1 = new ButtonGroup();
    //实现添加用户功能
    public void jButton1_actionPerformed(ActionEvent e) {
        String Name = jt1.getText().trim();                //声明变量获取输入的内容
        String Psw = new String(jp1.getPassword()).trim();
        Connection con =null;
        Statement s1 = null;
        Statement s2 = null;
        Statement s3 = null;
        ResultSet rs1 = null;
        boolean teacher = jr1.isSelected();
        boolean student = jr2.isSelected();
        if((Name.length()!=0&&Psw.length()!=0)&&(teacher||student))
        {
            try
            {
                con=DBcon.link();
                s1=con.createStatement();
                rs1=s1.executeQuery("select * from Login where Name='"+Name+"'");   //查询用户是肉存在
                if(!rs1.next())                 //如果没有查询到继续执行
                {
                    if(teacher)
                    {
                              try
                              {
                                  con=DBcon.link();
                                  s2=con.createStatement();
                                  s2.executeUpdate("insert into Login values('"+Name+"','"+Psw+"','教师')");//插入输入的用户名和密码(教师)
                                  JOptionPane.showMessageDialog(null,"教师用户添加成功!");
                                  int p=JOptionPane.showConfirmDialog(null,"您是否还要添加?","提示",JOptionPane.YES_NO_OPTION);
                                  if(p!=0)
                                  {
                                      this.setVisible(false);
                                  }
                                  else
                                  {
                                      jt1.setText("");
                                      jp1.setText("");
                                      jt1.requestFocus();
                                      return;
                                  }
                              }
                              catch(SQLException ce)
                              {
                                  JOptionPane.showMessageDialog(null,ce.getMessage());
                              }
                   }
                   if(student)
                   {
                       try
                       {
                           con=DBcon.link();
                           s3=con.createStatement();
                           s3.executeUpdate("insert into Login values('"+Name+"','"+Psw+"','学生')");   //插入输入的用户名和密码(学生)
                           JOptionPane.showMessageDialog(null,"学生用户添加成功!");
                           int p=JOptionPane.showConfirmDialog(null,"您是否还要添加?","提示",JOptionPane.YES_NO_OPTION);
                           if(p!=0)
                           {
                               this.setVisible(false);
                           }
                           else
                           {
                               jt1.setText("");
                               jp1.setText("");
                               jt1.requestFocus();
                               return;
                           }
                       }
                       catch(SQLException ct)
                       {
                           JOptionPane.showMessageDialog(null,ct.getMessage());
                       }
                   }

              }
              else
              {
                   JOptionPane.showMessageDialog(null,"用户已经存在,请重新添加!","提示",JOptionPane.ERROR_MESSAGE);  //用户存在不给添加
                   jt1.setText("");
                   jp1.setText("");
                   jt1.requestFocus();
              }
            }
            catch(Exception co)
            {
                co.getMessage();
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null,"信息未填完全,无法添加!");  //信息未填完整则弹出提示
        }
    }
}


class tjStu_jButton1_actionAdapter implements ActionListener {
    private tjStu adaptee;
    tjStu_jButton1_actionAdapter(tjStu adaptee) {
        this.adaptee = adaptee;
    }

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

⌨️ 快捷键说明

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