今天完善了帮扶程序的后端,实现了后端的增和查询
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(); } } } }
标签:Toast,26,2024.4,冲刺,entity,etPlace,import,android,etRequest From: https://www.cnblogs.com/Sunyiran/p/18174834