向数据库插入计划数据
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 表示出错 } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示