结对课堂练习-地铁查询系统(2)
作者:互联网
第一阶段-地铁查询系统:
java代码编写部分:
dao.java:
package Date;
import Model.LineModel;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
public class Dao {
public final String U = "jdbc:mysql://localhost:3306/db?&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true";//db是数据所在的数据库
public final String UName = "root";
public final String PassWord = "";//数据库密码
public LineModel selectkluxian(int no1) {
PreparedStatement ps = null;
Connection con = null;
LineModel kc= new LineModel();
List<String> station = new ArrayList<>();
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(U, UName, PassWord);
String sql = "select * from line where no1 =?";
ps = con.prepareStatement(sql);
ps.setInt(1, no1);
rs = ps.executeQuery();
int count = 0;
while(rs.next())
{
count++;
//System.out.println(count);
int ID=rs.getInt("ID");
String name=rs.getString("name");
//System.out.println(name);
int no=rs.getInt("no1");
if(count == 1) {
kc.setLineName(no + "号线");
}
station.add(name);
//list.add(kc);
}
kc.setStations(station);
return kc;
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(rs!= null) rs.close();
if(ps!= null) ps.close();
if(con!= null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return kc;
}
public List<String> selectzhantai(String name) {
PreparedStatement ps = null;
Connection con = null;
LineModel kc= new LineModel();
List<String> station = new ArrayList<>();
List<String> s = new ArrayList<>();
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(U, UName, PassWord);
String sql = "select * from line where name =?";
ps = con.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
int count = 0;
while(rs.next())
{
String tname=rs.getString("name");
int no = rs.getInt("no1");
//System.out.println(name);
s.add(no+"号线"+" ");
//list.add(kc);
}
return s;
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(rs!= null) rs.close();
if(ps!= null) ps.close();
if(con!= null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return s;
}
public List<String> selectxianlu2(int no1) {
PreparedStatement ps = null;
Connection con = null;
List<String> station = new ArrayList<>();
List<String> s = new ArrayList<>();
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(U, UName, PassWord);
String sql = "select * from line where =?";
ps = con.prepareStatement(sql);
ps.setInt(1, no1);
rs = ps.executeQuery();
int count = 0;
while(rs.next())
{
String name=rs.getString("name");
station.add(name);
//list.add(kc);
}
return station;
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(rs!= null) rs.close();
if(ps!= null) ps.close();
if(con!= null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return station;
}
}
DateInputing.java:
package Date;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import Model.GraphModel;
import Model.LineModel;
public class DateInputing {
public static List<LineModel> FinalLine = new ArrayList<LineModel>(); //地铁线路信息
public List<GraphModel> FinalGraph = new ArrayList<GraphModel>(); //邻接表信息
public DateInputing() throws IOException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("---");
Dao da = new Dao();
int count = 0;
while(true) {
LineModel tmpline = da.selectkluxian(++count);
if(tmpline.getStations().size() == 0) {
continue;
}
if (count == 15) {
break;
}
//存储线路信息
List<String> LineStation = tmpline.getStations();
//System.out.println(LineStation.size() + "-");
String[] tmpdata = new String[tmpline.getStations().size() + 1];
tmpdata[0] = tmpline.getLineName();
for (int i = 1; i <= LineStation.size(); i++) {
tmpdata[i] = LineStation.get(i - 1);
}
FinalLine.add(tmpline);
//将线路信息处理成邻接表
String tmp1 = tmpdata[1];
String tmp2 = tmpdata[2];
//System.out.println(tmp2);
for (int i = 2; i < tmpdata.length; i++) {
List<String> stations = new ArrayList<String>();
List<String> LineName = new ArrayList<String>();
List<String> stations1 = new ArrayList<String>();
List<String> LineName1 = new ArrayList<String>();
GraphModel graphstation = new GraphModel();
GraphModel graphstation1 = new GraphModel();
//判断tmp1是否是已经存储过
int isExit = 0;
int m = 0;
for (int j = 0; j < FinalGraph.size(); j++) {
if (tmp1.equals(FinalGraph.get(j).getStationName())) {
isExit = 1;
m = j; //tmp1在FinalGraph哪一个位置,存储到m中
break;
}
}
//判断tmp2是否是已经存储过
int isExit1 = 0;
int n = 0;
for (int j = 0; j < FinalGraph.size(); j++) {
if (tmp2.equals(FinalGraph.get(j).getStationName())) {
isExit1 = 1;
n = j;
break;
}
}
int p = 0;
if (isExit == 0 && isExit1 == 0) { //tmp1未存储过,tmp2未存储过
graphstation.setStationName(tmp1); //未存储过,新建graphstation类,将数据存入类中,最后压入FinalGraph类
LineName.add(tmpdata[0]);
stations.add(tmp2);
graphstation.setLineName(LineName);
graphstation.setStations(stations);
graphstation1.setStationName(tmp2);
LineName1.add(tmpdata[0]);
stations1.add(tmp1);
graphstation1.setLineName(LineName1);
graphstation1.setStations(stations1);
FinalGraph.add(graphstation);
FinalGraph.add(graphstation1);
} else if (isExit == 0 && isExit1 == 1) { //tmp1未存储过,tmp2已存储过
graphstation.setStationName(tmp1);
LineName.add(tmpdata[0]);
stations.add(tmp2);
graphstation.setLineName(LineName);
graphstation.setStations(stations);
List<String> station = FinalGraph.get(n).getStations(); //已存储过,则在FinalGraph第n个位置,在相邻战点后面加上
List<String> linename = FinalGraph.get(n).getLineName();
station.add(tmp1);
for (int t = 0; t < linename.size(); t++) {
if (tmpdata[0].equals(linename.get(t))) {
p = 1;
break;
}
}
if (p == 0)
linename.add(tmpdata[0]);
FinalGraph.get(n).setLineName(linename);
FinalGraph.get(n).setStations(station);
FinalGraph.add(graphstation);
} else if (isExit == 1 && isExit1 == 0) { //tmp1已存储过,tmp2未存储过
graphstation.setStationName(tmp2);
LineName1.add(tmpdata[0]);
stations1.add(tmp1);
graphstation.setLineName(LineName1);
graphstation.setStations(stations1);
List<String> station = FinalGraph.get(m).getStations();
List<String> linename = FinalGraph.get(m).getLineName();
station.add(tmp2);
for (int t = 0; t < linename.size(); t++) {
if (tmpdata[0].equals(linename.get(t))) {
p = 1;
break;
}
}
if (p == 0)
linename.add(tmpdata[0]);
FinalGraph.get(m).setLineName(linename);
FinalGraph.get(m).setStations(station);
FinalGraph.add(graphstation);
} else if (isExit == 1 && isExit1 == 1) { //tmp1,tmp2都已存储过
List<String> station = FinalGraph.get(m).getStations();
List<String> linename = FinalGraph.get(m).getLineName();
station.add(tmp2);
for (int t = 0; t < linename.size(); t++) {
if (tmpdata[0].equals(linename.get(t))) {
p = 1;
break;
}
}
if (p == 0)
linename.add(tmpdata[0]);
FinalGraph.get(m).setLineName(linename);
FinalGraph.get(m).setStations(station);
List<String> station1 = FinalGraph.get(n).getStations();
List<String> linename1 = FinalGraph.get(n).getLineName();
station1.add(tmp1);
for (int t = 0; t < linename1.size(); t++) {
if (tmpdata[0].equals(linename1.get(t))) {
p = 1;
break;
}
}
if (p == 0)
linename1.add(tmpdata[0]);
FinalGraph.get(n).setLineName(linename1);
FinalGraph.get(n).setStations(station1);
}
tmp1 = tmp2;
if (i + 1 < tmpdata.length)
tmp2 = tmpdata[i + 1];
else
break;
}
}
}
}
标签:结对,课堂练习,get,int,rs,List,FinalGraph,add,地铁 来源: https://www.cnblogs.com/wlqyyds/p/16298170.html