2024.4.1(周一)进度
完善地铁app的查询功能,截止到今天已完成线路查询、站点查询、起终点查询
package com.example.beijingsubway; import android.os.Bundle; import android.annotation.SuppressLint; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import android.widget.EditText; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class FpsActivity extends AppCompatActivity implements View.OnClickListener { private Button fps_find; private TextView textView; private EditText linename; private static final int TEST_USER_SELECT = 1; @SuppressLint("HandlerLeak") private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { int SubwayID; switch (msg.what) { case TEST_USER_SELECT: Subway subway = (Subway) msg.obj; SubwayID = subway.getId(); System.out.println("***********"); System.out.println("***********"); System.out.println("id:" + SubwayID); textView.setText(Integer.toString(SubwayID)); break; } } }; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fps); fps_find = findViewById(R.id.fps_find); textView = findViewById(R.id.fps_result); linename = findViewById(R.id.line_name); fps_find.setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.fps_find) { String lineNameText = linename.getText().toString(); if (!lineNameText.isEmpty()) { int inputId = Integer.parseInt(lineNameText); new Thread(new Runnable() { @Override public void run() { try { Connection conn = DBOpenHelper.getConn(); if (conn != null) { String sql = "SELECT * FROM subway WHERE id = ?"; try (PreparedStatement pst = conn.prepareStatement(sql)) { pst.setInt(1, inputId); try (ResultSet rs = pst.executeQuery()) { StringBuilder resultText = new StringBuilder(); while (rs.next()) { int id = rs.getInt("id"); String s_station = rs.getString("s_station"); String e_station = rs.getString("e_station"); String path = rs.getString("path"); resultText.append("线路: ").append(id) .append("\n起点站: ").append(s_station) .append("\n终点站: ").append(e_station) .append("\n路线: ").append(path) .append("\n\n"); } runOnUiThread(new Runnable() { @Override public void run() { textView.setText(resultText.toString()); } }); } } conn.close(); } else { runOnUiThread(new Runnable() { @Override public void run() { Snackbar.make(fps_find, "无法连接到数据库", Snackbar.LENGTH_SHORT).show(); } }); } } catch (SQLException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Snackbar.make(fps_find, "查询操作失败", Snackbar.LENGTH_SHORT).show(); } }); } } }).start(); } else { Snackbar.make(fps_find, "请输入线路名称", Snackbar.LENGTH_SHORT).show(); } } } }