useradd.java~4~

来自「教材管理系统」· JAVA~4~ 代码 · 共 220 行

JAVA~4~
220
字号
package bookmanager;

import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class userAdd
    extends JFrame {
  //定义结果集
  ResultSet rs;
//定义数据库操作对象
  private DBManager db = new DBManager();

  XYLayout xYLayout1 = new XYLayout();
  JButton jButtonCancel = new JButton();
  JLabel jLabel2 = new JLabel();
  JButton jButtonOK = new JButton();
  JPasswordField jPasswordconfirm = new JPasswordField();
  JLabel jLabel1 = new JLabel();
  JTextField jTextFieldusername = new JTextField();
  JLabel jLabel4 = new JLabel();
  JPasswordField jPassword = new JPasswordField();
  JComboBox jComboBoxpower = new JComboBox();
  JLabel jLabel3 = new JLabel();  JLabel jLabel5 = new JLabel();  JTextField jTextField1 = new JTextField();  JLabel jLabel6 = new JLabel();  JTextField jTextField2 = new JTextField();  JLabel jLabel7 = new JLabel();  JTextField jTextField3 = new JTextField();  JLabel jLabel8 = new JLabel();  JTextField jTextField4 = new JTextField();  JLabel jLabel9 = new JLabel();  JTextField jTextField5 = new JTextField();

  public userAdd() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }

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

  private void jbInit() throws Exception {
    jTextFieldusername.setText("");
    jTextFieldusername.setFont(new java.awt.Font("Dialog", 0, 16));
    jLabel1.setText("用户名");
    jLabel1.setFont(new java.awt.Font("Dialog", 0, 16));
    jPasswordconfirm.setText("");
    jButtonOK.setText("添加");
    jButtonOK.addMouseListener(new userAdd_jButtonOK_mouseAdapter(this));
    jButtonOK.setFont(new java.awt.Font("Dialog", 0, 16));
    jLabel2.setText("密码");
    jLabel2.setFont(new java.awt.Font("Dialog", 0, 16));
    jButtonCancel.setText("取消");
    jButtonCancel.addMouseListener(new userAdd_jButtonCancel_mouseAdapter(this));
    jButtonCancel.setFont(new java.awt.Font("Dialog", 0, 16));
    this.getContentPane().setLayout(xYLayout1);
    xYLayout1.setWidth(447);
    xYLayout1.setHeight(623);
    this.setTitle("添加用户");
    jLabel4.setFont(new java.awt.Font("Dialog", 0, 16));
    jLabel4.setText("登陆权限");
    jPassword.setFont(new java.awt.Font("Dialog", 0, 16));
    jPassword.setText("");
    jComboBoxpower.setFont(new java.awt.Font("Dialog", 0, 16));
    jLabel3.setFont(new java.awt.Font("Dialog", 0, 16));
    jLabel3.setText("确认密码");
    //添加权限下拉框中的可选项

    jComboBoxpower.addItem("普通用户");
    jComboBoxpower.addItem("出入库操作员");
    jComboBoxpower.addItem("管理员");

    jLabel5.setFont(new java.awt.Font("Dialog", 0, 16));    jLabel5.setText("所在专业");    jTextField1.setFont(new java.awt.Font("Dialog", 0, 16));    jTextField1.setText("");    jLabel6.setFont(new java.awt.Font("Dialog", 0, 16));    jLabel6.setText("所属院系");    jTextField2.setFont(new java.awt.Font("Dialog", 0, 16));    jTextField2.setText("");    jLabel7.setFont(new java.awt.Font("Dialog", 0, 16));    jLabel7.setText("联系电话");    jTextField3.setFont(new java.awt.Font("Dialog", 0, 16));    jTextField3.setText("");    jLabel8.setFont(new java.awt.Font("Dialog", 0, 16));    jLabel8.setText("专业人数");    jTextField4.setFont(new java.awt.Font("Dialog", 0, 16));    jTextField4.setText("");    jLabel9.setFont(new java.awt.Font("Dialog", 0, 16));    jLabel9.setText("入学时间");    jTextField5.setFont(new java.awt.Font("Dialog", 0, 16));    jTextField5.setText("");    this.getContentPane().add(jLabel1, new XYConstraints(88, 30, 86, 34));
    this.getContentPane().add(jLabel3, new XYConstraints(85, 141, 91, 36));
    this.getContentPane().add(jLabel2, new XYConstraints(88, 87, 80, 34));
    this.getContentPane().add(jTextFieldusername,
                                new XYConstraints(230, 29, 140, 31));
    this.getContentPane().add(jPassword,  new XYConstraints(230, 84, 140, 32));
    this.getContentPane().add(jPasswordconfirm,
                               new XYConstraints(231, 141, 140, 32));
    this.getContentPane().add(jComboBoxpower,
                               new XYConstraints(230, 196, 141, 27));
    this.getContentPane().add(jLabel4, new XYConstraints(86, 194, 93, 34));    this.getContentPane().add(jTextField1,   new XYConstraints(228, 246, 143, 31));    this.getContentPane().add(jLabel5, new XYConstraints(88, 243, 71, 35));    this.getContentPane().add(jLabel6,   new XYConstraints(89, 294, 83, 32));    this.getContentPane().add(jLabel8,  new XYConstraints(86, 405, 76, 28));    this.getContentPane().add(jButtonCancel, new XYConstraints(252, 533, 94, 30));    this.getContentPane().add(jLabel9, new XYConstraints(90, 454, 75, 32));    this.getContentPane().add(jButtonOK, new XYConstraints(118, 537, 91, 31));    this.getContentPane().add(jTextField2, new XYConstraints(227, 298, 148, 30));    this.getContentPane().add(jTextField3, new XYConstraints(225, 357, 150, 27));    this.getContentPane().add(jTextField4, new XYConstraints(222, 407, 155, 27));    this.getContentPane().add(jTextField5, new XYConstraints(222, 453, 154, 29));    this.getContentPane().add(jLabel7, new XYConstraints(91, 354, 73, 31));

  }

  void jButtonCancel_mouseClicked(MouseEvent e) {
    this.dispose();
  }

//执行插入操作。首先校验输入是否为空,然后检验用户名是否已存在,然后执行插入操作
  void jButtonOK_mouseClicked(MouseEvent e) {
    String strSQL;
    //校验用户名是否为空
    if (jTextFieldusername.getText().trim().equals("")) {
      JOptionPane.showMessageDialog(null, "用户名不许为空!");
      return;
    }
    //校验密码是否为空
    if (jPassword.getText().trim().equals("")) {
      JOptionPane.showMessageDialog(null, "密码不许为空!");
      return;
    }
    //校验两次输入的密码是否一致
    if (!jPassword.getText().trim().equals(jPasswordconfirm.getText().trim())) {
      JOptionPane.showMessageDialog(null, "两次输入的密码不一致!");
      return;
    }

    //生成sql操作语句,查询要添加的用户名是否已经存在,若存在执行删除,若不存在提示并返回
    strSQL = "select * from user where Username='" +
        jTextFieldusername.getText().trim() + "'";
    rs = db.getResult(strSQL);
    boolean isexist = false;
    try {
      isexist = rs.first();
    }
    catch (SQLException ex1) {
    }
    //若用户名存在,提示警告信息
    if (isexist)

    {
      JOptionPane.showMessageDialog(null, "用户名已存在!");
    }
    else {
      //然后执行插入操作
      strSQL = "insert into  user(Username,Password,Power) values('";
      strSQL = strSQL + jTextFieldusername.getText().trim() + "','";

      strSQL = strSQL + jPasswordconfirm.getText().trim() + "','";
      strSQL = strSQL + jComboBoxpower.getSelectedItem().toString().trim() +
          "')";
      //由数据库操作对象执行数据库操作,并返回操作成功失败的提示信息
      if (db.executeSql(strSQL)) {
        JOptionPane.showMessageDialog(null, "成功添加!");
      }
      else {
        JOptionPane.showMessageDialog(null, " 添加失败,请重新操作!");
      }
    }
  }
}

class userAdd_jButtonCancel_mouseAdapter
    extends java.awt.event.MouseAdapter {
  userAdd adaptee;

  userAdd_jButtonCancel_mouseAdapter(userAdd adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseClicked(MouseEvent e) {
    adaptee.jButtonCancel_mouseClicked(e);
  }
}

class userAdd_jButtonOK_mouseAdapter
    extends java.awt.event.MouseAdapter {
  userAdd adaptee;

  userAdd_jButtonOK_mouseAdapter(userAdd adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseClicked(MouseEvent e) {
    adaptee.jButtonOK_mouseClicked(e);
  }
}

⌨️ 快捷键说明

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