/**
* function: 统计打卡次数
*/
public static String count(String account) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
StringBuilder resultBuilder = new StringBuilder();
try {
connection = JDBCUtils.getConn();
if (connection != null) {
// 获取当前系统日期
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
String formattedDate = dateFormat.format(calendar.getTime());
String query = "SELECT COUNT(*) AS count FROM k2 WHERE Account = ? AND SUBSTRING(date, 1, 7) = ?";
statement = connection.prepareStatement(query);
statement.setString(1, account);
statement.setString(2, formattedDate);
resultSet = statement.executeQuery();
if (resultSet.next()) {
int count = resultSet.getInt("count");
System.out.println("统计结果: " + count);
resultBuilder.append("打卡次数:").append(count).append("\n");
}
}
} finally {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
return resultBuilder.toString();
}