騰訊公益賽個人衝刺部落格4(2024.4.26)

记得关月亮發表於2024-05-06
今天完善了幫扶程式的後端,實現了後端的增和查詢
package com.example.helppeople;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import com.example.helppeople.R;
import com.example.helppeople.entity.Help;
import com.example.helppeople.utils.JDBCUtils;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Help2 extends AppCompatActivity {
    private EditText etRequest;
    private EditText etPlace;
    private EditText etMoney;
    private Button btnAdd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help2);

        etRequest = findViewById(R.id.etRequest);
        etPlace = findViewById(R.id.etPlace);
        etMoney = findViewById(R.id.etMoney);
        btnAdd = findViewById(R.id.btnAdd);
        etRequest.setHint("幫扶要求:");
        etPlace.setHint("幫扶位置:");
        etMoney.setHint("支付金額:");
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCourseToDatabase();
            }
        });
    }
    public void fanhuionclick(View view) {
        Intent intent = new Intent();
        intent.setClass(Help2.this,gridview.class);
        startActivity(intent);
    }
    private void addCourseToDatabase() {

        String request = etRequest.getText().toString().trim();
        String place = etPlace.getText().toString().trim();
        String money = etMoney.getText().toString().trim();
        if (request.isEmpty() || place.isEmpty() || money.isEmpty()) {
            Toast.makeText(this, "請填寫完整的需求資訊", Toast.LENGTH_SHORT).show();
            return;
        }

        Help entity = new Help();
        entity.setRequest(request);
        entity.setPlace(place);
        entity.setMoney(money);

        SaveCourseTask task = new SaveCourseTask();
        task.execute(entity);
    }



    private class SaveCourseTask extends AsyncTask<Help, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Help... params) {


            Help entity = params[0];
            try {
                // 獲取資料庫連線
                Connection connection = JDBCUtils.getConn();

                if (connection != null) {
                    // 建立 SQL 插入語句
                    String sql = "INSERT INTO help (request, place, money) VALUES (?, ?, ?)";
                    // 建立 PreparedStatement 物件
                    PreparedStatement statement = connection.prepareStatement(sql);
                    statement.setString(1, entity.getRequest());
                    statement.setString(2, entity.getPlace());
                    statement.setString(3, entity.getMoney());

                    // 執行插入操作
                    statement.executeUpdate();
                    // 提交事務
                    connection.commit();
                    // 關閉 PreparedStatement 和 Connection
                    statement.close();
                    connection.close();

                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {
            if (success) {
                // 資料儲存成功
                Toast.makeText(Help2.this, "資料提交成功", Toast.LENGTH_SHORT).show();
                etRequest.setText("");
                etPlace.setText("");
                etMoney.setText("");
            } else {
                // 資料儲存失敗
                Toast.makeText(Help2.this, "資料提交成功", Toast.LENGTH_SHORT).show();
            }
        }

    }

}

相關文章