📄 useradd.java
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.sql.*;
public class userAdd extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JTextField jTextFieldusername = null;
private JComboBox jComboBoxpower = null;
private JButton jButtonOK = null;
private JButton jButtonCancel = null;
private JPasswordField jPassword = null;
private JPasswordField jPasswordconfirm = null;
ResultSet rs;
private DBManager db =new DBManager();
public userAdd() throws HeadlessException {
// TODO 自动生成构造函数存根
super();
initialize();
}
public userAdd(GraphicsConfiguration arg0) {
super(arg0);
// TODO 自动生成构造函数存根
initialize();
}
public userAdd(String arg0) throws HeadlessException {
super(arg0);
// TODO 自动生成构造函数存根
initialize();
}
public userAdd(String arg0, GraphicsConfiguration arg1) {
super(arg0, arg1);
// TODO 自动生成构造函数存根
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(329, 219);
this.setContentPane(getJContentPane());
this.setTitle("添加用户");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel4 = new JLabel();
jLabel4.setText("登录权限");
jLabel4.setSize(new Dimension(62, 18));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setLocation(new Point(60, 110));
jLabel3 = new JLabel();
jLabel3.setText("确认密码");
jLabel3.setSize(new Dimension(62, 18));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setLocation(new Point(60, 80));
jLabel2 = new JLabel();
jLabel2.setText("密码");
jLabel2.setSize(new Dimension(62, 18));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setLocation(new Point(60, 50));
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(60, 20, 62, 18));
jLabel1.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("用户名");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel3, null);
jContentPane.add(jLabel4, null);
jContentPane.add(getJTextFieldusername(), null);
jContentPane.add(getJComboBoxpower(), null);
jContentPane.add(getJButtonOK(), null);
jContentPane.add(getJButtonCancel(), null);
jContentPane.add(getJPassword(), null);
jContentPane.add(getJPasswordconfirm(), null);
}
return jContentPane;
}
/**
* This method initializes jTextFieldusername
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldusername() {
if (jTextFieldusername == null) {
jTextFieldusername = new JTextField();
jTextFieldusername.setLocation(new Point(162, 20));
jTextFieldusername.setSize(new Dimension(100, 18));
}
return jTextFieldusername;
}
/**
* This method initializes jComboBoxpower
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBoxpower() {
if (jComboBoxpower == null) {
jComboBoxpower = new JComboBox();
jComboBoxpower.setLocation(new Point(162, 110));
jComboBoxpower.setToolTipText("");
jComboBoxpower.setEditable(false);
jComboBoxpower.setSize(new Dimension(100, 18));
jComboBoxpower.addItem("管理员");
jComboBoxpower.addItem("书籍操作员");
jComboBoxpower.addItem("借阅操作员");
jComboBoxpower.addItem("还入操作员");
jComboBoxpower.addItem("读者");
}
return jComboBoxpower;
}
/**
* This method initializes jButtonOK
*
* @return javax.swing.JButton
*/
private JButton getJButtonOK() {
if (jButtonOK == null) {
jButtonOK = new JButton();
jButtonOK.setLocation(new Point(65, 143));
jButtonOK.setText("添加");
jButtonOK.setSize(new Dimension(60, 20));
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
//检验用户名是否为空
if(jTextFieldusername.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "用户名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
//检验密码是否为空
if(jPassword.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if(!jPassword.getText().trim().equals(jPasswordconfirm.getText().trim())){
JOptionPane.showMessageDialog(null, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
String strSQL;
strSQL="select * from alluser where Username="+"'"+jTextFieldusername.getText().trim()+"'";
rs=db.getResult(strSQL);
boolean isexist=false;
try{
isexist=rs.first();
}
catch (SQLException ex1){
}
if(isexist){
JOptionPane.showMessageDialog(null, "用户名已存在", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
else{//执行插入操作
String quanxian=null;
if(jComboBoxpower.getSelectedItem().toString().trim().equals("管理员")) quanxian="admin";
else if(jComboBoxpower.getSelectedItem().toString().trim().equals("书籍操作员")) quanxian="bookop";
else if(jComboBoxpower.getSelectedItem().toString().trim().equals("借阅操作员")) quanxian="borrowop";
else if(jComboBoxpower.getSelectedItem().toString().trim().equals("还入操作员")) quanxian="returnop";
else if(jComboBoxpower.getSelectedItem().toString().trim().equals("读者")) quanxian="reader";
strSQL="insert into alluser(Username,aPassword,aPower) values('";
strSQL=strSQL+jTextFieldusername.getText().trim()+"','";
strSQL=strSQL+jPasswordconfirm.getText().trim()+"','";
strSQL=strSQL+quanxian+"')";System.out.println(strSQL);
//由数据库操作对象,并返回操作成功的提示信息
if(db.executeSql(strSQL)){
JOptionPane.showMessageDialog(null, "添加成功", "成功", JOptionPane.INFORMATION_MESSAGE);
buttonexit();
}
else{
JOptionPane.showMessageDialog(null, "添加失败,请重新操作", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
});
}
return jButtonOK;
}
/**
* This method initializes jButtonCancel
*
* @return javax.swing.JButton
*/
public void buttonexit(){
this.dispose();
}
private JButton getJButtonCancel() {
if (jButtonCancel == null) {
jButtonCancel = new JButton();
jButtonCancel.setLocation(new Point(167, 143));
jButtonCancel.setText("取消");
jButtonCancel.setSize(new Dimension(60, 20));
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
buttonexit();
}
});
}
return jButtonCancel;
}
/**
* This method initializes jPassword
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPassword() {
if (jPassword == null) {
jPassword = new JPasswordField();
jPassword.setLocation(new Point(162, 50));
jPassword.setSize(new Dimension(100, 18));
}
return jPassword;
}
/**
* This method initializes jPasswordconfirm
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPasswordconfirm() {
if (jPasswordconfirm == null) {
jPasswordconfirm = new JPasswordField();
jPasswordconfirm.setLocation(new Point(162, 80));
jPasswordconfirm.setSize(new Dimension(100, 18));
}
return jPasswordconfirm;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -