?? useradd.java
字號:
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.addActionListener(new userAdd_jButtonOK_actionAdapter(this));
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);
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, " 添加失敗,請重新操作!");
}
}
}
public void jButtonOK_actionPerformed(ActionEvent e) {
}
}
class userAdd_jButtonOK_actionAdapter
implements ActionListener {
private userAdd adaptee;
userAdd_jButtonOK_actionAdapter(userAdd adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButtonOK_actionPerformed(e);
}
}
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 + -