6.11小测代码
以下为会议预约管理系统的部分代码
BookMeetingActivity.java
package com.example.myapplication611; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class BookMeetingActivity extends AppCompatActivity { private EditText meetingTitle, meetingContent, meetingStartTime, meetingEndTime, meetingNum, meetingParticipants; private Spinner meetingRoomSpinner; private Button submitMeetingButton; private ArrayList<String> availableRooms = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_book_meeting); // 手动创建一些示例数据并设置给 Spinner ArrayList<String> sampleData = new ArrayList<>(); sampleData.add("会议室A"); sampleData.add("会议室B"); sampleData.add("会议室C"); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, sampleData); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); meetingRoomSpinner = findViewById(R.id.meetingRoomSpinner); meetingRoomSpinner.setAdapter(adapter); // 获取其他视图对象 meetingTitle = findViewById(R.id.meetingTitle); meetingContent = findViewById(R.id.meetingContent); meetingStartTime = findViewById(R.id.meetingStartTime); meetingEndTime = findViewById(R.id.meetingEndTime); meetingNum = findViewById(R.id.meetingNum); meetingParticipants = findViewById(R.id.meetingParticipants); submitMeetingButton = findViewById(R.id.submitMeetingButton); // 点击按钮提交会议申请 submitMeetingButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 检查所有相关视图对象是否为空 if (checkAllFields()) { String title = meetingTitle.getText().toString(); String content = meetingContent.getText().toString(); String startTime = meetingStartTime.getText().toString(); String endTime = meetingEndTime.getText().toString(); String num = meetingNum.getText().toString(); String participants = meetingParticipants.getText().toString(); String selectedRoom = meetingRoomSpinner.getSelectedItem().toString(); new SubmitMeetingTask().execute(title, content, startTime, endTime, num, participants, selectedRoom); } else { Toast.makeText(BookMeetingActivity.this, "有必填项为空,请填写完整", Toast.LENGTH_SHORT).show(); } } }); } // 检查所有必填字段是否为空 private boolean checkAllFields() { return !(meetingTitle.getText().toString().isEmpty() || meetingContent.getText().toString().isEmpty() || meetingStartTime.getText().toString().isEmpty() || meetingEndTime.getText().toString().isEmpty() || meetingNum.getText().toString().isEmpty() || meetingParticipants.getText().toString().isEmpty() || meetingRoomSpinner.getSelectedItem() == null); } // 异步任务:提交会议申请 private class SubmitMeetingTask extends AsyncTask<String, Void, Boolean> { @Override protected Boolean doInBackground(String... params) { String title = params[0]; String content = params[1]; String rawStartTime = params[2]; String rawEndTime = params[3]; String num = params[4]; String participants = params[5]; String selectedRoom = params[6]; // 替换错误的分隔符 String startTime = rawStartTime.replace("-", "-"); String endTime = rawEndTime.replace("-", "-"); Mysql.getconn(); try { // 检查会议室是否在该时间段内没有冲突 String checkSql = "SELECT COUNT(*) FROM Meeting WHERE MeetroomId = (SELECT MeetroomId FROM Meetroom WHERE MeetroomName = ?) " + "AND ((MeetingBegin <= ? AND MeetingEnd >= ?) OR (MeetingBegin <= ? AND MeetingEnd >= ?) OR (MeetingBegin >= ? AND MeetingEnd <= ?))"; try (PreparedStatement checkStmt = Mysql.conn.prepareStatement(checkSql)) { checkStmt.setString(1, selectedRoom); checkStmt.setString(2, startTime); checkStmt.setString(3, startTime); checkStmt.setString(4, endTime); checkStmt.setString(5, endTime); checkStmt.setString(6, startTime); checkStmt.setString(7, endTime); ResultSet rs = checkStmt.executeQuery(); if (rs.next() && rs.getInt(1) > 0) { return false; // 会议时间冲突 } } // 插入新的会议申请 String insertSql = "INSERT INTO Meeting (MeetingName, MeetingContent, MeetingBegin, MeetingEnd, MeetingNum, Participants, MeetroomId, UserID, AuditStatus, AuditMind) " + "VALUES (?, ?, ?, ?, ?, ?, (SELECT MeetroomId FROM Meetroom WHERE MeetroomName = ?), ?, '待审核', '')"; try (PreparedStatement insertStmt = Mysql.conn.prepareStatement(insertSql)) { insertStmt.setString(1, title); insertStmt.setString(2, content); insertStmt.setString(3, startTime); insertStmt.setString(4, endTime); insertStmt.setInt(5, Integer.parseInt(num)); insertStmt.setString(6, participants); insertStmt.setString(7, selectedRoom); insertStmt.setInt(8, 1); // 假设登录的用户ID为1,实际应该从登录信息中获取 insertStmt.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); return false; } finally { Mysql.close(); } return true; } @Override protected void onPostExecute(Boolean success) { if (success) { Toast.makeText(BookMeetingActivity.this, "会议申请提交成功", Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(BookMeetingActivity.this, "会议时间冲突或其他错误", Toast.LENGTH_SHORT).show(); } } } }
EmployeeActivity.java
package com.example.myapplication611; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class EmployeeActivity extends AppCompatActivity { private Button viewMeetingRoomsButton; private Button searchMeetingRoomsButton; private Button bookMeetingButton; private Button viewMeetingsButton; private String userRole; private String userName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_employee); userRole = getIntent().getStringExtra("USER_ROLE"); userName = getIntent().getStringExtra("USERNAME"); viewMeetingRoomsButton = findViewById(R.id.viewMeetingRoomsButton); searchMeetingRoomsButton = findViewById(R.id.searchMeetingRoomsButton); bookMeetingButton = findViewById(R.id.bookMeetingButton); viewMeetingsButton = findViewById(R.id.viewMeetingsButton); viewMeetingRoomsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(EmployeeActivity.this, ViewMeetingRoomsActivity.class); intent.putExtra("USER_ROLE", userRole); intent.putExtra("USERNAME", userName); startActivity(intent); } }); searchMeetingRoomsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(EmployeeActivity.this, SearchMeetingRoomsActivity.class); intent.putExtra("USER_ROLE", userRole); intent.putExtra("USERNAME", userName); startActivity(intent); } }); bookMeetingButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(EmployeeActivity.this, BookMeetingActivity.class); intent.putExtra("USER_ROLE", userRole); intent.putExtra("USERNAME", userName); startActivity(intent); } }); viewMeetingsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(EmployeeActivity.this, MeetingListActivity.class); intent.putExtra("USER_ROLE", userRole); intent.putExtra("USERNAME", userName); startActivity(intent); } }); } }
activity_book_meeting.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/meetingTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="会议主题" /> <EditText android:id="@+id/meetingContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="会议内容" /> <EditText android:id="@+id/meetingStartTime" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="开始时间 (格式: YYYY-MM-DD HH:MM:SS)" /> <EditText android:id="@+id/meetingEndTime" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="结束时间 (格式: YYYY-MM-DD HH:MM:SS)" /> <EditText android:id="@+id/meetingNum" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="参会人数" /> <EditText android:id="@+id/meetingParticipants" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="参会人员(姓名以逗号分隔)" /> <Spinner android:id="@+id/meetingRoomSpinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" /> <Button android:id="@+id/submitMeetingButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="提交申请" /> </LinearLayout>
MainActivity.java
package com.example.myapplication611; import android.content.Intent; import android.os.Bundle; import android.os.StrictMode; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class MainActivity extends AppCompatActivity { private EditText usernameEditText; private EditText passwordEditText; private Button loginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 允许主线程进行网络操作(仅用于调试,不推荐在生产环境中使用) StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .permitNetwork().build()); usernameEditText = findViewById(R.id.UserNameEdit); passwordEditText = findViewById(R.id.PassWordEdit); loginButton = findViewById(R.id.LoginButton); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = usernameEditText.getText().toString().trim(); String password = passwordEditText.getText().toString().trim(); validateLogin(username, password); } }); } private void validateLogin(String username, String password) { new Thread(new Runnable() { @Override public void run() { Mysql.getconn(); if (Mysql.conn == null) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "数据库连接失败", Toast.LENGTH_SHORT).show(); } }); return; } boolean isValid = false; String role = null; String sql = "SELECT UserName, UserID, Position FROM Employee WHERE UserName = ? AND UserID = ?"; try { PreparedStatement preparedStatement = Mysql.conn.prepareStatement(sql); preparedStatement.setNString(1, username); // 使用 setNString 方法设置参数 preparedStatement.setNString(2, password); // 使用 setNString 方法设置参数 ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { isValid = true; role = resultSet.getString("Position"); } preparedStatement.close(); resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } final boolean finalIsValid = isValid; final String finalRole = role; runOnUiThread(new Runnable() { @Override public void run() { if (finalIsValid) { Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); // 根据角色跳转到相应的页面 Intent intent; if ("Manager".equals(finalRole)) { intent = new Intent(MainActivity.this, ManagerActivity.class); } else if ("Employee".equals(finalRole)) { intent = new Intent(MainActivity.this, EmployeeActivity.class); } else { Toast.makeText(MainActivity.this, "未知角色", Toast.LENGTH_SHORT).show(); return; } intent.putExtra("USER_ROLE", finalRole); startActivity(intent); } else { Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } }); } }).start(); } @Override protected void onDestroy() { super.onDestroy(); // 在活动销毁时关闭数据库连接 Mysql.close(); } }