import android.util.Log; import com.example.demo3.entity.Record; import com.example.demo3.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class RecordDao { private static final String TAG = "mysql-db_timing-RecordDao"; // 方法:插入每日打卡数据 public static boolean insertRecord(Record record) { Connection connection = JDBCUtils.getConn(); if (connection != null) { try { String sql = "INSERT INTO record (studentId, weekNum, startTime, endTime, recording) VALUES (?, ?, ?, ?, ?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, record.getStudentId()); ps.setInt(2, record.getWeekNum()); ps.setString(3, record.getStartTime()); ps.setString(4, record.getEndTime()); ps.setString(5, record.getRecording()); 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 Record findRecord(String studentId, int weekNum) { Connection connection = JDBCUtils.getConn(); if (connection != null) { try { String sql = "SELECT * FROM record WHERE studentId = ? AND weekNum = ?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, studentId); ps.setInt(2, weekNum); ResultSet resultSet = ps.executeQuery(); if (resultSet.next()) { Record record = new Record(); record.setStudentId(resultSet.getString("studentId")); record.setWeekNum(resultSet.getInt("weekNum")); record.setStartTime(resultSet.getString("startTime")); record.setEndTime(resultSet.getString("endTime")); record.setRecording(resultSet.getString("recording")); resultSet.close(); ps.close(); connection.close(); return record; } } catch (SQLException e) { Log.e(TAG, "Error finding record: " + e.getMessage()); e.printStackTrace(); } } return null; } // 方法:查询所有打卡记录 public List<Record> findAllRecords() { Connection connection = JDBCUtils.getConn(); List<Record> records = new ArrayList<>(); if (connection != null) { try { String sql = "SELECT * FROM record"; PreparedStatement ps = connection.prepareStatement(sql); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()) { Record record = new Record(); record.setStudentId(resultSet.getString("studentId")); record.setWeekNum(resultSet.getInt("weekNum")); record.setStartTime(resultSet.getString("startTime")); record.setEndTime(resultSet.getString("endTime")); record.setRecording(resultSet.getString("recording")); records.add(record); } resultSet.close(); ps.close(); connection.close(); } catch (SQLException e) { Log.e(TAG, "Error finding all records: " + e.getMessage()); e.printStackTrace(); } } return records; } // 方法:根据学生ID查询打卡记录 public List<Record> findRecordsByStudentId(String studentId) { Connection connection = JDBCUtils.getConn(); List<Record> records = new ArrayList<>(); if (connection != null) { try { String sql = "SELECT * FROM record WHERE studentId = ?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, studentId); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()) { Record record = new Record(); record.setStudentId(resultSet.getString("studentId")); record.setWeekNum(resultSet.getInt("weekNum")); record.setStartTime(resultSet.getString("startTime")); record.setEndTime(resultSet.getString("endTime")); record.setRecording(resultSet.getString("recording")); records.add(record); } resultSet.close(); ps.close(); connection.close(); } catch (SQLException e) { Log.e(TAG, "Error finding records by student ID: " + e.getMessage()); e.printStackTrace(); } } return records; } // 方法:根据周数查询打卡记录 public List<Record> findRecordsByWeekNum(int weekNum) { Connection connection = JDBCUtils.getConn(); List<Record> records = new ArrayList<>(); if (connection != null) { try { String sql = "SELECT * FROM record WHERE weekNum = ?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1, weekNum); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()) { Record record = new Record(); record.setStudentId(resultSet.getString("studentId")); record.setWeekNum(resultSet.getInt("weekNum")); record.setStartTime(resultSet.getString("startTime")); record.setEndTime(resultSet.getString("endTime")); record.setRecording(resultSet.getString("recording")); records.add(record); } resultSet.close(); ps.close(); connection.close(); } catch (SQLException e) { Log.e(TAG, "Error finding records by week number: " + e.getMessage()); e.printStackTrace(); } } return records; } }
标签:ps,getString,record,数据库,resultSet,插入,connection,close,打卡 From: https://www.cnblogs.com/aixin52129211/p/18107333