java實(shí)現(xiàn)登錄注冊界面
本文實(shí)例為大家分享了java實(shí)現(xiàn)登錄注冊界面的具體代碼,供大家參考,具體內(nèi)容如下
數(shù)據(jù)庫設(shè)計(jì)
既然只是一個登錄和注冊的界面,數(shù)據(jù)庫方面就只設(shè)計(jì)一個Admin表,表內(nèi)有三個值。
- id就存登錄所需要的賬號;
- name存名字;
- password存儲密碼
Admin.java
這個類代表用戶的實(shí)體類,包含三個變量,并對其進(jìn)行封裝
private String id;? ? ? ?//帳號 private String name; ? ? ? ? ? //姓名 private String password; ? ? ?//密碼
Login_Register.java
主程序的入口,創(chuàng)建一個JFrame窗口,窗口包括兩個待輸入的文本框,以及登錄和注冊兩個按鈕。
其中代碼框使用JPasswordField類,這樣就會使密碼文本框中的內(nèi)容顯示星號。
為登錄和注冊加監(jiān)聽器。
Login.java
在Login_Register中點(diǎn)擊登錄按鈕后就會創(chuàng)建一個新的Login類,該類中會有一個JudgeAdmin方法,用于連接數(shù)據(jù)庫,判斷賬號密碼是否正確。
如果賬號正確,會彈出登錄成功的窗口,否則彈出賬號或密碼錯誤的窗口。
AdminRegister.java
用戶注冊的圖形化界面,包含四個文本框和一個注冊按鈕。當(dāng)點(diǎn)擊注冊按鈕時,會創(chuàng)建一個新的Register類,把文本框中的變量傳入Register類。
Register.java
用于判斷傳來的數(shù)據(jù)是否符合規(guī)則,并向數(shù)據(jù)庫添加新用戶,
當(dāng)用戶名和賬號為空時,會彈出相應(yīng)的窗口。
并且要求密碼框和確認(rèn)密碼框中的密碼完全一致,否則不能注冊。
如果所有的條件都滿足,向數(shù)據(jù)庫中添加數(shù)據(jù),并彈出注冊成功的窗口。
代碼
Admin.java
package src; /* 管理員實(shí)體 */ public class Admin { ?? ?private String id; ? ? ? ? ? ? ? ? //編號 ?? ?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("賬號:"); ? ? ? ? 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("注冊"); ? ? ? ? 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)建一個Admin用戶,把輸入框中的用戶名密碼和提出來 ? ? ? ? ? ? ? ? Admin admin = new Admin(); ? ? ? ? ? ? ? ? admin.setID(ID); ? ? ? ? ? ? ? ? admin.setPassword(passwd); ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? //登錄 ? ? ? ? ? ? ? ? Login login = new Login(); ? ? ? ? ? ? ? ? login.setAdmin(admin); ? ? ? ? ?? ? ? ? ? ? ? ? ? if(login.JudgeAdmin()==0) { ? ? ? ? ? ? ? ? ?? ?//彈出賬號或密碼錯誤的窗口 ? ? ? ? ? ? ? ? ?? ?JOptionPane.showMessageDialog(null, "賬號或密碼錯誤", "賬號或密碼錯誤", JOptionPane.WARNING_MESSAGE); ? ? ? ? ? ? ? ? ?? ?//清除密碼框中的信息 ? ? ? ? ? ? ? ? ?? ?password.setText(""); ? ? ? ? ? ? ? ? ?? ?//清除賬號框中的信息 ? ? ? ? ? ? ? ? ?? ?userID.setText(""); ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ?//System.out.println("登陸失敗"); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ?? ?//彈出登錄成功的窗口 ? ? ? ? ? ? ? ? ?? ?JOptionPane.showMessageDialog(null, "登陸成功", "登陸成功", JOptionPane.NO_OPTION); ? ? ? ? ? ? ? ? ?? ?//點(diǎn)擊確定后會跳轉(zhuǎn)到主窗口 ? ? ? ? ? ? ? ? ?? ?frame.setVisible(false); ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? ? ? ? ? ? ?//為注冊按鈕添加監(jiān)聽器 ? ? ? ? ?buttonregister.addActionListener(new ActionListener() { ? ? ? ? ?? ? public void actionPerformed(ActionEvent e) { ? ? ? ? ?? ??? ? //注冊頁面 ? ? ? ? ? ? ? ? ?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和密碼是否正確,如果正確,顯示登錄成功 ?? ? * 如果錯誤,彈出一個窗口,顯示賬號或密碼錯誤 ?? ? */ ?? ?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; /* ?* 管理員注冊界面 ?*? ?*/ public class AdminRegister extends JFrame{ ?? ?AdminRegister () { ?? ??? ?init(); ?? ?} ?? ?void init() { ? ? ? ? ? ? JFrame frame = new JFrame("注冊管理員賬號"); ? ? ? ? ? ? frame.setLayout(null); ? ? ? ? ? ?? ? ? ? ? ? ? JLabel nameStr = new JLabel("用戶名:"); ? ? ? ? ? ? nameStr.setBounds(250, 150, 100, 25); ? ? ? ? ? ? frame.add(nameStr); ? ? ? ?? ? ? ? ? ? ? JLabel IDStr = new JLabel("賬號:"); ? ? ? ? ? ? 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("注冊"); ? ? ? ? ? ? buttonregister.setBounds(350, 350, 70, 25); ? ? ? ? ? ? frame.add(buttonregister); ? ? ? ? ? ?? ? ? ? ? ? ? frame.setBounds(400, 100, 800, 640); ? ? ? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ? ? ? ? ? ? frame.setVisible(true); ? ? ? ? ? //為注冊按鈕增加監(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); ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? //如果注冊成功,返回登錄界面 ? ? ? ? ? ? ? ? ? ? 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; ? ? } ? ?? ? ?? ? ? //判斷注冊的賬號是否符合規(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, "賬號不能為空!", "賬號為空", 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ī)則,彈出注冊成功的窗口,并將賬號添加數(shù)據(jù)庫 ? ? ? ? JOptionPane.showMessageDialog(null, "注冊成功"); ? ? ? ? addAdmin(); ? ? ? ? return true; ? ? } ? ?? ? ? //向數(shù)據(jù)庫添加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("添加用戶失??!"); ? ? ?? ?} ? ? ?? ? ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)登錄與注冊頁面
- javaweb實(shí)現(xiàn)注冊登錄頁面
- JavaWeb實(shí)現(xiàn)用戶登錄注冊功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Java+mysql用戶注冊登錄功能
- JAVA簡單實(shí)現(xiàn)MD5注冊登錄加密實(shí)例代碼
- Servlet+JavaBean+JSP打造Java Web注冊與登錄功能
- JavaWeb簡單用戶登錄注冊實(shí)例代碼(有驗(yàn)證碼)
- Java簡易登錄注冊小程序
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能(服務(wù)器)
相關(guān)文章
springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫的更新批處理方式
這篇文章主要介紹了springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫的更新批處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java微信公眾號開發(fā)(搭建本地測試環(huán)境)
這篇文章主要介紹了java微信公眾號開發(fā),主要內(nèi)容有測試公眾號與本地測試環(huán)境搭建,需要的朋友可以參考下2015-12-12SpringAOP 構(gòu)造注入的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringAOP_構(gòu)造注入的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-05-05