结对作业(地铁项目)11

近期地铁项目作业再次升级,部署到安卓端,我们依旧准备先从后端做起,在连接前端

package com.example.subway.database;

import java.sql.*;

public class DB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
String url = "jdbc:mysql://172.18.56.78:3306/subway?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
String username = "root";
String password = "20223959";
public DB(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,username,password);
stmt = conn.createStatement();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}
}
public void close(){
try {
if (rs!=null){
rs.close();
}
if (stmt!=null){
stmt.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}


}
package com.example.subway.dao;


import com.example.subway.database.DB;
import com.example.subway.entity.SubwayBean;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Sub {

public ArrayList<SubwayBean> ArrayTransferStation(String station1, String station2){

ArrayList<SubwayBean> resultArray = new ArrayList<>();
DB db = new DB();
PreparedStatement preparedStatement=null;
ResultSet rs = null;
int i=0;

try {
String sql ="WITH RECURSIVE transfer (start_station, stop_station, stops, path) AS (\n" +
" SELECT station_name, next_station, 1, CAST(CONCAT(line_name,' ',station_name , '--->', line_name,' ',next_station) AS CHAR(1000))\n" +
" FROM station_table WHERE station_name = ?\n" +
" UNION ALL\n" +
" SELECT p.start_station, e.next_station, stops + 1, CONCAT(p.path, '--->', e.line_name, ' ',e.next_station)\n" +
" FROM transfer p\n" +
" JOIN station_table e\n" +
" ON p.stop_station = e.station_name AND (INSTR(p.path, e.next_station) = 0)\n" +
")\n" +
"SELECT * FROM transfer WHERE stop_station =?;\n";

preparedStatement=db.conn.prepareStatement(sql);
preparedStatement.setString(1,station1);
preparedStatement.setString(2,station2);
rs = preparedStatement.executeQuery();

while (rs.next())
{
i++;
SubwayBean subwayBean = new SubwayBean();
subwayBean._StartStation = (String) rs.getObject(1);
subwayBean._EndStation = (String) rs.getObject(2);
subwayBean._Number = rs.getObject(3);
subwayBean._Path = (String) rs.getObject(4);
resultArray.add(subwayBean);

}
if(i==0)
{
return null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
db.close();
}
return resultArray;
}
public ArrayList<String> ArrayStationLine(String line){

DB db = new DB();
ArrayList<String> station = new ArrayList<>();
String l_name = "'" + line + "'";
try {
String selectQuery = "SELECT * FROM station_table WHERE line_name="+l_name;
db.rs =db.stmt.executeQuery(selectQuery);

while (db.rs.next()){

String base_name=db.rs.getString("station_name");
station.add(base_name);

}

} catch (SQLException e) {
e.printStackTrace();
} finally{
db.close();
}
if (station.size()==0){
return null;
}
return station;
}
public String LineStation(String name){

DB db = new DB();
String na = "'" + name + "'";
String line = "地铁";
try {
String selectQuery = "SELECT * FROM station_table WHERE station_name="+na;
db.rs =db.stmt.executeQuery(selectQuery);

if (db.rs.next()){
String base_name=db.rs.getString("line_name");
line += base_name;
// line += "号线";
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
db.close();
}
if (line == "地铁"){
return null;
}
return line;
}

}
package com.example.subway;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.subway.dao.Sub;
import com.example.subway.entity.SubwayBean;
//import com.example.subway.database.MySQLConnection;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Spinner spiLine;
private EditText ediStation;
private EditText ediStart;
private EditText ediEnd;
private TextView tvStation;

private TextView tvLine;

private TextView tvSe;

// MySQLConnection mySQLConnection = new MySQLConnection();

Sub sub = new Sub();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


spiLine=findViewById(R.id.spiLine);
ediStation=findViewById(R.id.ediStaion);
ediStart=findViewById(R.id.ediStart);
ediEnd=findViewById(R.id.ediEnd);
tvLine=findViewById(R.id.tvLine);
tvStation= findViewById(R.id.tvStation);
tvSe = findViewById(R.id.tvSe);

findViewById(R.id.btnLine).setOnClickListener(this);
findViewById(R.id.btnStation).setOnClickListener(this);
findViewById(R.id.btnSe).setOnClickListener(this);

}

@Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.btnLine){

String select = (String) spiLine.getSelectedItem();
Log.d( "lhw",select);

new Thread(new Runnable() {
@Override
public void run() {
ArrayList<String> l1 = sub.ArrayStationLine(select);

runOnUiThread(new Runnable() {
@Override
public void run() {
tvLine.setText(l1.toString());
}
});

}
}).start();
}else if(id == R.id.btnStation){
String na = ediStation.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
String line = sub.LineStation(na);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvStation.setText(line);
}
});
}
}).start();
}else{
String start = ediStart.getText().toString();
String end = ediEnd.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
ArrayList<SubwayBean> arr = sub.ArrayTransferStation(start,end);
runOnUiThread(new Runnable() {
@Override
public void run() {
String lines = "";
for(int i = 0;i<arr.size();++i){
lines += arr.get(i)._Path + "\n\n";
}
tvSe.setText(lines);
}
});
}
}).start();
}
}
}
posted @   平安喜乐×  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示