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;
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM