JAVA實(shí)現(xiàn)簡(jiǎn)單系統(tǒng)登陸注冊(cè)模塊
前期準(zhǔn)備
首先要先明確有個(gè)大體的思路,要實(shí)現(xiàn)什么樣的功能,了解完成整個(gè)模塊要運(yùn)用到哪些方面的知識(shí),以及從做的過程中去發(fā)現(xiàn)自己的不足。技術(shù)方面的進(jìn)步大都都需要從實(shí)踐中出來的。
功能:用戶注冊(cè)功能+系統(tǒng)登錄功能+生成驗(yàn)證碼
知識(shí):窗體設(shè)計(jì)、數(shù)據(jù)庫設(shè)計(jì)、JavaBean封裝屬性、JDBC實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的連接、驗(yàn)證碼(包括彩色驗(yàn)證碼)生成技術(shù),還有就些比如像使用正則表達(dá)式校驗(yàn)用戶注冊(cè)信息、隨機(jī)獲得字符串、對(duì)文本可用字符數(shù)的控制等
設(shè)計(jì)的模塊預(yù)覽圖:
彩色驗(yàn)證碼預(yù)覽圖:
所用數(shù)據(jù)庫:MySQL
數(shù)據(jù)庫設(shè)計(jì)
創(chuàng)建一個(gè)數(shù)據(jù)庫db_database01,其中包含一個(gè)表格tb_user,用來保存用戶的注冊(cè)的數(shù)據(jù)。
其中包含4個(gè)字段
id int(11)
username varchar(15)
password varchar(20)
email varchar(45)
MySQL語句可以這樣設(shè)計(jì):
create schema db_database01; use db_database01; create table tb_user( id int(11) not null auto_increment primary key, username varchar(15) not null, password varchar(20) not null, email varchar(45) not null ); insert into tb_user values(1,"lixiyu","lixiyu",lixiyu419@gmail.com);
這樣把lixiyu作為用戶名。
select語句檢查一下所建立的表格:
編寫JavaBean封裝用戶屬性
package com.lixiyu.model; public class User { private int id;// 編號(hào) private String username;// 用戶名 private String password;// 密碼 private String email;// 電子郵箱 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
編寫JDBC工具類
將與數(shù)據(jù)庫操作相關(guān)的代碼放置在DBConfig接口和DBHelper類中
DBConfig接口用于保存數(shù)據(jù)庫、用戶名和密碼信息
代碼:
package com.lixiyu.util; public interface DBConfig { String databaseName = "db_database01";// 數(shù)據(jù)庫名稱 String username = "root";// 數(shù)據(jù)庫用戶名 String password = "lixiyu";// 數(shù)據(jù)庫密碼 }
為簡(jiǎn)化JDBC開發(fā),DBHelper使用了了Commons DbUtil組合。
DBHelper類繼承了DBConfig接口,該類中包含4種方法:
(1)getConnection()方法:獲得數(shù)據(jù)庫連接,使用MySQL數(shù)據(jù)源來簡(jiǎn)化編程,避免因加載數(shù)據(jù)庫驅(qū)動(dòng)而發(fā)生異常。
(2)exists()方法:判斷輸入的用戶名是否存在。
(3)check()方法:當(dāng)用戶輸入用戶名和密碼,查詢使用check()方法是否正確。
(4)save()方法:用戶輸入合法注冊(cè)信息后,,將信息進(jìn)行保存。
詳細(xì)代碼:
package com.lixiyu.util; import java.sql.Connection; import java.sql.SQLException; import java.util.Arrays; import java.util.List; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import org.apache.commons.lang.StringEscapeUtils; import com.lixiyu.model.User; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; public class DBHelper implements DBConfig { /* * 使用MySQL數(shù)據(jù)源獲得數(shù)據(jù)庫連接對(duì)象 * * @return:MySQL連接對(duì)象,如果獲得失敗返回null */ public static Connection getConnection() { MysqlDataSource mds = new MysqlDataSource();// 創(chuàng)建MySQL數(shù)據(jù)源 mds.setDatabaseName(databaseName);// 設(shè)置數(shù)據(jù)庫名稱 mds.setUser(username);// 設(shè)置數(shù)據(jù)庫用戶名 mds.setPassword(password);// 設(shè)置數(shù)據(jù)庫密碼 try { return mds.getConnection();// 獲得連接 } catch (SQLException e) { e.printStackTrace(); } return null;// 如果獲取失敗就返回null } /* * 判斷指定用戶名的用戶是否存在 * * @return:如果存在返回true,不存在或者查詢失敗返回false */ public static boolean exists(String username) { QueryRunner runner = new QueryRunner();// 創(chuàng)建QueryRunner對(duì)象 String sql = "select id from tb_user where username = '" + username + "';";// 定義查詢語句 Connection conn = getConnection();// 獲得連接 ResultSetHandler<List<Object>> rsh = new ColumnListHandler();// 創(chuàng)建結(jié)果集處理類 try { List<Object> result = runner.query(conn, sql, rsh);// 獲得查詢結(jié)果 if (result.size() > 0) {// 如果列表中存在數(shù)據(jù) return true;// 返回true } else {// 如果列表中沒有數(shù)據(jù) return false;// 返回false } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn);// 關(guān)閉連接 } return false;// 如果發(fā)生異常返回false } /* * 驗(yàn)證用戶名和密碼是否正確 使用Commons Lang組件轉(zhuǎn)義字符串避免SQL注入 * * @return:如果正確返回true,錯(cuò)誤返回false */ public static boolean check(String username, char[] password) { username = StringEscapeUtils.escapeSql(username);// 將用戶輸入的用戶名轉(zhuǎn)義 QueryRunner runner = new QueryRunner();// 創(chuàng)建QueryRunner對(duì)象 String sql = "select password from tb_user where username = '" + username + "';";// 定義查詢語句 Connection conn = getConnection();// 獲得連接 ResultSetHandler<Object> rsh = new ScalarHandler();// 創(chuàng)建結(jié)果集處理類 try { String result = (String) runner.query(conn, sql, rsh);// 獲得查詢結(jié)果 char[] queryPassword = result.toCharArray();// 將查詢到得密碼轉(zhuǎn)換成字符數(shù)組 if (Arrays.equals(password, queryPassword)) {// 如果密碼相同則返回true Arrays.fill(password, '0');// 清空傳入的密碼 Arrays.fill(queryPassword, '0');// 清空查詢的密碼 return true; } else {// 如果密碼不同則返回false Arrays.fill(password, '0');// 清空傳入的密碼 Arrays.fill(queryPassword, '0');// 清空查詢的密碼 return false; } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn);// 關(guān)閉連接 } return false;// 如果發(fā)生異常返回false } /* * 保存用戶輸入的注冊(cè)信息 * * @return:如果保存成功返回true,保存失敗返回false */ public static boolean save(User user) { QueryRunner runner = new QueryRunner();// 創(chuàng)建QueryRunner對(duì)象 String sql = "insert into tb_user (username, password, email) values (?, ?, ?);";// 定義查詢語句 Connection conn = getConnection();// 獲得連接 Object[] params = { user.getUsername(), user.getPassword(), user.getEmail() };// 獲得傳遞的參數(shù) try { int result = runner.update(conn, sql, params);// 保存用戶 if (result > 0) {// 如果保存成功返回true return true; } else {// 如果保存失敗返回false return false; } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn);// 關(guān)閉連接 } return false;// 如果發(fā)生異常返回false } }
系統(tǒng)登錄
1.1窗體設(shè)計(jì)
使用BoxLayout布局,將控件排列方式設(shè)置從上至下:
窗體使用了標(biāo)簽、文本域、密碼域和按鈕等控件
實(shí)現(xiàn)代碼:
public class login extends JFrame{ private static final long serialVersionUID = -4655235896173916415L; private JPanel contentPane; private JTextField usernameTextField; private JPasswordField passwordField; private JTextField validateTextField; private String randomText; public static void main(String args[]){ try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable(){ public void run(){ try{ login frame=new login(); frame.setVisible(true); }catch(Exception e){ e.printStackTrace(); } } }); } public login(){ setTitle("系統(tǒng)登錄"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane=new JPanel(); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS)); JPanel usernamePanel=new JPanel(); contentPane.add(usernamePanel); JLabel usernameLable=new JLabel("\u7528\u6237\u540D\uFF1A"); usernameLable.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); usernamePanel.add(usernameLable); usernameTextField=new JTextField(); usernameTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); usernamePanel.add(usernameTextField); usernameTextField.setColumns(10); JPanel passwordPanel = new JPanel(); contentPane.add(passwordPanel); JLabel passwordLabel = new JLabel("\u5BC6 \u7801\uFF1A"); passwordLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); passwordPanel.add(passwordLabel); passwordField = new JPasswordField(); passwordField.setColumns(10); passwordField.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); passwordPanel.add(passwordField); JPanel validatePanel = new JPanel(); contentPane.add(validatePanel); JLabel validateLabel = new JLabel("\u9A8C\u8BC1\u7801\uFF1A"); validateLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); validatePanel.add(validateLabel); validateTextField = new JTextField(); validateTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); validatePanel.add(validateTextField); validateTextField.setColumns(5); randomText = RandomStringUtils.randomAlphanumeric(4); CAPTCHALabel label = new CAPTCHALabel(randomText);//隨機(jī)驗(yàn)證碼 label.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); validatePanel.add(label); JPanel buttonPanel=new JPanel(); contentPane.add(buttonPanel); JButton submitButton=new JButton("登錄"); submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { do_submitButton_actionPerformed(e); } }); submitButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); buttonPanel.add(submitButton); JButton cancelButton=new JButton("退出"); cancelButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ do_cancelButton_actionPerformed(e); } }); cancelButton.setFont(new Font("微軟雅黑",Font.PLAIN,15)); buttonPanel.add(cancelButton); pack();// 自動(dòng)調(diào)整窗體大小 setLocation(com.lixiyu.util.SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示 }
窗體居中顯示:
public class SwingUtil { /* * 根據(jù)容器的大小,計(jì)算居中顯示時(shí)左上角坐標(biāo) * * @return 容器左上角坐標(biāo) */ public static Point centreContainer(Dimension size) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// 獲得屏幕大小 int x = (screenSize.width - size.width) / 2;// 計(jì)算左上角的x坐標(biāo) int y = (screenSize.height - size.height) / 2;// 計(jì)算左上角的y坐標(biāo) return new Point(x, y);// 返回左上角坐標(biāo) } }
1.2獲取及繪制驗(yàn)證碼
public class CAPTCHALabel extends JLabel { private static final long serialVersionUID = -963570191302793615L; private String text;// 用于保存生成驗(yàn)證圖片的字符串 public CAPTCHALabel(String text) { this.text = text; setPreferredSize(new Dimension(60, 36));// 設(shè)置標(biāo)簽的大小 } @Override public void paint(Graphics g) { super.paint(g);// 調(diào)用父類的構(gòu)造方法 g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設(shè)置字體 g.drawString(text, 5, 25);// 繪制字符串 } }
*彩色驗(yàn)證碼:
public class ColorfulCAPTCHALabel extends JLabel { private static final long serialVersionUID = -963570191302793615L; private String text;// 用于保存生成驗(yàn)證圖片的字符串 private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };// 定義畫筆顏色數(shù)組 public ColorfulCAPTCHALabel(String text) { this.text = text; setPreferredSize(new Dimension(60, 36));// 設(shè)置標(biāo)簽的大小 } @Override public void paint(Graphics g) { super.paint(g);// 調(diào)用父類的構(gòu)造方法 g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設(shè)置字體 for (int i = 0; i < text.length(); i++) { g.setColor(colors[RandomUtils.nextInt(colors.length)]); g.drawString("" + text.charAt(i), 5 + i * 13, 25);// 繪制字符串 } } }
1.3非空校驗(yàn)
if (username.isEmpty()) {// 判斷用戶名是否為空 JOptionPane.showMessageDialog(this, "用戶名不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (new String(password).isEmpty()) {// 判斷密碼是否為空 JOptionPane.showMessageDialog(this, "密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (validate.isEmpty()) {// 判斷驗(yàn)證碼是否為空 JOptionPane.showMessageDialog(this, "驗(yàn)證碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; }
1.4合法性校驗(yàn)
if (!DBHelper.exists(username)) {// 如果用戶名不存在則進(jìn)行提示 JOptionPane.showMessageDialog(this, "用戶名不存在!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (!DBHelper.check(username, password)) {// 如果密碼錯(cuò)誤則進(jìn)行提示 JOptionPane.showMessageDialog(this, "密碼錯(cuò)誤!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (!validate.equals(randomText)) {// 如果校驗(yàn)碼不匹配則進(jìn)行提示 JOptionPane.showMessageDialog(this, "驗(yàn)證碼錯(cuò)誤!", "警告信息", JOptionPane.WARNING_MESSAGE); return; }
1.5顯示主窗體
EventQueue.invokeLater(new Runnable() { @Override public void run() { try { MainFrame frame = new MainFrame();// 創(chuàng)建主窗體 frame.setVisible(true);// 設(shè)置主窗體可見 } catch (Exception e) { e.printStackTrace(); } } }); dispose();// 將登錄窗體銷毀 }
設(shè)計(jì)主窗體(比較簡(jiǎn)單這個(gè)):
public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設(shè)置單擊關(guān)閉窗體按鈕時(shí)執(zhí)行的操作 setSize(450, 300);// 設(shè)置窗體大小 contentPane = new JPanel();// 創(chuàng)建面板 contentPane.setLayout(new BorderLayout(0, 0));// 設(shè)置面板布局使用邊界布局 setContentPane(contentPane);// 應(yīng)用面板 JLabel tipLabel = new JLabel("恭喜您成功登錄系統(tǒng)!");// 創(chuàng)建標(biāo)簽 tipLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 40));// 設(shè)置標(biāo)簽字體 contentPane.add(tipLabel, BorderLayout.CENTER);// 應(yīng)用標(biāo)簽 setLocation(SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示 }
用戶注冊(cè)
1.1窗體設(shè)計(jì)
public class Register extends JFrame { /** * */ private static final long serialVersionUID = 2491294229716316338L; private JPanel contentPane; private JTextField usernameTextField; private JPasswordField passwordField1; private JPasswordField passwordField2; private JTextField emailTextField; private JLabel tipLabel = new JLabel();// 顯示提示信息 /** * Launch the application. */ public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { @Override public void run() { try { Register frame = new Register(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Register() { setTitle("\u7528\u6237\u6CE8\u518C"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = new JPanel(); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)); JPanel usernamePanel = new JPanel(); contentPane.add(usernamePanel); JLabel usernameLabel = new JLabel("\u7528 \u6237 \u540D\uFF1A"); usernameLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); usernamePanel.add(usernameLabel); usernameTextField = new JTextField(); usernameTextField.setToolTipText("\u8BF7\u8F93\u51655~15\u4E2A\u7531\u5B57\u6BCD\u6570\u5B57\u4E0B\u5212\u7EBF\u7EC4\u6210\u7684\u5B57\u7B26\u4E32"); AbstractDocument doc = (AbstractDocument) usernameTextField.getDocument(); doc.setDocumentFilter(new DocumentSizeFilter(15));// 限制文本域內(nèi)可以輸入字符長(zhǎng)度為15 doc.addDocumentListener(new DocumentSizeListener(tipLabel, 15)); usernameTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); usernamePanel.add(usernameTextField); usernameTextField.setColumns(10); JPanel passwordPanel1 = new JPanel(); contentPane.add(passwordPanel1); JLabel passwordLabel1 = new JLabel("\u8F93\u5165\u5BC6\u7801\uFF1A"); passwordLabel1.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); passwordPanel1.add(passwordLabel1); passwordField1 = new JPasswordField(); doc = (AbstractDocument) passwordField1.getDocument(); doc.setDocumentFilter(new DocumentSizeFilter(20));// 限制密碼域內(nèi)可以輸入字符長(zhǎng)度為20 doc.addDocumentListener(new DocumentSizeListener(tipLabel, 20)); passwordField1.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); passwordField1.setColumns(10); passwordPanel1.add(passwordField1); JPanel passwordPanel2 = new JPanel(); contentPane.add(passwordPanel2); JLabel passwordLabel2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A"); passwordLabel2.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); passwordPanel2.add(passwordLabel2); passwordField2 = new JPasswordField(); doc = (AbstractDocument) passwordField2.getDocument(); doc.setDocumentFilter(new DocumentSizeFilter(20));// 限制密碼域內(nèi)可以輸入字符長(zhǎng)度為20 doc.addDocumentListener(new DocumentSizeListener(tipLabel, 20)); passwordField2.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); passwordField2.setColumns(10); passwordPanel2.add(passwordField2); JPanel emailPanel = new JPanel(); contentPane.add(emailPanel); JLabel emailLabel = new JLabel("\u7535\u5B50\u90AE\u7BB1\uFF1A"); emailLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); emailPanel.add(emailLabel); emailTextField = new JTextField(); doc = (AbstractDocument) emailTextField.getDocument(); doc.setDocumentFilter(new DocumentSizeFilter(45));// 限制文本域內(nèi)可以輸入字符長(zhǎng)度為45 doc.addDocumentListener(new DocumentSizeListener(tipLabel, 45)); emailTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); emailPanel.add(emailTextField); emailTextField.setColumns(10); JPanel buttonPanel = new JPanel(); contentPane.add(buttonPanel); JButton submitButton = new JButton("\u63D0\u4EA4"); submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { do_submitButton_actionPerformed(e); } }); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); tipLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); buttonPanel.add(tipLabel); Component glue = Box.createGlue(); buttonPanel.add(glue); submitButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); buttonPanel.add(submitButton); JButton cancelButton = new JButton("\u53D6\u6D88"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { do_cancelButton_actionPerformed(e); } }); cancelButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15)); buttonPanel.add(cancelButton); pack();// 自動(dòng)調(diào)整窗體大小 setLocation(SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示 }
1.2用DocumentFilter限制文本可用字符數(shù)
public class DocumentSizeFilter extends DocumentFilter { private int maxSize;// 獲得文本的最大長(zhǎng)度 public DocumentSizeFilter(int maxSize) { this.maxSize = maxSize;// 獲得用戶輸入的最大長(zhǎng)度 } @Override public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if ((fb.getDocument().getLength() + string.length()) <= maxSize) {// 如果插入操作完成后小于最大長(zhǎng)度 super.insertString(fb, offset, string, attr);// 調(diào)用父類中的方法 } else { Toolkit.getDefaultToolkit().beep();// 發(fā)出提示聲音 } } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if ((fb.getDocument().getLength() + text.length() - length) <= maxSize) {// 如果替換操作完成后小于最大長(zhǎng)度 super.replace(fb, offset, length, text, attrs);// 調(diào)用父類中的方法 } else { Toolkit.getDefaultToolkit().beep();// 發(fā)出提示聲音 } } }
1.3用DocumentListener接口實(shí)現(xiàn)顯示文本控件已用字符
public class DocumentSizeListener implements DocumentListener { private JLabel tipLabel; private int maxSize; public DocumentSizeListener(JLabel tipLabel, int maxSize) { this.tipLabel = tipLabel; this.maxSize = maxSize; } @Override public void insertUpdate(DocumentEvent e) { setTipText(e); } @Override public void removeUpdate(DocumentEvent e) { setTipText(e); } @Override public void changedUpdate(DocumentEvent e) { setTipText(e); } private void setTipText(DocumentEvent e) { Document doc = e.getDocument();// 獲得文檔對(duì)象 tipLabel.setForeground(Color.BLACK);// 設(shè)置字體顏色 if (doc.getLength() > (maxSize * 4 / 5)) {// 如果已輸入字符長(zhǎng)度大于最大長(zhǎng)度的80% tipLabel.setForeground(Color.RED);// 使用紅色顯示提示信息 } else { tipLabel.setForeground(Color.BLACK);// 使用黑色顯示提示信息 } tipLabel.setText("提示信息:" + doc.getLength() + "/" + maxSize); } }
1.4非空校驗(yàn)
if (username.isEmpty()) {// 判斷用戶名是否為空 JOptionPane.showMessageDialog(this, "用戶名不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (new String(password1).isEmpty()) {// 判斷密碼是否為空 JOptionPane.showMessageDialog(this, "密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (new String(password2).isEmpty()) {// 判斷確認(rèn)密碼是否為空 JOptionPane.showMessageDialog(this, "確認(rèn)密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } if (email.isEmpty()) {// 判斷電子郵箱是否為空 JOptionPane.showMessageDialog(this, "電子郵箱不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE); return; }
1.5使用正則表達(dá)式校驗(yàn)字符串(合法性校驗(yàn))
// 校驗(yàn)用戶名是否合法 if (!Pattern.matches("\\w{5,15}", username)) { JOptionPane.showMessageDialog(this, "請(qǐng)輸入合法的用戶名!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } // 校驗(yàn)兩次輸入的密碼是否相同 if (!Arrays.equals(password1, password2)) { JOptionPane.showMessageDialog(this, "兩次輸入的密碼不同!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } // 校驗(yàn)電子郵箱是否合法 if (!Pattern.matches("\\w+@\\w+\\.\\w+", email)) { JOptionPane.showMessageDialog(this, "請(qǐng)輸入合法的電子郵箱!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } // 校驗(yàn)用戶名是否存在 if (DBHelper.exists(username)) { JOptionPane.showMessageDialog(this, "用戶名已經(jīng)存在", "警告信息", JOptionPane.WARNING_MESSAGE); return; }
1.6保存注冊(cè)信息
User user = new User(); user.setUsername(username); user.setPassword(new String(password1)); user.setEmail(email); Arrays.fill(password1, '0');// 清空保存密碼的字符數(shù)組 Arrays.fill(password2, '0');// 清空保存密碼的字符數(shù)組 if (DBHelper.save(user)) { JOptionPane.showMessageDialog(this, "用戶注冊(cè)成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE); return; } else { JOptionPane.showMessageDialog(this, "用戶注冊(cè)失??!", "警告信息", JOptionPane.WARNING_MESSAGE); return; } }
至此,一個(gè)簡(jiǎn)單而有完整的登陸注冊(cè)模塊就完成了。
以上就是本文的全部?jī)?nèi)容,希望大家可以喜歡。
- java模擬cookie登陸操作
- cookie、session和java過濾器結(jié)合實(shí)現(xiàn)登陸程序
- JavaWeb登陸功能實(shí)現(xiàn)代碼
- JAVA簡(jiǎn)單鏈接Oracle數(shù)據(jù)庫 注冊(cè)和登陸功能的實(shí)現(xiàn)代碼
- java shiro實(shí)現(xiàn)退出登陸清空緩存
- JavaWeb基于Session實(shí)現(xiàn)的用戶登陸注銷方法示例
- Java Web實(shí)現(xiàn)session過期后自動(dòng)跳轉(zhuǎn)到登陸頁功能【基于過濾器】
- java客戶端登陸服務(wù)器用戶名驗(yàn)證
- Java Web開發(fā)過程中登陸模塊的驗(yàn)證碼的實(shí)現(xiàn)方式總結(jié)
- Java Swing中JDialog實(shí)現(xiàn)用戶登陸UI示例
- Java 模擬cookie登陸簡(jiǎn)單操作示例
相關(guān)文章
Java雙向鏈表按照順序添加節(jié)點(diǎn)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java雙向鏈表按照順序添加節(jié)點(diǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Spring框架中一個(gè)有用的小組件之Spring Retry組件詳解
Spring Retry 是從 Spring batch 中獨(dú)立出來的一個(gè)功能,主要實(shí)現(xiàn)了重試和熔斷,對(duì)于那些重試后不會(huì)改變結(jié)果,毫無意義的操作,不建議使用重試,今天通過本文給大家介紹Spring Retry組件詳解,感興趣的朋友一起看看吧2021-07-07java動(dòng)態(tài)口令登錄實(shí)現(xiàn)過程詳解
這篇文章主要介紹了java動(dòng)態(tài)口令登錄實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07詳解IntelliJ IDEA2020.1和JDK14體驗(yàn)
這篇文章主要介紹了詳解IntelliJ IDEA2020.1和JDK14體驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Java使用Filter實(shí)現(xiàn)登錄驗(yàn)證
本文主要介紹了Java使用Filter實(shí)現(xiàn)登錄驗(yàn)證,Filter類似于門衛(wèi),你在進(jìn)入之前門衛(wèi)需要盤查你,身份合法進(jìn)入,身份不合法攔截,感興趣的可以了解一下2023-11-11Java 的 FileFilter文件過濾與readline讀行操作實(shí)例代碼
這篇文章介紹了Java 的 FileFilter文件過濾與readline讀行操作實(shí)例代碼,有需要的朋友可以參考一下2013-09-09Java基礎(chǔ)之spring5新功能學(xué)習(xí)
這篇文章主要介紹了Java基礎(chǔ)之spring5新功能學(xué)習(xí),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05java 高并發(fā)中volatile的實(shí)現(xiàn)原理
這篇文章主要介紹了java 高并發(fā)中volatile的實(shí)現(xiàn)原理的相關(guān)資料,在多線程并發(fā)編程中synchronized和Volatile都扮演著重要的角色,Volatile是輕量級(jí)的synchronized,它在多處理器開發(fā)中保證了共享變量的“可見性”,需要的朋友可以參考下2017-03-03