📄 admadd.java~5~
字号:
package bookmanage;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
* <p>Title: 图书管理系统</p>
*
* <p>Description: AdmAdd类继承JDialog,主要是用来增加管理员</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: 浙江工业大学信息学院计算机系</p>
*
* @author 曾文秋
* @version 1.0
*/
public class AdmAdd
extends JDialog {
TextVerifier tv=new TextVerifier(10);//限制文本框中最多只能输入10个字符
TextVerifier tv1=new TextVerifier(15);
TextVerifier tv2=new TextVerifier(15);
JLabel jLabel1 = new JLabel();
JTextField jTextFieldName = new JTextField();//用户名
JLabel jLabel2 = new JLabel();
JComboBox jComboBoxType = new JComboBox();//用户类型
JPasswordField jTextFieldPassword = new JPasswordField();//密码
JLabel jLabel3 = new JLabel();
JPasswordField jTextFieldAffirmPassword = new JPasswordField();//确认密码
JLabel jLabel4 = new JLabel();
JButton jButtonAdd = new JButton();
JButton jButtonCancel = new JButton();
//定义结果集
ResultSet rs;
//定义数据库操作对象
private DBManager db = new DBManager();
public AdmAdd(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
public AdmAdd() {
this(new Frame(), "AdmAdd", false);
}
/**
* 初始化界面
* @throws Exception
*/
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jLabel1.setText("用户名:");
jLabel1.setBounds(new Rectangle(56, 24, 56, 23));
jTextFieldName.setDocument(tv);
jLabel2.setText("用户类型:");
jLabel2.setBounds(new Rectangle(56, 72, 62, 23));
jComboBoxType.setBounds(new Rectangle(145, 74, 89, 23));
jComboBoxType.addItem("超级管理员");
jComboBoxType.addItem("管理员");
jTextFieldPassword.setDocument(tv1);
jTextFieldPassword.setBounds(new Rectangle(145, 119, 89, 23));
jLabel3.setText("密码:");
jLabel3.setBounds(new Rectangle(56, 119, 56, 23));
jTextFieldAffirmPassword.setDocument(tv2);
jTextFieldAffirmPassword.setBounds(new Rectangle(145, 167, 89, 23));
jLabel4.setText("确认密码:");
jLabel4.setBounds(new Rectangle(56, 167, 63, 23));
jButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonAdd_actionPerformed(e);
}
});
jButtonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonCancel_actionPerformed(e);
}
});
this.setTitle("增加管理员");
jButtonCancel.setBounds(new Rectangle(152, 215, 81, 23));
jButtonCancel.setText("取消");
jButtonAdd.setBounds(new Rectangle(56, 215, 81, 23));
jButtonAdd.setText("增加");
this.getContentPane().add(jLabel1);
this.getContentPane().add(jTextFieldName);
this.getContentPane().add(jButtonCancel);
this.getContentPane().add(jButtonAdd);
this.getContentPane().add(jTextFieldAffirmPassword);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jTextFieldPassword);
this.getContentPane().add(jComboBoxType);
this.getContentPane().add(jLabel2);
jTextFieldName.setBounds(new Rectangle(145, 24, 89, 23));
}
public void jButtonAdd_actionPerformed(ActionEvent e) {
String Name=null;
String Password=null;
String AffirmPassword=null;
String Type=null;
Name=jTextFieldName.getText().trim();
Password=new String(jTextFieldPassword.getPassword()).trim();
AffirmPassword=new String(jTextFieldAffirmPassword.getPassword()).trim();
Type=jComboBoxType.getSelectedItem().toString().trim();
String strSQL;
//校验用户名是否为空
if (Name.equals("")) {
JOptionPane.showMessageDialog(AdmAdd.this, "用户名不许为空!");
return;
}
//校验密码是否为空
if (Password.equals("")) {
JOptionPane.showMessageDialog(AdmAdd.this, "密码不许为空!");
return;
}
//校验两次输入的密码是否一致
if (!Password.equals(AffirmPassword)) {
JOptionPane.showMessageDialog(AdmAdd.this, "两次输入的密码不一致!");
return;
}
//生成sql操作语句,查询要添加的用户名是否已经存在,若存在执行删除,若不存在提示并返回
strSQL = "select * from administrator where id='" + Name + "'";
rs = db.getResult(strSQL);
boolean isexist = false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
}
//若用户名存在,提示警告信息
if (isexist){
JOptionPane.showMessageDialog(AdmAdd.this, "用户名已存在!");
return;
}
else {
//然后执行插入操作
strSQL = "insert into administrator(id,password,power) values('";
strSQL = strSQL + Name + "','";
strSQL = strSQL + Password + "','";
strSQL = strSQL + Type + "')";
//由数据库操作对象执行数据库操作,并返回操作成功失败的提示信息
if (db.executeSql(strSQL)) {
JOptionPane.showMessageDialog(null, "成功添加!");
}
else {
JOptionPane.showMessageDialog(null, " 添加失败,请重新操作!");
}
}
}
public void jButtonCancel_actionPerformed(ActionEvent e) {
this.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -