腾讯公益赛冲刺个人博客1(2024.4.23)

今天确定了组内第一阶段的任务是基本完成sos和帮扶两个核心功能以及登录注册等常规功能

完成登录和注册的基本页面以及数据库的设计

package com.example.helppeople.entity;

public class Student {

    private String id;
    private String name;
    private String phone;

    private String password;

    public Student() {
    }

    public Student(String id, String name, String phone, String password) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.password = password;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
package com.example.helppeople.dao;

import android.util.Log;

import com.example.helppeople.entity.Student;
import com.example.helppeople.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;

public class StudentDao {
    private static final String TAG = "mydb-StudentDao";
    private static String currentLoggedInUserId;

    public int login(String id, String password) {
        HashMap<String, Object> map = new HashMap<>();
        Connection connection = JDBCUtils.getConn();
        int msg = 0;

        try {
            String sql = "select * from student where name = ?";
            if (connection != null) {
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null) {
                    Log.e(TAG, "账号:" + id);
                    ps.setString(1, id);
                    ResultSet rs = ps.executeQuery();
                    int count = rs.getMetaData().getColumnCount();
                    while (rs.next()) {
                        for (int i = 1; i <= count; i++) {
                            String field = rs.getMetaData().getColumnName(i);
                            map.put(field, rs.getString(field));
                        }
                    }
                    connection.close();
                    ps.close();

                    if (map.size() != 0) {
                        StringBuilder s = new StringBuilder();
                        for (String key : map.keySet()) {
                            if (key.equals("password")) {
                                if (password.equals(map.get(key))) {
                                    msg = 1;
                                    currentLoggedInUserId = id;
                                } else
                                    msg = 2;
                                break;
                            }
                        }
                    } else {
                        Log.e(TAG, "查询结果为空");
                        msg = 3;
                    }
                } else {
                    msg = 0;
                }
            } else {
                msg = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "异常login:" + e.getMessage());
            msg = 0;
        }

        return msg;
    }

    public boolean register(Student student) {
        HashMap<String, Object> map = new HashMap<>();
        Connection connection = JDBCUtils.getConn();

        try {
            String sql = "insert into student(id, name, phone,password) values (?,?,?,?)";
            if (connection != null) {
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null) {
                    ps.setString(1, student.getId());
                    ps.setString(2, student.getName());
                    ps.setString(3, student.getPhone());
                    ps.setString(4, student.getPassword());

                    int rs = ps.executeUpdate();
                    if (rs > 0)
                        return true;
                    else
                        return false;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "异常register:" + e.getMessage());
            return false;
        }
    }

    public Student findStudent(String studentId) {
        Connection connection = JDBCUtils.getConn();
        Student student = null;
        try {
            String sql = "select * from student where id = ?";
            if (connection != null) {
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null) {
                    ps.setString(1, studentId);
                    ResultSet rs = ps.executeQuery();

                    while (rs.next()) {
                        String id = rs.getString(1);
                        String name = rs.getString(2);
                        String phone = rs.getString(3);
                        String password = rs.getString(4);
                        student = new Student(id, name, phone, password);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "异常findUser:" + e.getMessage());
            return null;
        }
        return student;
    }
}

 

posted @ 2024-05-06 13:05  记得关月亮  阅读(3)  评论(0编辑  收藏  举报