2024.6.13

第七十二天

所花时间:2小时

代码量:400+

博客量:1

了解到的知识点:

dao+entity

package com.example.huiyi.dao;

import com.example.huiyi.entity.user;
import com.example.huiyi.utils.JDBCUtils;
import android.util.Log;

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


public class userDao {

    private static final String TAG = "mysql-tb_user-StudentDao";

    /**
     * function: 登录
     * */
    public int login(String id, String phone){

        HashMap<String, Object> map = new HashMap<>();
        // 根据数据库名称,建立连接
        Connection connection = JDBCUtils.getConn();
        int msg = 0;
        try {
            // mysql简单的查询语句。这里是根据studystudent表的ID字段来查询某条记录
            String sql = "select * from Employee2 where UserName = ?";
            if (connection != null){// connection不为null表示与数据库建立了连接
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null){
                    Log.e(TAG,"用户ID:" + id);
                    //根据用户ID进行查询
                    ps.setString(1, id);
                    // 执行sql查询语句并返回结果集
                    ResultSet rs = ps.executeQuery();
                    int count = rs.getMetaData().getColumnCount();
                    //将查到的内容储存在map里
                    while (rs.next()){
                        // 注意:下标是从1开始的
                        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("Phone")){
                                if(phone.equals(map.get(key))){
                                    msg = 1;            //密码正确
                                }
                                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;
    }


    /**
     * function: 注册
     * */
    public boolean register(user user){
        HashMap<String, Object> map = new HashMap<>();
        // 根据数据库名称,建立连接
        Connection connection = JDBCUtils.getConn();

        try {
            String sql = "insert into Employee2(UserName,Sex,Department,Phone,Position) values (?,?,?,?,?)";
            if (connection != null){// connection不为null表示与数据库建立了连接
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null){

                    //将数据插入数据库
                    ps.setString(1,user.getUserName());
                    ps.setString(2,user.getSex());
                    ps.setString(3,user.getDepartment());
                    ps.setString(4,user.getPhone());
                    ps.setString(5,user.getPosition());
                    // 执行sql查询语句并返回结果集
                    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;
        }

    }

    /**
     * function: 根据学号进行查找该用户是否存在
     * */
    public user findStudent(String name) {

        // 根据数据库名称,建立连接
        Connection connection = JDBCUtils.getConn();
        user user1 = null;
        try {
            String sql = "select * from Employee2 where UserName = ?";
            if (connection != null){// connection不为null表示与数据库建立了连接
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null) {
                    ps.setString(1, name);
                    ResultSet rs = ps.executeQuery();

                    while (rs.next()) {
                        //注意:下标是从1开始
                        String id = rs.getString(1);
                        String name1 = rs.getString(2);
                        String sex = rs.getString(3);
                        String department = rs.getString(4);
                        String phone = rs.getString(5);
                        String address = rs.getString(6);
                        user1 = new user(id,name1, sex, department, phone,address);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "异常findUser:" + e.getMessage());
            return null;
        }
        return user1;
    }

}
package com.example.huiyi.entity;

import java.io.Serializable;

public class Meeting implements Serializable {
    private int id;
    private String name;
    private String content;
    private String beginTime;
    private String endTime;
    private String participants;
    private String auditStatus;
    private String auditOpinion;

    public Meeting(int id, String name, String content, String beginTime, String endTime, String participants, String auditStatus, String auditOpinion) {
        this.id = id;
        this.name = name;
        this.content = content;
        this.beginTime = beginTime;
        this.endTime = endTime;
        this.participants = participants;
        this.auditStatus = auditStatus;
        this.auditOpinion = auditOpinion;
    }

    // Getters and setters...

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getParticipants() {
        return participants;
    }

    public void setParticipants(String participants) {
        this.participants = participants;
    }

    public String getAuditStatus() {
        return auditStatus;
    }

    public void setAuditStatus(String auditStatus) {
        this.auditStatus = auditStatus;
    }

    public String getAuditOpinion() {
        return auditOpinion;
    }

    public void setAuditOpinion(String auditOpinion) {
        this.auditOpinion = auditOpinion;
    }
}
package com.example.huiyi.entity;

public class MeetingRoom {
    private int id;
    private String name;
    private String address;
    private String readyState;
    private String status;
    private int capacity;

    public MeetingRoom(int id, String name, String address, String readyState, String status, int capacity) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.readyState = readyState;
        this.status = status;
        this.capacity = capacity;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getReadyState() {
        return readyState;
    }

    public void setReadyState(String readyState) {
        this.readyState = readyState;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
// Getters and setters...
}
package com.example.huiyi.entity;

public class user {
    private String userId;
    private String userName;
    private String sex;
    private String department;
    private String phone;
    private String position;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public user() {
    }

    public user(String userId, String userName, String sex, String department, String phone, String position) {
        this.userId = userId;
        this.userName = userName;
        this.sex = sex;
        this.department = department;
        this.phone = phone;
        this.position = position;
    }
}

 

posted @ 2024-06-17 02:43  cvjj  阅读(8)  评论(0)    收藏  举报