3.25
添加数据到数据库,成功失败都会有提示
private void uploadMoodRecord() {
String moodText = editTextMood.getText().toString();
Log.d("MoodRecordActivity", "Uploading record: Age=" + age + ", Beauty=" + beauty + ", Expression=" + expression + ", Mood=" + moodText);
executorService.execute(() -> {
try {
Connection connection = JDBCUtils.getConn();
if (connection != null) {
Log.d("MoodRecordActivity", "Database connection established");
String sql = "INSERT INTO mood_records (age, beauty, expression, image, mood, date) VALUES (?, ?, ?, ?, ?, NOW())";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, age);
preparedStatement.setString(2, beauty);
preparedStatement.setString(3, expression);
preparedStatement.setString(4, imageBase64);
preparedStatement.setString(5, moodText);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
runOnUiThread(() -> {
Toast.makeText(MoodRecordActivity.this, "Record uploaded successfully", Toast.LENGTH_SHORT).show();
finish(); // 返回到主页面
});
} else {
Log.e("MoodRecordActivity", "Failed to connect to database");
}
} catch (SQLException e) {
Log.e("MoodRecordActivity", "SQL Exception: " + e.getMessage());
}
});
}