首页 > 其他分享 >03地铁查询系统

03地铁查询系统

时间:2023-06-10 22:45:39浏览次数:37  
标签:03 layout 查询 content 地铁 import android data id

地铁查询系统

2023.6.10

1、优化返回两个站点之间最短路径功能:成为一个类,进行单元测试。

2、生成遍历车站类:要求尽可能快地遍历地铁的所有车站呢(只用经过一次,不用下车,就算经过车站)。

 

 

连接数据库:

package com.example.underground;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

 

public class DBUtil {

 

    //连接数据库

    public Connection getConnection()  {

        try {

            Class.forName("com.mysql.jdbc.Driver");

        } catch (ClassNotFoundException e) {

            throw new RuntimeException(e);

        }

        Connection con = null;

        try {

            con = DriverManager.getConnection("jdbc:mysql://10.99.112.73:3306/dab2?useUnicode=true&characterEncoding=utf8", "zsf", "1234");

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

        return con;

    }

 

    //check1

    public String getCheck1(String text) throws SQLException {

 

        String data="此地铁线路途径站点如下:"+"\n";

        Connection connection = getConnection();

        PreparedStatement preparedStatement = null;

        String sql = "select distinct station_name from bj_subway where line_name like ?";

        preparedStatement=connection.prepareStatement(sql);

        preparedStatement.setString(1, "%"+text+"%");

        ResultSet rs=preparedStatement.executeQuery();

 

        while(rs.next()){

            data+=rs.getString("station_name");

            data+="\n";

 

        }

        return data;

    }

 

    //check2

    public String getCheck2(String text) throws SQLException {

 

        String data="途径该站点的线路如下:"+"\n";

        Connection connection = getConnection();

        PreparedStatement preparedStatement = null;

        String sql = "select distinct line_name from bj_subway where station_name = ?";

        preparedStatement=connection.prepareStatement(sql);

        preparedStatement.setString(1, text);

        ResultSet rs=preparedStatement.executeQuery();

 

        while(rs.next()){

            data+=rs.getString("line_name");

            data+="\n";

 

        }

        return data;

    }

 

    //check3

    public String getCheck3(String text1,String text2) throws SQLException {

 

        String data="";

        Connection connection = getConnection();

        PreparedStatement preparedStatement = null;

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

        preparedStatement=connection.prepareStatement(sql);

        preparedStatement.setString(1, text1);

        preparedStatement.setString(2, text2);

        ResultSet rs=preparedStatement.executeQuery();

 

        while(rs.next()){

            int stops=rs.getInt("stops")+1;

            data=data+"共经过"+stops+"站:\n";

            data+=rs.getString("path");

            data+="\n";

            break;

 

        }

        return data;

    }

 

 

}

页面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="@mipmap/dietie"

    tools:context=".MainActivity">

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="30dp"

        android:layout_marginTop="40dp"

        android:layout_marginRight="30dp"

        android:gravity="center"

        android:orientation="horizontal">

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="线路查询"

            android:textColor="@color/black"

            android:textSize="30sp" />

 

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="20dp"

        android:layout_marginTop="15dp"

        android:layout_marginRight="20dp"

        android:gravity="center_vertical"

        android:orientation="horizontal">

 

        <EditText

            android:id="@+id/et_check1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"

            android:gravity="center_horizontal"

            android:hint="请输入要查询的线路"

            android:selectAllOnFocus="true"

            android:inputType="text"

            android:paddingLeft="10dp"

            android:textSize="18sp" />

 

    </LinearLayout>

 

 

    <Button

        android:id="@+id/btn_check1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="100dp"

        android:layout_marginTop="10dp"

        android:layout_marginRight="100dp"

        android:text="线路查询"

        android:textSize="30sp" />

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="30dp"

        android:layout_marginTop="10dp"

        android:layout_marginRight="30dp"

        android:gravity="center"

        android:orientation="horizontal">

 

        <ScrollView

            android:id="@+id/sv"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            >

        <TextView

            android:id="@+id/tv_check1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="null"

            android:textColor="@color/black"

            android:textSize="25sp" />

        </ScrollView>

 

    </LinearLayout>

 

 

</LinearLayout>

页面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="@mipmap/dietie"

    tools:context=".MainActivity">

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="30dp"

        android:layout_marginTop="40dp"

        android:layout_marginRight="30dp"

        android:gravity="center"

        android:orientation="horizontal">

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="线路查询"

            android:textColor="@color/black"

            android:textSize="30sp" />

 

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="20dp"

        android:layout_marginTop="15dp"

        android:layout_marginRight="20dp"

        android:gravity="center_vertical"

        android:orientation="horizontal">

 

        <EditText

            android:id="@+id/et_check1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"

            android:gravity="center_horizontal"

            android:hint="请输入要查询的线路"

            android:selectAllOnFocus="true"

            android:inputType="text"

            android:paddingLeft="10dp"

            android:textSize="18sp" />

 

    </LinearLayout>

 

 

    <Button

        android:id="@+id/btn_check1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="100dp"

        android:layout_marginTop="10dp"

        android:layout_marginRight="100dp"

        android:text="线路查询"

        android:textSize="30sp" />

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="30dp"

        android:layout_marginTop="10dp"

        android:layout_marginRight="30dp"

        android:gravity="center"

        android:orientation="horizontal">

 

        <ScrollView

            android:id="@+id/sv"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            >

        <TextView

            android:id="@+id/tv_check1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="null"

            android:textColor="@color/black"

            android:textSize="25sp" />

        </ScrollView>

 

    </LinearLayout>

 

 

</LinearLayout>

   

 

 

package com.example.underground;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.os.StrictMode;

import android.text.TextUtils;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

import java.sql.SQLException;

 

public class Check2Activity extends AppCompatActivity implements View.OnClickListener {

 

    private DBUtil util;

    private EditText etCheck2;

    private Button btnCheck2;

    private TextView tvCheck2;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_check2);

        setTitle("站点查询");

 

        util = new DBUtil();

        //忘了干啥的了

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

 

        etCheck2 = findViewById(R.id.et_check2);

        tvCheck2 = findViewById(R.id.tv_check2);

        btnCheck2 = findViewById(R.id.btn_check2);

 

        btnCheck2.setOnClickListener(this);

    }

 

    @Override

    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_check2:

                String text = etCheck2.getText().toString().trim();

                String data= "";

                if(TextUtils.isEmpty(text)){

                    data="输入为空!";

                }

                else {

                    try {

                        data = util.getCheck2(text);

                    } catch (SQLException e) {

                        data = "站点不存在!";

                    }

                    if(data.equals("途径该站点的线路如下:\n")){

                        data="站点不存在!";

                    }

                }

                tvCheck2.setText(data);

                break;

        }

    }

}

 

 

 

 

 

package com.example.underground;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.os.StrictMode;

import android.text.TextUtils;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

import java.sql.SQLException;

 

public class Check3Activity extends AppCompatActivity implements View.OnClickListener {

 

    private DBUtil util;

    private EditText etCheck31;

    private EditText etCheck32;

    private Button btnCheck3;

    private TextView tvCheck3;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_check3);

        setTitle("起点—终点查询");

 

        util = new DBUtil();

        //忘了干啥的了

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

 

        etCheck31 = findViewById(R.id.et_check31);

        etCheck32 = findViewById(R.id.et_check32);

        tvCheck3 = findViewById(R.id.tv_check3);

        btnCheck3 = findViewById(R.id.btn_check3);

 

        btnCheck3.setOnClickListener(this);

    }

 

    @Override

    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_check3:

                String text1 = etCheck31.getText().toString().trim();

                String text2 = etCheck32.getText().toString().trim();

                String data= "";

                if(TextUtils.isEmpty(text1)||TextUtils.isEmpty(text2)){

                    data="输入为空!";

                }

                else {

                    try {

                        data = util.getCheck3(text1,text2);

                    } catch (SQLException e) {

                        data = "两站点间不能通行!";

                    }

                    if(data.equals("")){

                        data="两站点间不能通行!";

                    }

                }

                tvCheck3.setText(data);

                break;

        }

    }

}

 

标签:03,layout,查询,content,地铁,import,android,data,id
From: https://www.cnblogs.com/ZRGUGUGU818/p/17472116.html

相关文章

  • SpringBoot进阶教程(七十六)多维度排序查询
    在项目中经常能遇到,需要对某些数据集合进行多维度排序的需求。对于集合多条件排序解决方案也有很多,今天我们就介绍一种,思路大致是设置一个分值的集合,这个分值是按照需求来设定大小的,再根据分值的大小对集合排序。v需求背景我们来模拟一个需求,现在需要查询一个用户列表,该列表......
  • 202303-天天向上队 实验七 综合软件项目案例
    项目内容课程班级博客链接2023年春软件工程这个作业要求链接实验七综合软件项目案例团队名称天天向上队团队的课程学习目标(1)练习用例图、类图、顺序图、状态图等UML建模技术在软件开发过程中的用途。(2)掌握软件项目的数据库逻辑结构设计方法。(3)掌握软件项目......
  • jmeter003:(HTTP请求默认值)元件
    HTTP请求默认值作用:当有多个请求的协议、ip、端口号、路径、内容编码、参数、消息数据体是一样时,可以使用http请求默认来代替多个请求值添加路径:线程组>配置元件>HTTP请求默认值使用方法:如下图,(请求1、请求2、请求3)添加了(HTTP请求默认值)之后,请求数据取的都是(HTTP请求默认值)里面......
  • StarWind V2V Converter报错V2V convert to VMDK 'unrecoverable error' on win10
    解决方法:避免在源名称和目标名称中使用任何非ASCII符号修改文件名,不能使用特殊字符  VMwareVirtualDiskDevelopmentKitErrorVMwareVirtualDiskDevelopmentKitunrecoverableerror:(vthread-4)NOTIMPLEMENTEDd:/build/ob/bora-13861102/bora/lib/unicode/unic......
  • ALEXA排名代表什么?网站ALEXA排名怎么查询?
    ALEXA排名是根据一个站点的流量数据来列出的排名,排名越靠前,说明该网站的流量越大,被访问的次数也越多。 ......
  • 使用clickhouse和mysql查询时间对比
    业务场景,对于数据量过大的数据统计,跑脚本会很吃力先建立一个clickhouse的mysql引擎表关联本地mysql数据表,以下这个表会自动同步mysql主表数据CREATETABLEtest_table(idUInt32,messageString,contentString,remarkString,order_idString,user_idUInt......
  • Python操作Excel文件中多WorkSheet模拟数据库内连接查询
    严格意义上来讲,是可以把Excel文件看作数据库的,C#通过OLEDB.net就可以使用SQL语句操作Excel文件中的数据。本文代码使用Python扩展库openpyxl操作Excel文件中多个WorkSheet中的数据,模拟了数据库的内连接。假设Excel文件名为data.xlsx,其中第一个WorkSheet数据如下:第二个WorkSheet数据......
  • 分页查询和条件分页查询
    分页查询分析:分析文档要求查看前端传递给后台的参数分析参数进行编码后台返回给前端的数据思路浏览器->Controller层->Service层->Mapper层->数据库设置分页拦截器@ConfigurationpublicclassMybatisPlusConfig{@BeanpublicMybati......
  • Failed to bind properties under 'spring.datasource.primary' to javax.sql.DataSou
     2023-06-1011:04:13.778WARN22452---[main]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Error......
  • Failed to bind properties under 'spring.datasource.primary' to javax.sql.DataSou
     2023-06-1011:04:13.778WARN22452---[main]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Error......