结对作业(地铁项目)11
1.新学期目标2.打卡3.打卡24.打卡35.打卡 46.打卡57.打卡68.打卡79.打卡810.打卡911.打卡1012.结对作业(地铁查询项目)0113.结对作业(地铁项目)0214.结对作业(地铁项目)0315.结对作业(地铁项目)0416.结对作业(地铁项目)0517.结对作业(地铁项目)0618.结对作业(地铁项目)0719.结对作业(地铁项目)0820.结对作业(地铁项目)0921.结对作业(地铁项目)10
22.结对作业(地铁项目)11
23.结对作业(地铁项目)1224.结对作业(地铁项目)1325.结对作业(地铁项目)1426.五一冲刺(政策查询系统)127.五一冲刺(政策查询系统)228.政策查询系统(安卓)129.政策查询系统(安卓)230.政策查询系统(安卓)331.政策查询系统(安卓)432.政策查询系统(安卓)533.政策查询系统(安卓)634.政策查询系统(安卓)735.JS开发36.安卓app开发相关37.第一次个人作业(安卓学习记录系统)0138.第一次个人作业(安卓学习记录系统)0239.第一次个人作业(安卓学习记录系统)0340.第一次个人作业(安卓学习记录系统)0441.第一次个人作业(安卓学习记录系统)0542.第一次个人作业(安卓学习记录系统)0643.第一次个人作业(安卓学习记录系统)0744.打卡1145.打卡1246.打卡1347.打卡1448.打卡1549.打卡1650.打卡1751.打卡1852.打卡1953.打卡2054.python学习55.python学习256.python学习357.Javaweb58.打卡2159.fragment学习和使用60.mybits学习161.mybits学习262.mybits学习363.课程总结64.个人总结近期地铁项目作业再次升级,部署到安卓端,我们依旧准备先从后端做起,在连接前端
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();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效