今天上午 将打卡逻辑代码优化了一下
之后每天就只可以打卡一次了
public static String daka(String time_s, String time_e, String text, String account) { Connection connection = JDBCUtils.getConn(); try { if (connection != null) { String sql1 = "SELECT * FROM infor WHERE date = ? AND account = ?"; PreparedStatement ps1 = connection.prepareStatement(sql1); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String currentDate = dateFormat.format(new Date()); ps1.setString(1, currentDate); ps1.setString(2, account); ResultSet resultSet = ps1.executeQuery(); if (!resultSet.next()) { String sql = "INSERT INTO infor (time_s, time_e, text, account, date) VALUES (?, ?, ?, ?, ?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, time_s); ps.setString(2, time_e); ps.setString(3, text); ps.setString(4, account); ps.setString(5, currentDate); int rs = ps.executeUpdate(); return rs > 0 ? "1" : "-1"; // 如果执行结果大于0,说明打卡成功,返回"1";否则返回"-1" } else { return "-1"; // 今日已打卡,返回"-1" } } else { return "0"; // 连接数据库失败,返回"0" } } catch (Exception e) { e.printStackTrace(); return "0"; // 发生异常,返回"0" } finally { // 可以在这里关闭连接等资源 } }
public void 打卡2(View view) { // 获取开始时间 int startHour = startTimePicker.getHour(); int startMinute = startTimePicker.getMinute(); // 获取结束时间 int endHour = endTimePicker.getHour(); int endMinute = endTimePicker.getMinute(); // 将开始时间转换为字符串形式 String startTime = String.format("%02d:%02d", startHour, startMinute); // 将结束时间转换为字符串形式 String endTime = String.format("%02d:%02d", endHour, endMinute); // 获取学习内容 String studyContent = studyContentEditText.getText().toString(); Intent intent = getIntent(); String account = intent.getStringExtra("account"); new Thread(){ @Override public void run() { String result = UserDao.daka(startTime, endTime, studyContent, account); switch (result) { case "1": runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(Clock_in.this, "打卡成功", Toast.LENGTH_LONG).show(); } }); break; case "-1": runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(Clock_in.this, "今日已打卡", Toast.LENGTH_LONG).show(); } }); break; case "0": runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(Clock_in.this, "连接数据库失败", Toast.LENGTH_LONG).show(); } }); break; } } }.start(); }
标签:Toast,ps,account,String,app,打卡,安卓,setString From: https://www.cnblogs.com/youxiandechilun/p/18096076