📄 useradd.java~9~
字号:
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(); 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(332); 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("管理员"); this.getContentPane().add(jButtonOK, new XYConstraints(105, 262, 91, 31)); 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, 120, 31)); this.getContentPane().add(jPassword, new XYConstraints(230, 84, 121, 32)); this.getContentPane().add(jPasswordconfirm, new XYConstraints(231, 141, 120, 32)); this.getContentPane().add(jComboBoxpower, new XYConstraints(230, 196, 121, 27)); this.getContentPane().add(jLabel4, new XYConstraints(86, 194, 93, 34)); this.getContentPane().add(jButtonCancel, new XYConstraints(229, 263, 94, 30)); } 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) ; if(rs != null) {JOptionPane.showMessageDialog(null,"用户名已存在!"); } else { //然后执行插入操作 strSQL="insert into user(username,password,power) values('"; strSQL= strSQL+jTextFieldusername.getText().trim() +"','"; strSQL= strSQL+jComboBoxpower.getSelectedItem().toString().trim() +"','"; strSQL= strSQL +jPasswordconfirm.getText().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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -