每日总结2023/3/20
地铁查询app系统,通过路线查询此路线包含站点
以下是程序源码
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:padding="30dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:text="欢迎来到北京地铁查询系统" android:textSize="30dp" android:textStyle="bold" /> <Button android:id="@+id/btn_enter" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击进入路线查询" android:textSize="20dp" android:textStyle="bold" android:layout_marginTop="5dp"/> <Button android:id="@+id/btn_teacher" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击查询你的目的路线" android:textSize="20dp" android:textStyle="bold" android:layout_marginTop="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="64dp" android:layout_marginTop="10dp" android:background="@drawable/bj" android:gravity="center" android:text="欢迎来到北京地铁查询系统" android:textSize="30dp" android:textStyle="bold" /> </LinearLayout>
MainActivity
package com.example.a86191.subway; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; ;import com.example.a86191.subway.util.Commonutil; import com.example.a86191.subway.util.SqliteHelper; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_teacher; private SqliteHelper mHelper; private String mDatabaseName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_enter).setOnClickListener(this); mDatabaseName = getFilesDir() + "/test.db"; btn_teacher=findViewById(R.id.btn_teacher); btn_teacher.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent i5 = new Intent(MainActivity.this,DianQureyActivity.class); startActivity(i5); } }); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_enter: //创建或者打开数据库,如果数据库不存在就创建,如果存在的话就打开数据库 SQLiteDatabase db = openOrCreateDatabase(mDatabaseName, Context.MODE_PRIVATE,null); Commonutil.showLongMsg(this,"欢迎!"); Intent intent = new Intent(this, LineQureyActivity.class); startActivity(intent); break; } } @Override protected void onStart() { super.onStart(); //获取数据库帮助器的实例 mHelper = SqliteHelper.getInstance(this); //打开数据库帮助器的读写连接 mHelper.openWriteLink(); mHelper.openReadLink(); } @Override protected void onStop() { super.onStop(); //关闭数据库连接 //mHelper.closeLink(); } }
line_query.xml
<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="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="请输入您需要查询的路线" android:textSize="40dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:text="路线号:" android:textSize="25dp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="5dp"/> <EditText android:id="@+id/find_id_ET" android:layout_margin="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <Button android:id="@+id/find_id_But" android:layout_width="183dp" android:layout_height="wrap_content" android:text="进行查询" android:layout_gravity="center"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/find_id_tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView> </LinearLayout>
LineQuery
package com.example.a86191.subway; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.example.a86191.subway.Bean.firstline; import com.example.a86191.subway.Dao.UserDao; import java.util.ArrayList; public class LineQureyActivity extends AppCompatActivity implements View.OnClickListener { private EditText et; private String desc; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line_qurey); findViewById(R.id.find_id_But).setOnClickListener(this); et = findViewById(R.id.find_id_ET); tv = findViewById(R.id.find_id_tv); } final Handler hand1 = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); tv.setText(desc); } }; @Override public void onClick(View view) { new Thread() { @Override public void run() { String Line=et.getText().toString(); UserDao ab=new UserDao(); ArrayList<firstline> p=ab.findLine(Line); desc=""; for (int i = 0; i < p.size(); i++) { desc = desc + "该路线为:" + p.get(i).getStopName() + "\n"; } int msg=1; hand1.sendEmptyMessage(msg); } }.start(); } }
Bean文件下的firstline
package com.example.a86191.subway.Bean; /** * Created by 86191 on 2023/3/19. */ public class firstline { private String StopID; private String StopName; private String Line; public firstline() { } public firstline(String StopID, String StopName,String Line) { this.StopID=StopID; this.StopName=StopName; this.Line=Line; } public String getStopID() { return StopID; } public void setStopID(String stopID) { StopID = stopID; } public String getStopName() { return StopName; } public void setStopName(String stopName) { StopName = stopName; } public String getLine() { return Line; } public void setLine(String line) { Line = line; } }
UserDao
package com.example.a86191.subway.Dao; import com.example.a86191.subway.Bean.firstline; import com.example.a86191.subway.util.MysqlHelper; import java.util.ArrayList; /** * Created by 86191 on 2023/3/19. */ public class UserDao extends MysqlHelper{ public ArrayList<firstline> findLine(String Line) { ArrayList<firstline> item = new ArrayList(); try{ int i = 0; getConnection(); String sql = "select * from firstline where Line=?"; pStmt = conn.prepareStatement(sql); pStmt.setString(1, String.valueOf(Line)); rs = pStmt.executeQuery(); while(rs.next()){ firstline f= new firstline( rs.getString("StopID"), rs.getString("StopName"), rs.getString("Line") ); item.add(f); } return item; }catch (Exception e){ e.printStackTrace(); }finally { closeAll(); } return null; } }
util包下的Commountil
package com.example.a86191.subway.util; /** * Created by 86191 on 2023/3/15. */ import android.app.AlertDialog; import android.content.Context; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; /* *自定义的通用工具类 */ public class Commonutil { /* *显示短消息 */ public static void showShortMsg(Context context, String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /* *显示长消息 */ public static void showLongMsg(Context context, String msg){ Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); } /* *显示消息对话框 */ public static void showDigMsg(Context context, String msg){ new AlertDialog.Builder(context) .setTitle("提示信息") .setMessage(msg) .setPositiveButton("确定",null) .setNegativeButton("取消",null) .create().show(); } public static String getNowDate(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(new Date()); } }
MysqlHelper
package com.example.a86191.subway.util; /** * Created by 86191 on 2023/3/15. */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; /* *Mysql数据库的连接辅助类 */ public class MysqlHelper { private static final String user = "root"; private static final String password = "zhaowenzhe1007"; private static final String dbName = "subway"; private static final String ip = "172.18.47.146"; private static final int port = 5418; private static final String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false"; public static Connection conn; //连接对象 public static Statement stmt; //命令集 public static PreparedStatement pStmt; //预编译命令集 public static ResultSet rs; //结果集 /* *获取连接 */ public static void getConnection(){ try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,user,password); System.out.print(111); } catch (Exception e){ e.printStackTrace(); System.out.print(996); } } /* *关闭连接 */ public static void closeAll(){ try { if(rs!=null){ rs.close(); rs = null; } if(stmt!=null){ stmt.close(); stmt = null; } if(pStmt!=null){ pStmt.close(); pStmt = null; } if(conn!=null){ conn.close(); conn = null; } } catch (Exception e){ e.printStackTrace(); } } }
SqliteHelper
package com.example.a86191.subway.util; /** * Created by 86191 on 2023/3/15. */ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SqliteHelper extends SQLiteOpenHelper { private static final String DB_NAME = "subway.db";//数据库名字 private static final int DB_VERSION = 1; //数据库版本 private static final String TABLE_NAME1 = "firstline";//数据表名字 private static final String TABLE_NAME2 = "changeline"; private static SqliteHelper mHelper = null; //数据库帮助器 private SQLiteDatabase mRDB = null; //用来读 private SQLiteDatabase mWDB = null; //用来写 private SqliteHelper(Context context){ super(context, DB_NAME, null ,DB_VERSION); } //利用单例模式获取数据库帮助器的唯一实例 public static SqliteHelper getInstance(Context context){ if (mHelper == null){ mHelper = new SqliteHelper(context); } return mHelper; } //打开数据库的读连接 public SQLiteDatabase openReadLink(){ if (mRDB == null || !mRDB.isOpen()){ mRDB = mHelper.getReadableDatabase(); } return mRDB; } //打开数据库的写连接 public SQLiteDatabase openWriteLink(){ if (mWDB == null || !mWDB.isOpen()){ mWDB = mHelper.getWritableDatabase(); } return mWDB; } //关闭数据库连接 public void closeLink(){ if (mRDB != null && mRDB.isOpen()){ mRDB.close(); mRDB = null; } if (mWDB != null && mWDB.isOpen()){ mWDB.close(); mWDB = null; } } //创建数据库,执行建表语句 @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { /* *储存打卡信息表 */ String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME2 + "(" + " ID1 INTEGER NOT NULL, " + " ID2 TEXT not null, " + " ChangeStopName VARCHAR not null)"; sqLiteDatabase.execSQL(sql); } //数据库版本更新时会执行,如用于添加新字段。。。 //当前版本DB_VERSION为1,将其更新为2后,会执行该函数 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }