欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java實(shí)現(xiàn)QQ登錄注冊功能的示例代碼

 更新時(shí)間:2022年05月11日 08:35:53   作者:遇安.112  
這篇文章主要和大家分享如何利用Java語言實(shí)現(xiàn)QQ登錄、注冊等功能。本文主要應(yīng)用的技術(shù)有:GUI、JDBC、多線程等,需要的可以參考一下

前言

本文主要應(yīng)用的技術(shù)有:GUI、JDBC、多線程

實(shí)現(xiàn)的功能具體如下:

1、登錄功能

2、注冊功能

3、是否隱藏密碼的選擇以及實(shí)現(xiàn)功能

4、選擇性別功能

5、密碼與確認(rèn)密碼功能

6、登錄頁面實(shí)時(shí)展示當(dāng)前的時(shí)間

7、當(dāng)?shù)卿洉r(shí)用戶名與密碼在數(shù)據(jù)庫中沒有相匹配的數(shù)據(jù),則會(huì)跳轉(zhuǎn)到注冊頁面上去。

8、同樣,注冊完畢后,數(shù)據(jù)會(huì)運(yùn)用JDBC將數(shù)據(jù)寫入數(shù)據(jù)庫中,然后跳轉(zhuǎn)回登錄頁面。

實(shí)現(xiàn)代碼

登錄頁面

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.time.LocalTime;
 
public class JDBC_登錄功能 {
    public static void main(String[] args) {
        guitext3 gt=new guitext3();
       new Thread(new time1(gt.time)).start();//啟動(dòng)線程
 
    }
}
class guitext3 {
    JFrame jf;
    JLabel l1,l2,time;
    JTextField f1;
    JPasswordField f2;
    JButton jb1,jb2;
    JCheckBox jc;
    public guitext3() {
        jf = new JFrame("QQ登錄窗口");
        jf.setSize(320,250);
        jf.setLocation(700,300);//設(shè)置窗口每次啟動(dòng)都顯示在屏幕正中央
        jf.setLayout(null);
        Font font = new Font("仿宋", Font.BOLD, 20);//設(shè)置統(tǒng)一的字體,讓代碼更加整潔美觀
        l1 = new JLabel("用戶名:");
        l1.setBounds(10,10,100,40);
        l1.setFont(font);
        f1 = new JTextField(null,20);
        f1.setBounds(90,15,180,30);
        f1.setFont(font);
        l2 = new JLabel("密  碼:");
        l2.setBounds(8,50,100,40);
        l2.setFont(font);
        f2=new JPasswordField(null,20);
        f2.setBounds(90,55,180,30);
        f2.setEchoChar('*');//設(shè)置密碼的外顯為*
        f2.setFont(font);
        jc=new JCheckBox("顯示密碼");
        jc.setBounds(230,130,80,40);
        jc.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {//被選中
                    f2.setEchoChar((char) 0);
                } else {
                    f2.setEchoChar('*');
                }
            }
        });
        ActionListener listener=new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
 
            }
        };
        jc.addActionListener(listener);
        jb1 = new JButton("登錄");
        jb1.setBounds(30,100,80,40);
        time=new JLabel();
        time.setBounds(30,150,140,40);
        time.setFont(font);
        //匿名內(nèi)部類
        jb1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                Statement st=null;
                Connection con=null;
                ResultSet rs=null;
                try {
                    //注冊驅(qū)動(dòng)
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    //這里的3306/后跟的是數(shù)據(jù)庫名
                    //獲取數(shù)據(jù)庫連接
                    String url="jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";
                    //通過DriverManager完成注冊
                    con= DriverManager.getConnection(url,"root","123");//你自己的數(shù)據(jù)庫用戶名和密碼
                    //執(zhí)行SQL語句
                    String sql="select * from student";//from 后跟表名
                    st=con.createStatement();
                    rs=st.executeQuery(sql);
            boolean flag=false;
            while(rs.next()){
                //如果輸入的用戶名和密碼與數(shù)據(jù)庫中的用戶和對應(yīng)的密碼相同,則彈出“登錄成功!”的窗口
                if(f1.getText().equals(rs.getString(1))&&f2.getText().equals(rs.getString(2))){
                    JOptionPane.showMessageDialog(null, "登錄成功!");
                    flag=true;//登陸成功后將標(biāo)記改為true方便確認(rèn)
                    break;
                }
            }
            if(flag==false){//如果標(biāo)記為false,則表示用戶名和密碼在數(shù)據(jù)庫中未找到,彈出“登錄失??!請注冊賬戶!”的窗口
                JOptionPane.showMessageDialog(null, "登錄失??!請注冊賬戶!");
                f1.setText(null);//清空用戶欄
                f2.setText(null);//清空密碼欄
                //這個(gè)賬號(hào)不存在,需要注冊,跳轉(zhuǎn)到注冊窗口
                guitext4 gt=new guitext4();
                jf.dispose();//關(guān)閉窗體,釋放所有資源
            }
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jb1.setFont(font);
        jb2 = new JButton("退出");
        jb2.setBounds(150,100,80,40);
        //匿名內(nèi)部類
        jb2.addMouseListener(new MouseAdapter() {
            //重寫鼠標(biāo)點(diǎn)擊事件
            public void mouseClicked(MouseEvent e) {//如果點(diǎn)擊了退出窗口,則彈出“退出成功!”的窗口
                JOptionPane.showMessageDialog(null, "退出成功!");
                //系統(tǒng)退出
                System.exit(0);
            }
        });
        jb2.setFont(font);
        //將這些按鈕和文本等加入到窗體中
        jf.add(l1);
        jf.add(f1);
        jf.add(l2);
        jf.add(f2);
        jf.add(jb1);
        jf.add(jb2);
        jf.add(time);
        jf.add(jc);
        jf.setVisible(true);//讓組件顯示
    }
}
//寫一個(gè)時(shí)間線程類
class time1 implements Runnable{
    JLabel time;//方便傳參
    public time1(JLabel time) {
        this.time = time;
    }
    public void run(){
        while (true) {
            try {
                Thread.sleep(1000);//休眠1秒
                LocalTime time=LocalTime.now();//獲取當(dāng)前時(shí)間
                this.time.setText(time.toString());//設(shè)置JLabel文本
                //SimpleDateFormat t = new SimpleDateFormat ("HH:mm:ss");//格式化時(shí)間,把時(shí)間格式化為時(shí):分:秒
               // this.time.setText(t.format(time));//給標(biāo)簽添加內(nèi)容,即時(shí)間
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

注冊頁面

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
 
public class JDBC_注冊功能 {
    public static void main(String[] args) {
        guitext4 gt=new guitext4();
    }
}
class guitext4{
    JFrame jf;
   // JPanel jp;
    JLabel l1,l2,l3;
    JTextField f1;
    JPasswordField f2,f3;
    JButton jb1,jb2;
    JRadioButton jr1,jr2;
    JCheckBox jc;
    public guitext4() {
        jf = new JFrame("QQ注冊窗口");
        jf.setSize(320,280);//設(shè)置窗體大小
        jf.setLocation(700,300);//設(shè)置窗口每次啟動(dòng)都顯示在屏幕正中央
        jf.setLayout(null);
       // jp = new JPanel();
        Font font = new Font("仿宋", Font.BOLD, 20);//設(shè)置統(tǒng)一的字體,讓代碼更加整潔美觀
        l1 = new JLabel("用戶名:");
        l1.setBounds(10,10,100,40);
        l1.setFont(font);
        f1 = new JTextField(null,20);
        f1.setBounds(90,15,180,30);
        f1.setFont(font);
            l2 = new JLabel("密  碼:");
            l2.setBounds(8, 50, 100, 40);
            l2.setFont(font);
            f2 = new JPasswordField(null, 20);
            f2.setBounds(90, 55, 180, 30);
            f2.setEchoChar('*');//設(shè)置密碼的外顯為*
            f2.setFont(font);
            l3 = new JLabel("確認(rèn)密碼:");
            l3.setBounds(8, 88, 130, 40);
            l3.setFont(font);
            f3 = new JPasswordField(null, 20);
            f3.setBounds(120, 95, 160, 30);
            f3.setEchoChar('*');//設(shè)置密碼的外顯為*
        jc=new JCheckBox("顯示密碼");//創(chuàng)建一個(gè)復(fù)選按鈕
        jc.setBounds(230,130,80,40);
        jc.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {//被選中
                    f2.setEchoChar((char) 0);//顯示原本的數(shù)據(jù)
                    f3.setEchoChar((char)0);//顯示原本的數(shù)據(jù)
                } else {
                    f2.setEchoChar('*');//設(shè)置密碼的外顯為*
                    f3.setEchoChar('*');//設(shè)置密碼的外顯為*
                }
            }
        });
                f3.setFont(font);
                ButtonGroup group = new ButtonGroup();
                //創(chuàng)建兩個(gè)單選按鈕
                jr1 = new JRadioButton("男");
                jr1.setBounds(70, 130, 80, 40);
                jr1.setFont(font);
                jr2 = new JRadioButton("女");
                jr2.setBounds(150, 130, 80, 40);
                jr2.setFont(font);
                //將兩個(gè)單選按鈕加入到同一個(gè)ButtonGroup組中
                group.add(jr1);
                group.add(jr2);
                //實(shí)現(xiàn)監(jiān)聽接口
                ActionListener listener = new AbstractAction() {
                    @Override
                    //actionPerformed==>發(fā)生動(dòng)作時(shí)調(diào)用
                    public void actionPerformed(ActionEvent e) {
 
                    }
                };
                jr1.addActionListener(listener);
                jr2.addActionListener(listener);
                jc.addActionListener(listener);
                jb1 = new JButton("注冊");
                jb1.setBounds(50, 170, 80, 40);
                //匿名內(nèi)部類
                jb1.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        PreparedStatement ps = null;//PreparedStatement==>表示預(yù)編譯的SQL語句的對象。
                        // SQL語句已預(yù)編譯并存儲(chǔ)在PreparedStatement對象中。
                        Connection con = null;//Connection==>與特定數(shù)據(jù)庫的連接
                        try {
                            //注冊驅(qū)動(dòng)
                            Class.forName("com.mysql.cj.jdbc.Driver");
                            //這里的3306/后跟的是數(shù)據(jù)庫名
                            //獲取數(shù)據(jù)庫連接
                            String url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";
                            //通過DriverManager完成注冊
                            con = DriverManager.getConnection(url, "root", "123");//你自己數(shù)據(jù)庫的用戶名和密碼
                            //執(zhí)行SQL語句
                            String sql = "insert into student(username,password,sex) values(?,?,?)";
                            ps = con.prepareStatement(sql);
                            if(f1.getText().length()!=0){
                                //注:這里的所有g(shù)etText()都不能寫成!=null
                                ps.setString(1, f1.getText());
                            }else{
                                JOptionPane.showMessageDialog(null, "姓名不能為空!");//彈出窗口
                            }
                            if(f2.getText().length()!=0) {
                                ps.setString(2, f2.getText());
                            }else{
                                JOptionPane.showMessageDialog(null, "密碼不能為空!");
                            }
                            if(jr1.isSelected()||jr2.isSelected()) {//如果選擇男或者女,就存入數(shù)據(jù)庫。
                                if (jr1.isSelected()) {//isSelected()==>判斷按鈕有沒有被選中
                                    ps.setString(3, jr1.getText());//選中男,則將sex=男添加到數(shù)據(jù)
                                }
                                if (jr2.isSelected()) {
                                    ps.setString(3, jr2.getText());//選中女,則將sex=女添加到數(shù)據(jù)
                                }
                            }else{//否則彈出窗口提醒“請選擇性別!”
                                JOptionPane.showMessageDialog(null, "請選擇性別!");
                            }
                        } catch (ClassNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                        if (new String(f2.getPassword()).equals(new String(f3.getPassword()))) {//如果密碼與確認(rèn)密碼一致
                            if (new String(f2.getPassword()).equals(new String(f3.getPassword()))&&f1.getText().length()!=0&&f2.getText().length()!=0
                                    &&(jr1.isSelected()||jr2.isSelected())) {//如果密碼與確認(rèn)密碼一致,用戶名和密碼不為空并且選擇了性別,則注冊成功
                                JOptionPane.showMessageDialog(null, "注冊成功!");
                                //注冊成功后跳轉(zhuǎn)到登錄窗口
                                guitext3 gt = new guitext3();
                                jf.dispose();//關(guān)閉窗體,釋放所有資源
                                try {
                                    int i = ps.executeUpdate();//將注冊的賬戶存儲(chǔ)到數(shù)據(jù)庫中
                                } catch (SQLException ex) {
                                    ex.printStackTrace();
                                }
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "注冊失?。∶艽a與確認(rèn)密碼不一致!");
                            f2.setText(null);//清空密碼
                            f3.setText(null);//清空確認(rèn)密碼
                        }
                    }
                });
                jb1.setFont(font);
                jb2 = new JButton("退出");
                jb2.setBounds(170, 170, 80, 40);
                //匿名內(nèi)部類
                jb2.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        JOptionPane.showMessageDialog(null, "退出成功!");
                        //系統(tǒng)退出
                        System.exit(0);
                    }
                });
                jb2.setFont(font);
                jf.add(l1);
                jf.add(f1);
                jf.add(l2);
                jf.add(f2);
                jf.add(l3);
                jf.add(f3);
                jf.add(jr1);
                jf.add(jr2);
                jf.add(jc);
                jf.add(jb1);
                jf.add(jb2);
                // jf.add(jp);
                jf.setVisible(true);
            }
        }

效果展示

登錄頁面運(yùn)行結(jié)果

注冊頁面運(yùn)行結(jié)果

是否隱藏密碼效果圖

到此這篇關(guān)于基于Java實(shí)現(xiàn)QQ登錄注冊功能的示例代碼的文章就介紹到這了,更多相關(guān)Java QQ登錄注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Base64算法實(shí)際應(yīng)用之郵件發(fā)送實(shí)例分析

    Java Base64算法實(shí)際應(yīng)用之郵件發(fā)送實(shí)例分析

    這篇文章主要介紹了Java Base64算法實(shí)際應(yīng)用之郵件發(fā)送,結(jié)合實(shí)例形式分析了java字符編碼與郵件發(fā)送相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • Java中MessageFormat的使用詳解

    Java中MessageFormat的使用詳解

    本文主要介紹了Java中MessageFormat的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能

    Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能

    本文給大家介紹Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-12-12
  • Java實(shí)現(xiàn)簡單的模板渲染

    Java實(shí)現(xiàn)簡單的模板渲染

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單的模板渲染的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 使用feign服務(wù)調(diào)用添加Header參數(shù)

    使用feign服務(wù)調(diào)用添加Header參數(shù)

    這篇文章主要介紹了使用feign服務(wù)調(diào)用添加Header參數(shù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java抽象類的概念講解

    Java抽象類的概念講解

    今天小編就為大家分享一篇關(guān)于Java抽象類的概念講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 關(guān)于JpaRepository的關(guān)聯(lián)查詢和@Query查詢

    關(guān)于JpaRepository的關(guān)聯(lián)查詢和@Query查詢

    這篇文章主要介紹了JpaRepository的關(guān)聯(lián)查詢和@Query查詢,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Golang Protocol Buffer案例詳解

    Golang Protocol Buffer案例詳解

    這篇文章主要介紹了Golang Protocol Buffer案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringMVC的最小化配置說明

    SpringMVC的最小化配置說明

    這篇文章主要介紹了SpringMVC的最小化配置說明,Spring MVC是一個(gè)基于Java的Web框架,用于構(gòu)建靈活、高效的Web應(yīng)用程序,它采用了MVC的設(shè)計(jì)模式,將應(yīng)用程序的邏輯分為模型、視圖和控制器三個(gè)部分,以實(shí)現(xiàn)代碼的分離和重用,需要的朋友可以參考下
    2023-10-10
  • java 中迭代器的使用方法詳解

    java 中迭代器的使用方法詳解

    這篇文章主要介紹了java 中迭代器的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09

最新評(píng)論