java實(shí)現(xiàn)登錄注冊(cè)界面
本文實(shí)例為大家分享了java實(shí)現(xiàn)登錄注冊(cè)界面的具體代碼,供大家參考,具體內(nèi)容如下
數(shù)據(jù)庫(kù)設(shè)計(jì)
既然只是一個(gè)登錄和注冊(cè)的界面,數(shù)據(jù)庫(kù)方面就只設(shè)計(jì)一個(gè)Admin表,表內(nèi)有三個(gè)值。
- id就存登錄所需要的賬號(hào);
- name存名字;
- password存儲(chǔ)密碼
Admin.java
這個(gè)類代表用戶的實(shí)體類,包含三個(gè)變量,并對(duì)其進(jìn)行封裝
private String id;? ? ? ?//帳號(hào) private String name; ? ? ? ? ? //姓名 private String password; ? ? ?//密碼
Login_Register.java
主程序的入口,創(chuàng)建一個(gè)JFrame窗口,窗口包括兩個(gè)待輸入的文本框,以及登錄和注冊(cè)兩個(gè)按鈕。
其中代碼框使用JPasswordField類,這樣就會(huì)使密碼文本框中的內(nèi)容顯示星號(hào)。
為登錄和注冊(cè)加監(jiān)聽器。
Login.java
在Login_Register中點(diǎn)擊登錄按鈕后就會(huì)創(chuàng)建一個(gè)新的Login類,該類中會(huì)有一個(gè)JudgeAdmin方法,用于連接數(shù)據(jù)庫(kù),判斷賬號(hào)密碼是否正確。
如果賬號(hào)正確,會(huì)彈出登錄成功的窗口,否則彈出賬號(hào)或密碼錯(cuò)誤的窗口。
AdminRegister.java
用戶注冊(cè)的圖形化界面,包含四個(gè)文本框和一個(gè)注冊(cè)按鈕。當(dāng)點(diǎn)擊注冊(cè)按鈕時(shí),會(huì)創(chuàng)建一個(gè)新的Register類,把文本框中的變量傳入Register類。
Register.java
用于判斷傳來的數(shù)據(jù)是否符合規(guī)則,并向數(shù)據(jù)庫(kù)添加新用戶,
當(dāng)用戶名和賬號(hào)為空時(shí),會(huì)彈出相應(yīng)的窗口。
并且要求密碼框和確認(rèn)密碼框中的密碼完全一致,否則不能注冊(cè)。
如果所有的條件都滿足,向數(shù)據(jù)庫(kù)中添加數(shù)據(jù),并彈出注冊(cè)成功的窗口。
代碼
Admin.java
package src; /* 管理員實(shí)體 */ public class Admin { ?? ?private String id; ? ? ? ? ? ? ? ? //編號(hào) ?? ?private String name; ? ? ? ? ? //姓名 ?? ?private String password; ? ? ?//密碼 ?? ?void setID(String id) { ?? ? ? ?this.id=id; ?? ?} ?? ?void setName(String name) { ?? ? ? ?this.name=name; ?? ?} ?? ?void setPassword(String password) { ?? ? ? ?this.password=password; ?? ?} ?? ? ?? ?String getID() { ?? ? ? ?return this.id; ?? ?} ?? ?String getName() { ?? ? ? ?return this.name; ?? ?} ?? ?String getPassword() { ?? ? ? ?return this.password; ?? ?} }
Login_Register.java
package src; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Login_Register extends JFrame{ ? ? ?? ? ?? ? ?? ?Login_Register() { ?? ??? ?init(); ?? ?} ?? ?//登錄界面初始化 ?? ?public void init() { ?? ?JFrame frame = new JFrame("登錄"); ? ? ? ? frame.setLayout(null); ? ? ? ?? ? ? ? ? JLabel nameStr = new JLabel("賬號(hào):"); ? ? ? ? nameStr.setBounds(250, 200, 100, 25); ? ? ? ? frame.add(nameStr); ? ? ? ?? ? ? ? ? JLabel passwordStr = new JLabel("密碼:"); ? ? ? ? passwordStr.setBounds(250, 250, 100, 25); ? ? ? ? frame.add(passwordStr); ? ? ? ? ?? ? ? ? ? JTextField userID = new JTextField(); ? ? ? ? userID.setBounds(300, 200, 150, 25); ? ? ? ? frame.add(userID); ? ? ? ?? ? ? ? ? JPasswordField password = new JPasswordField(); ? ? ? ? password.setBounds(300, 250, 150, 25); ? ? ? ? frame.add(password); ? ? ? ?? ? ? ? ? JButton buttonlogin = new JButton("登錄"); ? ? ? ? buttonlogin.setBounds(275, 300, 70, 25); ? ? ? ? frame.add(buttonlogin); ? ? ? ?? ? ? ? ? JButton buttonregister = new JButton("注冊(cè)"); ? ? ? ? buttonregister.setBounds(375, 300, 70, 25); ? ? ? ? frame.add(buttonregister); ? ? ? ? ?? ? ? ? ? frame.setBounds(400, 100, 800, 640); ? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ? ? ? ? frame.setVisible(true); ? ? ? ?? ? ? ? ? //為登錄按鈕添加監(jiān)聽器 ? ? ? ? ?buttonlogin.addActionListener(new ActionListener() { ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) { ? ? ? ? ? ? ? ? String ID = userID.getText(); ? ? ? ? ? ? ? ? String passwd = new String (password.getPassword()); ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè)Admin用戶,把輸入框中的用戶名密碼和提出來 ? ? ? ? ? ? ? ? Admin admin = new Admin(); ? ? ? ? ? ? ? ? admin.setID(ID); ? ? ? ? ? ? ? ? admin.setPassword(passwd); ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? //登錄 ? ? ? ? ? ? ? ? Login login = new Login(); ? ? ? ? ? ? ? ? login.setAdmin(admin); ? ? ? ? ?? ? ? ? ? ? ? ? ? if(login.JudgeAdmin()==0) { ? ? ? ? ? ? ? ? ?? ?//彈出賬號(hào)或密碼錯(cuò)誤的窗口 ? ? ? ? ? ? ? ? ?? ?JOptionPane.showMessageDialog(null, "賬號(hào)或密碼錯(cuò)誤", "賬號(hào)或密碼錯(cuò)誤", JOptionPane.WARNING_MESSAGE); ? ? ? ? ? ? ? ? ?? ?//清除密碼框中的信息 ? ? ? ? ? ? ? ? ?? ?password.setText(""); ? ? ? ? ? ? ? ? ?? ?//清除賬號(hào)框中的信息 ? ? ? ? ? ? ? ? ?? ?userID.setText(""); ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ?//System.out.println("登陸失敗"); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ?? ?//彈出登錄成功的窗口 ? ? ? ? ? ? ? ? ?? ?JOptionPane.showMessageDialog(null, "登陸成功", "登陸成功", JOptionPane.NO_OPTION); ? ? ? ? ? ? ? ? ?? ?//點(diǎn)擊確定后會(huì)跳轉(zhuǎn)到主窗口 ? ? ? ? ? ? ? ? ?? ?frame.setVisible(false); ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? ? ? ? ? ? ?//為注冊(cè)按鈕添加監(jiān)聽器 ? ? ? ? ?buttonregister.addActionListener(new ActionListener() { ? ? ? ? ?? ? public void actionPerformed(ActionEvent e) { ? ? ? ? ?? ??? ? //注冊(cè)頁面 ? ? ? ? ? ? ? ? ?frame.setVisible(false); ? ? ? ? ?? ??? ? AdminRegister ar = new AdminRegister();? ? ? ? ? ?? ? } ? ? ? ? ?}); ?? ?} ?? ? ? ? public static void main(String []args) {? ? ? ? ?//主程序 ? ? ? ?//登錄窗口 ? ? ?? ?Login_Register login_register = new Login_Register(); ? ? } }
Login.java
package src; /* 處理用戶登錄 */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Login { ?? ?Admin admin; ?? ? ?? ?void setAdmin(Admin admin) { ?? ??? ?this.admin=admin; ?? ??? ?//System.out.println(this.admin.getPassword()+" ? " + this.admin.getID()); ?? ?} ?? ?/* ?? ? * JudgeAdmin()方法 ?? ? * 判斷Admin的ID和密碼是否正確,如果正確,顯示登錄成功 ?? ? * 如果錯(cuò)誤,彈出一個(gè)窗口,顯示賬號(hào)或密碼錯(cuò)誤 ?? ? */ ?? ?private String driver = "com.mysql.cj.jdbc.Driver"; ? ? private String url = "jdbc:mysql://localhost:3306/hotelsql?serverTimezone=UTC&characterEncoding=utf-8"; ? ? private String user = "root"; ? ? private String password = "12481632"; ?? ? ?? ? public boolean login(Admin admin) throws SQLException, ClassNotFoundException { ?? ? ? ??? ?String sql="select * from admin where id=? and password=?"; ?? ? ? ? ? ? ?? ? ? ??? ?Class.forName(driver); ?? ? ? ??? ?Connection conn = DriverManager.getConnection(url, user, password); ?? ? ? ??? ?PreparedStatement ps = conn.prepareStatement(sql); ?? ? ? ??? ? ?? ? ? ? ? ?ps.setString(1, admin.getID()); ?? ? ? ? ? ?ps.setString(2, admin.getPassword()); ?? ? ? ? ? ?ResultSet rs = ps.executeQuery(); ?? ? ? ? ? ?int ans = 0; ?? ? ? ? ? ?if(rs.next()) { ?? ? ? ? ? ??? ?ans = 1; ?? ? ? ? ? ?} ? ?? ? ? ? ? ?rs.close(); ?? ? ? ? ? ?ps.close(); ?? ? ? ? ? ?conn.close(); ?? ? ? ? ? ?if(ans == 1) { ?? ? ? ? ? ??? ?return true; ?? ? ? ? ? ?} ?? ? ? ? ? ?else return false; ?? ? ? ?} ?? ?int JudgeAdmin() { ?? ??? ? ?? ??? ? ? ?try { ?? ??? ? ? ? ? ?if(login(this.admin)) { ?? ??? ? ? ? ? ??? ?System.out.println("登錄成功"); ?? ??? ? ? ? ? ??? ?return 1; ?? ??? ? ? ? ? ?}else { ?? ??? ? ? ? ? ? ? ?return 0; ?? ??? ? ? ? ? ?} ?? ??? ? ? ?}catch(Exception e) { ?? ??? ? ? ? ? ?//e.printStackTrace(); ?? ??? ? ? ??? ?//System.out.println("!!!!!!!!!"); ?? ??? ? ? ?} ?? ??? ?return 0; ?? ??? ? ?? ?}?? ? }
AdminRegister.java
package src; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; /* ?* 管理員注冊(cè)界面 ?*? ?*/ public class AdminRegister extends JFrame{ ?? ?AdminRegister () { ?? ??? ?init(); ?? ?} ?? ?void init() { ? ? ? ? ? ? JFrame frame = new JFrame("注冊(cè)管理員賬號(hào)"); ? ? ? ? ? ? frame.setLayout(null); ? ? ? ? ? ?? ? ? ? ? ? ? JLabel nameStr = new JLabel("用戶名:"); ? ? ? ? ? ? nameStr.setBounds(250, 150, 100, 25); ? ? ? ? ? ? frame.add(nameStr); ? ? ? ?? ? ? ? ? ? ? JLabel IDStr = new JLabel("賬號(hào):"); ? ? ? ? ? ? IDStr.setBounds(250, 200, 100, 25); ? ? ? ? ? ? frame.add(IDStr); ? ? ? ? ? ? JLabel passwordStr = new JLabel("密碼:"); ? ? ? ? ? ? passwordStr.setBounds(250, 250, 100, 25); ? ? ? ? ? ? frame.add(passwordStr); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JLabel confrimStr = new JLabel("確認(rèn)密碼:"); ? ? ? ? ? ? confrimStr.setBounds(250, 300, 100, 30); ? ? ? ? ? ? frame.add(confrimStr); ? ? ? ? ? ?? ? ? ? ? ? ? JTextField userName = new JTextField(); ? ? ? ? ? ? userName.setBounds(320, 150, 150, 25); ? ? ? ? ? ? frame.add(userName); ? ? ? ? ? ? JTextField userID = new JTextField(); ? ? ? ? ? ? userID.setBounds(320, 200, 150, 25); ? ? ? ? ? ? frame.add(userID); ? ? ? ? ? ? JPasswordField password = new JPasswordField(); ? ? ? ? ? ? password.setBounds(320, 250, 150, 25); ? ? ? ? ? ? frame.add(password); ? ? ? ? ? ? JPasswordField confrimPassword = new JPasswordField(); ? ? ? ? ? ? confrimPassword.setBounds(320, 300, 150, 25); ? ? ? ? ? ? frame.add(confrimPassword); ? ? ? ? ? ?? ? ? ? ? ? ? JButton buttonregister = new JButton("注冊(cè)"); ? ? ? ? ? ? buttonregister.setBounds(350, 350, 70, 25); ? ? ? ? ? ? frame.add(buttonregister); ? ? ? ? ? ?? ? ? ? ? ? ? frame.setBounds(400, 100, 800, 640); ? ? ? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ? ? ? ? ? ? frame.setVisible(true); ? ? ? ? ? //為注冊(cè)按鈕增加監(jiān)聽器 ? ? ? ? ? ? buttonregister.addActionListener(new ActionListener() { ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) { ? ? ? ? ? ? ? ? ? ? String name = userName.getText(); ? ? ? ? ? ? ? ? ? ? String ID = userID.getText(); ? ? ? ? ? ? ? ? ? ? String passwd = new String (password.getPassword()); ? ? ? ? ? ? ? ? ? ? String confrimpasswd = new String (confrimPassword.getPassword()); ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? //創(chuàng)建Register類 ? ? ? ? ? ? ? ? ? ? Register register = new Register(); ? ? ? ? ? ? ? ? ? ? register.setID(ID); ? ? ? ? ? ? ? ? ? ? register.setName(name); ? ? ? ? ? ? ? ? ? ? register.setPassword(passwd); ? ? ? ? ? ? ? ? ? ? register.setconfirmpasswd(confrimpasswd); ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? //如果注冊(cè)成功,返回登錄界面 ? ? ? ? ? ? ? ? ? ? try { ?? ??? ??? ??? ??? ??? ?if(register.JudgeRegister()) { ?? ??? ??? ??? ??? ??? ? ? ?frame.setVisible(false); ?? ??? ??? ??? ??? ??? ? ? ?Login_Register login_register = new Login_Register(); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} catch (SQLException e1) { ?? ??? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ??? ?//e1.printStackTrace(); ?? ??? ??? ??? ??? ?} catch (ClassNotFoundException e1) { ?? ??? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ??? ?e1.printStackTrace(); ?? ??? ??? ??? ??? ?} ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ?? ? ? ? ? ? ? }); ?? ?} }
Register.java
package src; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.swing.JOptionPane; public class Register { ? ? String name; ? ? String ID; ? ? String password; ? ? String confirmpassword; ? ?? ? ? private String driver = "com.mysql.cj.jdbc.Driver"; ? ? private String url = "jdbc:mysql://localhost:3306/hotelsql?serverTimezone=UTC&characterEncoding=utf-8"; ? ? private String user = "root"; ? ? private String sqlpassword = "12481632"; ? ?? ? ? void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? void setID(String ID) { ? ? ? ? this.ID = ID; ? ? } ? ? void setPassword(String password) { ? ? ? ? this.password = password; ? ? } ? ? void setconfirmpasswd(String confirmpassword) { ? ? ? ? this.confirmpassword = confirmpassword; ? ? } ? ?? ? ?? ? ? //判斷注冊(cè)的賬號(hào)是否符合規(guī)則 ? ? boolean JudgeRegister() throws SQLException, ClassNotFoundException { ? ? ? ?? ? ? ? ? if(this.name.equals("")) { ? ? ? ? ? ? JOptionPane.showMessageDialog(null, "用戶名不能為空!", "用戶名", JOptionPane.ERROR_MESSAGE); ? ? ? ? ? ? return false; ? ? ? ? } ? ? ? ?? ? ? ? ? if(this.ID.equals("")) { ? ? ? ? ? ? JOptionPane.showMessageDialog(null, "賬號(hào)不能為空!", "賬號(hào)為空", JOptionPane.ERROR_MESSAGE); ? ? ? ? ? ? return false; ? ? ? ? } ? ? ? ?? ? ? ? ? if(this.password.equals("")) { ? ? ? ? ? ? JOptionPane.showMessageDialog(null, "密碼不能為空!", "密碼為空", JOptionPane.ERROR_MESSAGE); ? ? ? ? ? ? return false; ? ? ? ? } ? ? ? ?? ? ? ? ? if(!this.password.equals(this.confirmpassword)) { ? ? ? ? ? ? JOptionPane.showMessageDialog(null, "兩次輸入的密碼不一致!", "密碼不一致", JOptionPane.ERROR_MESSAGE); ? ? ? ? ? ? return false; ? ? ? ? } ? ? ? ?? ? ? ? ? //符合規(guī)則,彈出注冊(cè)成功的窗口,并將賬號(hào)添加數(shù)據(jù)庫(kù) ? ? ? ? JOptionPane.showMessageDialog(null, "注冊(cè)成功"); ? ? ? ? addAdmin(); ? ? ? ? return true; ? ? } ? ?? ? ? //向數(shù)據(jù)庫(kù)添加Admin賬戶 ? ? void addAdmin() throws ClassNotFoundException, SQLException { ? ? ?? ?String sql="insert into admin (id, name, password) values (?,?,?)"; ? ? ?? ?Class.forName(driver); ? ? ?? ?try { ?? ? ? ??? ?Connection conn = DriverManager.getConnection(url, user, sqlpassword); ?? ? ? ??? ?PreparedStatement ps = conn.prepareStatement(sql); ?? ? ? ??? ?ps.setString(1, this.ID); ?? ? ? ? ? ?ps.setString(2, this.name); ?? ? ? ? ? ?ps.setString(3, this.password); ?? ? ? ? ? ?ps.executeUpdate(); ?? ? ? ? ? ?ps.close();?? ? ?? ? ? ? ? ?conn.close(); ?? ? ? ? ? ? ? ? ?? ?}catch(SQLException ex) { ? ? ?? ??? ?System.out.println("添加用戶失?。?); ? ? ?? ?} ? ? ?? ? ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)登錄與注冊(cè)頁面
- javaweb實(shí)現(xiàn)注冊(cè)登錄頁面
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Java+mysql用戶注冊(cè)登錄功能
- JAVA簡(jiǎn)單實(shí)現(xiàn)MD5注冊(cè)登錄加密實(shí)例代碼
- Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
- JavaWeb簡(jiǎn)單用戶登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)
- Java簡(jiǎn)易登錄注冊(cè)小程序
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能(服務(wù)器)
相關(guān)文章
springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫(kù)的更新批處理方式
這篇文章主要介紹了springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫(kù)的更新批處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03JavaWeb 簡(jiǎn)單分頁實(shí)現(xiàn)代碼
這篇文章主要介紹了JavaWeb 簡(jiǎn)單分頁實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11java微信公眾號(hào)開發(fā)(搭建本地測(cè)試環(huán)境)
這篇文章主要介紹了java微信公眾號(hào)開發(fā),主要內(nèi)容有測(cè)試公眾號(hào)與本地測(cè)試環(huán)境搭建,需要的朋友可以參考下2015-12-12SpringAOP 構(gòu)造注入的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringAOP_構(gòu)造注入的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-05-05Spring-data-JPA使用時(shí)碰到的問題以及解決方案
這篇文章主要介紹了Spring-data-JPA使用時(shí)碰到的問題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12