• JavaSwing+Mysql实现简单的登录界面+用户是否存在验证


    原生Java+mysql登录验证  

    client

    login.java

    功能:实现登录页面,与服务端传来的数据验证

    package LoginRegister;

    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.regex.Pattern;

    import javax.swing.*;

    public class Login extends JFrame {

    String user;
    String pwd;
    String struser[] = new String[2];
    public Login() {
    setBounds(200, 200, 400, 250);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container c = getContentPane();
    c.setLayout(null);
    setTitle("登录");
    JLabel jl = new JLabel("username");
    jl.setBounds(50, 50, 100, 20);
    JLabel j2 = new JLabel("password");
    j2.setBounds(50, 100, 100, 20);
    JTextField jt = new JTextField();
    jt.setBounds(150, 50, 200, 20);
    JPasswordField jp = new JPasswordField();
    jp.setBounds(150, 100, 200, 20);
    JButton btn = new JButton("登录");
    btn.setBounds(150, 150, 100, 30);
    c.add(jl);
    c.add(jt);
    c.add(j2);
    c.add(jp);
    c.add(btn);
    btn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    user = jt.getText();// 获取用户名;
    pwd = new String(jp.getPassword());// 获取、转换密码;
    if(user!=null&&pwd!=null) {//判断输入的值是否完整
    String data = SendLogin.sendLoginUser(user, pwd);

    Pattern p = Pattern.compile("&");//使用正则表达式,获取
    String[] type = p.split(data);
    String conState=type[0];
    String exsitsState = type[1];

    if(conState.equals("true")&&exsitsState.equals("true")) {
    System.out.println("登录成功!");
    }else {
    if(conState.equals("false")) {
    System.out.println("服务器连接失败!");
    }
    if(exsitsState.equals("false")) {
    System.out.println("帐户不存在!");

    }
    }
    }else {
    System.out.println("请填写完整!");
    }

    }
    });
    }
    public static void main(String[] args) {
    Login login = new Login();
    login.setVisible(true);
    login.setResizable(false);// 设置窗体不可改变大小

    }
    }

    SendLogin.java

    功能:实现客户端的数据传输给服务端,与服务端交换数据。

    package LoginRegister;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class SendLogin {
    public static String sendLoginUser(String username, String password) {
    String data=null;
    try {
    Socket client = new Socket("127.0.0.1", 1100);// 连接服务器;
    System.out.println("连接成功!");
    OutputStream out = client.getOutputStream();
    // String message = "服务器你好,我是客户端";
    String strUser[] = new String[2];
    strUser[0] = username;
    strUser[1] = password;
    String user = "username=" + username + "&" + "password=" + password;
    out.write(user.getBytes());

    InputStream in = client.getInputStream();
    byte bt[] = new byte[1024];
    int len = in.read(bt);
    data = new String(bt, 0, len);
    System.out.println("服务器发来消息:" + data);

    client.close();// 关闭连接;
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return data;
    }

    }

    server

    Server.java

    功能:实现与客户端的数据交互

    package com.login.server;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.regex.Pattern;

    import com.login.mysql.MysqlConnect;

    public class Server {
    public static void main(String[] args) {
    try {
    ServerSocket server = new ServerSocket(1100);//创建服务器套接字,开启1100端口;
    System.out.println("服务器启动成功,等待用户接入……");
    Socket client = server.accept();//等待用户接入;

    InputStream in = client.getInputStream();
    byte bt[] = new byte[1024];
    int len = in.read(bt);
    String data = new String(bt,0,len);
    //System.out.println("客户端发来消息:"+data);


    Pattern a = Pattern.compile("&");//使用正则表达式,获取
    String[] type = a.split(data);

    Pattern b = Pattern.compile("=");//使用正则表达式,获取
    String[] type2 = b.split(type[0]);

    Pattern c = Pattern.compile("=");//使用正则表达式,获取
    String[] type201 = c.split(type[1]);

    String username = type2[1];
    String password = type201[1];



    //System.out.println(username);
    //System.out.println(password);


    OutputStream out = client.getOutputStream();
    String returninfo = MysqlConnect.login(username, password);
    //String message = "客户端你好,我是服务器";
    out.write(returninfo.getBytes());

    client.close();//关闭连接;
    System.out.println("有客户端接入,客户端IP:"+client.getInetAddress());//获取客户端地址;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    MysqlConnect.java

    功能:连接数据库,查询数据

    package com.login.mysql;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class MysqlConnect {
    public static String login(String user,String pwd) {
    String userstate = null;
    String conState="false";
    String infoState="false";

    String userDemo = user;
    String pwdDemo = pwd;
    try {
    Class mysql = Class.forName("com.mysql.jdbc.Driver");
    Connection con = null; // 连接数据库的接口;
    PreparedStatement state = null;// 发送sql语句
    ResultSet rs = null; // 结果集接口;

    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "123456";
    String sql = "SELECT username,password FROM user WHERE username=? and password =?";

    con = DriverManager.getConnection(url, username, password);// 连接数据库;

    if(con!=null) {
    conState = "true";
    }
    state = con.prepareStatement(sql);
    state.setString(1, userDemo);
    state.setString(2, pwdDemo);

    rs = state.executeQuery();
    String exisit = null;
    while(rs.next()) {
    exisit =rs.getString(1);//判断是否有满足条件的一行
    }

    if(exisit!=null) {
    infoState="true";
    }
    //System.out.println(exisit);
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    userstate = conState+"&"+infoState;
    return userstate;
    }
    }

     

  • 相关阅读:
    CF1168B Good Triple 性质分析
    bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
    BZOJ 3940: [Usaco2015 Feb]Censoring AC自动机+栈
    BZOJ 1691 [Usaco2007 Dec]挑剔的美食家 multiset+排序+贪心
    BZOJ 1725: [Usaco2006 Nov]Corn Fields牧场的安排 状压动归
    BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路 Dijkstra
    BZOJ 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 幼儿园测试题
    BZOJ 5508: [Tjoi2019]甲苯先生的字符串 矩阵乘法_思维
    BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题
    描述符get/set/delete,init/new/call,元类
  • 原文地址:https://www.cnblogs.com/youlingdada-top/p/13086311.html
Copyright © 2020-2023  润新知