tcp网络编程加原生Jdbc模拟登录注册
功能:重置密码 过程描述:
1.首先登录服务器 登录成功,继续重置密码 登录失败,重新登录
2.重置密码成功后,需要告知客户端,修改成功.
使用的技术:
1.TCP通信
2.jdbc
3.数据库查找和修改
1. 客户端代码
public class Server {
public static void main(String[] args) throws Exception {
User user = login();
System.out.println("登录后的用户信息回显"+user);
if(user!=null){
updatePassword(user);
}else {
System.out.println("登录失败");
}
return;
}
private static boolean updatePassword(User user) throws Exception{
System.out.println("请输入想要修改的密码");
Scanner scanner = new Scanner(System.in);
String password = scanner.next();
user.setPassword(password);
Socket socket = new Socket("127.0.0.1", 9999);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(("path=/updatePassword&"+Objects.getJson(user)).getBytes());
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
System.out.println(new String(bytes,0,read));
return false;
}
private static User login() throws Exception {
System.out.println("请输入账号和密码");
Scanner scanner = new Scanner(System.in);
String username = scanner.next();
String passsword = scanner.next();
Socket socket = new Socket("127.0.0.1",9999);
OutputStream outputStream = socket.getOutputStream();
String str = "path=/login"+"&username="+username+"&password="+passsword;
outputStream.write(str.getBytes());
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
String s = new String(bytes,0,read);
socket.close();
User user = (User) Objects.getObjectFromString(User.class,s,"&","=");
if(user==null||user.getUsername().equals("null"))
return null;
return user;
}
}
-
服务器端代码
public class TCP {
static ServerSocket serverSocket = null;
static Connection connection = null;
static {
try {
serverSocket = new ServerSocket(9999);
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql4");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
while (true){
System.out.println("服务器正常,正在等待连接");
Socket accept = serverSocket.accept();
String s = getMessage(accept);
int start = s.indexOf("=");
int end = s.indexOf("&");
String substring = s.substring(start+1, end);
if(substring.equals("/login")){
login(accept,s);
}else if(substring.equals("/updatePassword")){
updatePassword(accept,s);
}
}
}
private static void updatePassword(Socket accept, String s) {
try {
User user = (User) Objects.getObjectFromString(User.class,s,"&","=");
String sql = "update user set password = ? where username = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,user.getPassword());
preparedStatement.setString(2,user.getUsername());
int i = preparedStatement.executeUpdate();
OutputStream outputStream = accept.getOutputStream();
if(i==1){
outputStream.write("true".getBytes());
}else outputStream.write("false".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void login(Socket accept,String s) throws Exception{
OutputStream outputStream = accept.getOutputStream();
User user = getUP(s);
String sql = "SELECT * FROM USER where username = ? AND password = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,""+user.getUsername());
preparedStatement.setString(2,""+user.getPassword());
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
outputStream.write(Objects.getJson(user).getBytes());
}else {
User user1 = new User();
outputStream.write(Objects.getJson(user1).getBytes());
}
}
private static String getMessage(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
byte[] bs = new byte[1024];
int read = inputStream.read(bs);
return new String(bs,0,read);
}
private static User getUP(String s) {
User user = null;
try {
user = (User) Objects.getObjectFromString(User.class,s,"&","=");
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
-
模型类User
public class User {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
-
自己用反射封装的工具类,主要是对象与字符串的形式互转(开发有专门的工具类,自己写的利于理解)
public class Objects {
public static Object getObjectFromString (Class c,String message,String allSplit,String mapSplit) throws Exception{
Object o = c.newInstance();
Field[] declaredFields = c.getDeclaredFields();
String[] split = message.split(allSplit);
Object[] os = new Object[declaredFields.length];
for (String s : split) {
String[] split1 = s.split(mapSplit);
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
if(split1[0].equals(declaredField.getName())){
Object o1 = typeConversion(declaredField.getGenericType(), split1[1]);
declaredField.set(o,o1);
break;
}
}
}
return o;
}
public static Object typeConversion(Type t,String str){
if(t.equals(String.class)){
return str;
}else if(t.equals(Integer.class)){
return Integer.parseInt(str);
}else if(t.equals(Long.class)){
return Long.parseLong(str);
}else if(t.equals(Double.class)){
return Double.parseDouble(str);
}else if(t.equals(Float.class)){
return Float.parseFloat(str);
}else if(t.equals(Long.class)){
return Long.parseLong(str);
}
return null;
}
public static <T> String getJson(T t) throws Exception{
Class<?> c = t.getClass();
Field[] declaredFields = c.getDeclaredFields();
StringBuilder string = new StringBuilder();
if(declaredFields==null||declaredFields.length==0)
return null;
string.append(declaredFields[0].getName());
string.append("=");
string.append(declaredFields[0].get(t));
for (int i = 1; i < declaredFields.length; i++) {
string.append("&");
string.append(declaredFields[i].getName());
string.append("=");
string.append(declaredFields[i].get(t));
}
return string.toString();
}
}