首页 > 其他分享 >4.5

4.5

时间:2024-04-30 17:36:21浏览次数:17  
标签:4.5 ps studentId return connection planning sql

PlanningDao

package com.example.demo3.dao;

import static com.example.demo3.dao.StudentDao.getCurrentLoggedInUserId;

import android.util.Log;
import com.example.demo3.entity.Planning;
import com.example.demo3.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.example.demo3.dao.StudentDao;

public class PlanningDao {
    private static final String TAG = "mysql-db_timing-PlanningDao";

    public PlanningDao() {
        // 在构造函数中初始化必要的操作,如果有的话
    }

    // 方法:插入目标数据
    public static boolean insertGoal(Planning planning) {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                String sql = "INSERT INTO planning (studentId, weekNum, goal) VALUES (?, ?, ?)";
                PreparedStatement ps = connection.prepareStatement(sql);
                String studentId = getCurrentLoggedInUserId();
                ps.setString(1, studentId);
                ps.setInt(2, planning.getWeekNum());
                ps.setString(3, planning.getGoal());
                int rowsAffected = ps.executeUpdate();
                ps.close();
                connection.close();
                return rowsAffected > 0;
            } catch (SQLException e) {
                Log.e(TAG, "目标录入失败" + e.getMessage());
                e.printStackTrace();
            }
        }
        return false;
    }

    //录入目标分析
    public boolean insertAnalysis(Planning planning){
        Connection connection = JDBCUtils.getConn();
        if(connection != null){
            try{
                String sql = "UPDATE planning SET analysis = ?, complete = ? WHERE studentId = ? AND weekNum = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, planning.getAnalysis()); // 设置分析字段
                ps.setInt(2, planning.getComplete());   // 设置完成度字段
                ps.setString(3, getCurrentLoggedInUserId()); // 设置学生ID
                ps.setInt(4, planning.getWeekNum());  // 设置周数
                int rowsAffected = ps.executeUpdate();
                ps.close();
                connection.close();
                return rowsAffected > 0;
            }catch(SQLException e){
                Log.e(TAG, "目标分析录入失败" + e.getMessage());
                e.printStackTrace();
            }
        }
        return false;
    }


    // 方法:根据学生ID和周数查找计划
    public static Planning findPlanning(String studentId, int weekNum) {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                String sql = "SELECT * FROM planning WHERE studentId = ? AND weekNum = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, studentId);
                ps.setInt(2, weekNum);
                ResultSet resultSet = ps.executeQuery();
                if (resultSet.next()) {
                    Planning planning = new Planning();
                    planning.setStudentId(resultSet.getString("studentId"));
                    planning.setWeekNum(resultSet.getInt("weekNum"));
                    planning.setGoal(resultSet.getString("goal"));
                    planning.setAnalysis(resultSet.getString("analysis"));
                    planning.setComplete(resultSet.getInt("complete"));
                    resultSet.close();
                    ps.close();
                    connection.close();
                    return planning;
                }
            } catch (SQLException e) {
                Log.e(TAG, "Error finding planning: " + e.getMessage());
                e.printStackTrace();
            }
        }
        return null;
    }


    public static double calculateCompletionPercentage(String studentId) {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                // 查询指定 studentId 的所有记录的 complete 字段总和
                String sql = "SELECT SUM(complete) AS totalComplete FROM planning WHERE studentId = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, studentId);
                ResultSet resultSet = ps.executeQuery();

                if (resultSet.next()) {
                    int totalComplete = resultSet.getInt("totalComplete");
                    // 查询 student 表中指定 studentId 对应的 setGoal 值
                    StudentDao studentDao = new StudentDao();
                    int setGoal = studentDao.getSetGoal(studentId);

                    // 如果 setGoal 为 0,避免除以0错误,返回0
                    if (setGoal == 0) {
                        return 0;
                    }

                    // 计算完成度百分比
                    return ((double) totalComplete / setGoal);
                }
            } catch (SQLException e) {
                Log.e(TAG, "Error calculating completion percentage: " + e.getMessage());
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    Log.e(TAG, "Error closing connection: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
        return -1; // 返回 -1 表示出错
    }

}

RecordDao

package com.example.demo3.dao;

import static com.example.demo3.dao.StudentDao.getCurrentLoggedInUserId;

import android.util.Log;
import com.example.demo3.entity.Planning;
import com.example.demo3.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.example.demo3.dao.StudentDao;

public class PlanningDao {
    private static final String TAG = "mysql-db_timing-PlanningDao";

    public PlanningDao() {
        // 在构造函数中初始化必要的操作,如果有的话
    }

    // 方法:插入目标数据
    public static boolean insertGoal(Planning planning) {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                String sql = "INSERT INTO planning (studentId, weekNum, goal) VALUES (?, ?, ?)";
                PreparedStatement ps = connection.prepareStatement(sql);
                String studentId = getCurrentLoggedInUserId();
                ps.setString(1, studentId);
                ps.setInt(2, planning.getWeekNum());
                ps.setString(3, planning.getGoal());
                int rowsAffected = ps.executeUpdate();
                ps.close();
                connection.close();
                return rowsAffected > 0;
            } catch (SQLException e) {
                Log.e(TAG, "目标录入失败" + e.getMessage());
                e.printStackTrace();
            }
        }
        return false;
    }

    //录入目标分析
    public boolean insertAnalysis(Planning planning){
        Connection connection = JDBCUtils.getConn();
        if(connection != null){
            try{
                String sql = "UPDATE planning SET analysis = ?, complete = ? WHERE studentId = ? AND weekNum = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, planning.getAnalysis()); // 设置分析字段
                ps.setInt(2, planning.getComplete());   // 设置完成度字段
                ps.setString(3, getCurrentLoggedInUserId()); // 设置学生ID
                ps.setInt(4, planning.getWeekNum());  // 设置周数
                int rowsAffected = ps.executeUpdate();
                ps.close();
                connection.close();
                return rowsAffected > 0;
            }catch(SQLException e){
                Log.e(TAG, "目标分析录入失败" + e.getMessage());
                e.printStackTrace();
            }
        }
        return false;
    }


    // 方法:根据学生ID和周数查找计划
    public static Planning findPlanning(String studentId, int weekNum) {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                String sql = "SELECT * FROM planning WHERE studentId = ? AND weekNum = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, studentId);
                ps.setInt(2, weekNum);
                ResultSet resultSet = ps.executeQuery();
                if (resultSet.next()) {
                    Planning planning = new Planning();
                    planning.setStudentId(resultSet.getString("studentId"));
                    planning.setWeekNum(resultSet.getInt("weekNum"));
                    planning.setGoal(resultSet.getString("goal"));
                    planning.setAnalysis(resultSet.getString("analysis"));
                    planning.setComplete(resultSet.getInt("complete"));
                    resultSet.close();
                    ps.close();
                    connection.close();
                    return planning;
                }
            } catch (SQLException e) {
                Log.e(TAG, "Error finding planning: " + e.getMessage());
                e.printStackTrace();
            }
        }
        return null;
    }


    public static double calculateCompletionPercentage(String studentId) {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                // 查询指定 studentId 的所有记录的 complete 字段总和
                String sql = "SELECT SUM(complete) AS totalComplete FROM planning WHERE studentId = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, studentId);
                ResultSet resultSet = ps.executeQuery();

                if (resultSet.next()) {
                    int totalComplete = resultSet.getInt("totalComplete");
                    // 查询 student 表中指定 studentId 对应的 setGoal 值
                    StudentDao studentDao = new StudentDao();
                    int setGoal = studentDao.getSetGoal(studentId);

                    // 如果 setGoal 为 0,避免除以0错误,返回0
                    if (setGoal == 0) {
                        return 0;
                    }

                    // 计算完成度百分比
                    return ((double) totalComplete / setGoal);
                }
            } catch (SQLException e) {
                Log.e(TAG, "Error calculating completion percentage: " + e.getMessage());
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    Log.e(TAG, "Error closing connection: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
        return -1; // 返回 -1 表示出错
    }

}

StudentDao

package com.example.demo3.dao;
import android.util.Log;
import com.example.demo3.entity.Student;
import com.example.demo3.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 = "mysql-db_timing-StudentDao";
    private static String currentLoggedInUserId; // 添加一个类变量来存储当前登录用户的ID



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

        try{
            //简单的sql查询
            String sql = "select * from student where id = ?";
            if (connection != null){// connection不为null表示与数据库建立了连接
                PreparedStatement ps = connection.prepareStatement(sql);
                if (ps != null){
                    Log.e(TAG,"账号:" + 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("password")){
                                if(password.equals(map.get(key))){
                                    msg = 1;            //密码正确
                                    currentLoggedInUserId = id; // 设置当前登录用户的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;
    }



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

        try {
            String sql = "insert into student(id,name,phone,className,password,setGoal,setRecord) values (?,?,?,?,?,?,?)";
            if (connection != null){// 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.getClassName());
                    ps.setString(5, student.getPassword());
                    ps.setInt(6,student.getSetGoal());
                    ps.setInt(7,student.getSetRecord());

                    // 执行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;
        }

    }


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

                    while (rs.next()) {
                        //注意:下标是从1开始
                        String id = rs.getString(1);
                        String name = rs.getString(2);
                        String phone = rs.getString(3);
                        String className = rs.getString(4);
                        String password = rs.getString(5);
                        int setGoal = rs.getInt(6);
                        int setRecord = rs.getInt(7);
                        student = new Student(id, name, phone, className, password, setGoal, setRecord);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "异常findUser:" + e.getMessage());
            return null;
        }
        return student;
    }


    public static String getCurrentLoggedInUserId() {
        return currentLoggedInUserId;
    }


    /**
     * 增加 setGoal 字段的值
     */
    public static boolean incrementSetGoal() {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                String sql = "UPDATE student SET setGoal = setGoal + 1 WHERE id = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                String studentId = getCurrentLoggedInUserId(); // 获取当前登录用户的ID
                ps.setString(1, studentId);
                int rowsAffected = ps.executeUpdate();
                ps.close();
                connection.close();
                return rowsAffected > 0;
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, "异常 incrementSetGoal:" + e.getMessage());
                return false;
            }
        }
        return false;
    }

    /**
     * 增加 setRecord 字段的值
     */
    public static boolean incrementSetRecord() {
        Connection connection = JDBCUtils.getConn();
        if (connection != null) {
            try {
                String sql = "UPDATE student SET setRecord = setRecord + 1 WHERE id = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                String studentId = getCurrentLoggedInUserId(); // 获取当前登录用户的ID
                ps.setString(1, studentId);
                int rowsAffected = ps.executeUpdate();
                ps.close();
                connection.close();
                return rowsAffected > 0;
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, "异常 incrementSetRecord:" + e.getMessage());
                return false;
            }
        }
        return false;
    }

    public int getSetGoal(String studentId) {
        StudentDao studentDao = new StudentDao();
        Student student = studentDao.findStudent(studentId);
        if (student != null) {
            return student.getSetGoal();
        } else {
            // 如果未找到学生,则返回默认值或者抛出异常,这取决于你的需求
            return 0; // 默认值为0
        }
    }

    public int getSetRecord(String studentId){
        StudentDao studentDao = new StudentDao();
        Student student = studentDao.findStudent(studentId);
        if (student != null) {
            return student.getSetRecord();
        } else {
            // 如果未找到学生,则返回默认值或者抛出异常,这取决于你的需求
            return 0; // 默认值为0
        }
    }

}

 

标签:4.5,ps,studentId,return,connection,planning,sql
From: https://www.cnblogs.com/yindantong/p/18168437

相关文章

  • 光影魔术手 v4.5.6.208 绿色便携版
    更新流水:2024.04.27:跟进官方4.5.6.208,第一版修改内容:by.星罗月兔&DxFans&haiyang457去校验(方案来自@星罗月兔),去更新,去多余组件及无用菜单;便携版集成新版启动器,简化了诸多文件存在,看起来更清爽;单文件版方案来自@haiyang457,特此感谢!下载地址:https://down.neoimaging......
  • Visual Studio 安装 旧版本(.NET Framework 4.0 和 4.5)
    VisualStudio2022安装程序中单个组件中没有 .NETFramework4.0 或者 .NETFramework4.5其他NET版本直接在下载适用于VisualStudio的.NETSDK中的开发者工具包解决方式:通过nuget下载4.0安装包下载地址:https://www.nuget.org/packages/Microsoft.NETFramework.......
  • 4.5
    最近在开发中,需要用到将图片保存到数据库中,然后再把数据库中的数据拿出来进行显示。后面查了下一些方法,主要这样的实现就有两种思路:方法一:将图片解析为二进制的数据,然后放入到数据库中的一种BLOD类型中方法二:保存图片的URL路径方法三:其实还可以将图片存放到本地文件,那么就以后......
  • PT Application Inspector 4.5 (Linux) - 静态、动态和交互式应用程序安全测试
    PTApplicationInspector4.5(Linux)-静态、动态和交互式应用程序安全测试唯一一款提供高质量分析和便捷工具以自动确认漏洞的源代码分析器请访问原文链接:PTApplicationInspector4.5(Linux)-静态、动态和交互式应用程序安全测试,查看最新版。原创作品,转载请保留出处。......
  • Codes 重新定义 SaaS 模式的研发项目管理平台开源版 4.5.3 发布
    一:简介   Codes重新定义SaaS模式= 云端认证+程序及数据本地安装+不限功能+30人免费    Codes 是一个 高效、简洁、轻量的一站式研发项目管理平台。包含需求管理,任务管理,测试管理,缺陷管理,自动化测试,cicd 等功能;Codes帮助企业加速融合研发、测试、运......
  • 2024.4.5 软工日报
    packagecom.example.sub_find;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.Toast;importandroidx.appcompat.app.AppCompatActivity;importand......
  • VS2022 解决方案打不开 .NET Framework 4.0 、 4.5 等老项目
    vs2022开发工具最低支持net4.8,以下的如net3.5、4.0、4.5项目,加载不上怎么处理。一、下载.NETFramework框架.NETFramework4.5.2.NETFramework4.5.1.NETFramework4.5.NETFramework4.0,把框架放到vs安装目录下的对应包中。microsoft.netframework.referenceassemblies.......
  • c# Framwork4.5 添加webapi处理跨域请求问题
     常用2种方式 1)web.config允许所有来源,HTTP方法,请求标头跨域:后端,在Web.config中找到<system.webServer>标签,里面添加配置如下:<httpProtocol><customHeaders><addname="Access-Control-Allow-Origin"value="*"/><addname="Acc......
  • apache-HttpClient4.5
    packagecom.yonyou.ucf.mdf.sample.utils;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.fasterxml.jackson.core.type.TypeReference;importcom.fasterxml.jackson.databind.ObjectMapper;importlombok.extern.slf4j.Slf4j;......
  • VS2019+open CV4.5.5的配置
    1.去openCV的官网下载对应版本:OpenCV-OpenComputerVisionLibrary2.右击此电脑--属性--高级系统设置--环境变量--点击变量Path的右边进行新建分别输入:D:\opencv\opencv\build\x64\vc15\binD:\opencv\opencv\build\x64\vc14\bin%OPENCV_DIR%\bin3.打开op......