java结合数据库实现多人聊天

该博客介绍了如何使用Java编程语言结合MySQL数据库,实现一个多人聊天应用。内容包括登录和注册页面的数据库交互,客户端聊天页面的设计,以及服务器的启动和客户端的连接流程。
摘要由CSDN通过智能技术生成

1.登录页面

package com.ff.chat.chatclient.frame;

import com.ff.chat.chatclient.bean.User;
import com.ff.chat.chatclient.dao.LoginDao;
import com.mysql.cj.jdbc.Driver;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.sql.*;
import java.util.ArrayList;

public class LoginFrame extends JFrame {
   

    JTextField accountField = null;

    //创建窗口
    public void creatFrame(){
   
        this.setTitle("聊天窗口");
        this.setSize(400,400);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        /*
        * 创建一个4行1列的面板
        * */
        JPanel jp = new JPanel(new GridLayout(4,1));

        //欢迎登陆面板
        JPanel welcomePanel = new JPanel();
        JLabel welcomLabel = new JLabel("欢迎登陆");
        welcomePanel.add(welcomLabel);

        //账号面板
        JPanel accountPanel = new JPanel();
        JLabel accountLabel = new JLabel("账号");
        accountField = new JTextField(15);
        accountPanel.add(accountLabel);
        accountPanel.add(accountField);

        //密码面板
        JPanel passwordPanel = new JPanel();
        JLabel passwordLabel = new JLabel("密码");
        JPasswordField passwordField = new JPasswordField(15);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);

        //登录按钮面板
        JPanel btnPanel = new JPanel();
        JButton loginBtn = new JButton("登录");
        JButton regBtn = new JButton("注册");
        btnPanel.add(loginBtn);
        btnPanel.add(regBtn);

        jp.add(welcomePanel);
        jp.add(accountPanel);
        jp.add(passwordPanel);
        jp.add(btnPanel);
        this.add(jp);

        this.setVisible(true);

        //组件绑定事件监听
        loginBtn.addActionListener(new ActionListener() {
   
            @Override
            public void actionPerformed(ActionEvent e) {
   
                //获得账号密码
                String account = accountField.getText();
                String password = new String(passwordField.getPassword());
                if(account.length() == 0){
   
                    JOptionPane.showMessageDialog(null,"账号不能为空",
                            "操作提示",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if(password.length() == 0){
   
                    JOptionPane.showMessageDialog(null,"密码不能为空",
                            "操作提示",JOptionPane.WARNING_MESSAGE);
                    return;
                }

                //与数据库交互
                LoginDao loginDao = new LoginDao();
                try{
   
                    User user = loginDao.checkLogin(account,password);
                    if(user!=null){
   
                        //连接服务器,创建Socket对象
                        try {
   
                            Socket socket = new Socket("127.0.0.1",9998);//该地址为要连接的服务器的地址
                            new ClientFrame(socket,user.getName()).creatFrame();
                            dispose(); //释放当前登录窗口
                        } catch (IOException ioException) {
   
                            ioException.printStackTrace();
                            JOptionPane.showMessageDialog(null,"服务器连接失败",
                                    "操作提示",JOptionPane.WARNING_MESSAGE);
                        }
                    }else {
   
                        JOptionPane.showMessageDialog(null,"密码或账号输入错误!",
                                "操作提示",JOptionPane.WARNING_MESSAGE);
                    }
                }catch (Exception throwables){
   
                    throwables.printStackTrace();
                    JOptionPane.showMessageDialog(null,"数据库连接失败!",
                            "操作提示",JOptionPane.WARNING_MESSAGE);
                }




            }
        });

        //注册绑定事件监听
        regBtn.addActionListener(new ActionListener() {
   
            @Override
            public void actionPerformed(ActionEvent e) {
   
                new RegFrame().creatFrame();
                dispose();
            }
        });
    }


}

与数据库交互部分

package com.ff.chat.chatclient.dao;

import com.ff.chat.chatclient.bean.User;
import com.mysql.cj.jdbc.Driver;


import java.sql.*;


public class LoginDao {
   

    public User checkLogin(String account,String password){
   
        User user = new User();
   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值