📄 procedure.java
字号:
//用于主页的处理
package gongjiaochexitong.gongjiaoche.main_pkg;
import gongjiaochexitong.gongjiaoche.*;
import java.sql.*;
import java.util.Vector;
public class procedure{
//获取相关车次信息
public static Vector getCheCi_List(conn_Bus db) throws Exception{
Vector cheCiList=new Vector();
ResultSet rs;
String sql="select * from businfo";
rs=db.exeQuery(sql); //
while(rs.next()){
businfo checi=new businfo();
checi.setBusNo(rs.getInt("busNo"));
checi.setBusType(rs.getString("busType"));
checi.setStartTime(rs.getTime("startTime"));
checi.setEndTime(rs.getTime("endTime"));
checi.setStartStation(rs.getInt("startStation"));
checi.setEndStation(rs.getInt("endStation"));
cheCiList.add(checi);
}
return cheCiList;
}
//获取相关站点信息
public static Vector getZhanDian_List(conn_Bus db) throws Exception{
Vector zhanDianList=new Vector();
ResultSet rs;
String sql="select stationNo,stationName from stationinfo";
rs=db.exeQuery(sql);
while(rs.next()){
stationinfo station=new stationinfo();
station.setStationNo(rs.getInt("stationNo"));
station.setStationName(rs.getString("stationName"));
//System.out.println("--------------1");
//System.out.println(rs.getString("stationName"));
zhanDianList.add(station);
}
return zhanDianList;
}
//判断上下行行程上站点是否相同(上下行行程路线是否相同.相同:flag=1;不同:flag=2)
public static int isJourney(conn_Bus db,int checihao) throws Exception{
int flag=0;
ResultSet rs;
String sql="select distinct journey from course where course.busNo='"+checihao+"'";
rs=db.exeQuery(sql);
while(rs.next()){
flag++;
}
return flag;
}
//获取某车次在上行或下行行程上的站点序列(单行程的)
public static Vector searchJourney(conn_Bus db,int checihao,String journey) throws Exception{
Vector resultList=new Vector();
ResultSet rs;
String sql="select stationName from stationinfo, course "
+"where stationinfo.stationNo=course.stationNo "
+"and course.busNo= '"+checihao+"' and "
+"course.journey= '"+journey+"' order by seqNum asc";
rs=db.exeQuery(sql);
while(rs.next()){
resultList.add(rs.getString("stationName"));
}
return resultList;
}
//获取某车次在上下行行程上的站点序列
public static Vector search_CheCiHao(conn_Bus db,int checihao) throws Exception{
Vector resultList = new Vector(); //
Vector resultCheCiHao=new Vector(); //存放车次号
Vector journeyDown = new Vector(); //journey="down"
Vector journeyUp = new Vector(); //journey="up"
int flag=0;
flag=isJourney(db,checihao);
resultCheCiHao.add(checihao);
resultList.add(0,resultCheCiHao);
if(flag==1){
journeyDown = searchJourney(db,checihao,"down");
resultList.add(1,journeyDown); //
}
else if(flag==2){
journeyDown = searchJourney(db,checihao,"down");
resultList.add(1,journeyDown); //
journeyUp = searchJourney(db,checihao,"up");
resultList.add(2,journeyUp); //
}
return resultList;
}
//获取经过此站点的所有车次及这些车次的行程
public static Vector search_ZhanDianMing(conn_Bus db, String zhandian) throws Exception{
Vector resultList = new Vector(); //第一个单元放车次号集
Vector resultSearchCheCiHao=new Vector(); //search the resultset of the checihao
ResultSet rs;
int checihao=0;
String sql="select distinct busNo from course,stationinfo "
+"where course.stationNo=stationinfo.stationNo and stationName='"+zhandian+"'";
rs=db.exeQuery(sql);
while(rs.next()){
checihao=rs.getInt("busNo");
resultSearchCheCiHao=search_CheCiHao(db,checihao);
resultList.add(resultSearchCheCiHao);
}
return resultList;
}
//查询经过两站点的所有车次号(并集,mysql 不支持集合交操作)
public static ResultSet searchSameCheCiHao(conn_Bus db,String qidian,String zhongdian) throws Exception{
ResultSet rs=null;
String sql="(select distinct busNo from course,stationinfo "
+"where course.stationNo=stationinfo.stationNo and stationName='"+qidian+"') "
+"union distinct "
+"(select distinct busNo from course,stationinfo "
+"where course.stationNo=stationinfo.stationNo and stationName='"+zhongdian+"')";
rs=db.exeQuery(sql);
return rs;
}
//判断某车次是是否经过某站点
public static boolean isCheCiToStation(conn_Bus db,int checihao,String stationName) throws Exception{
ResultSet rs=null;
boolean exist=false;
String sql="select distinct stationinfo.stationNo "
+"from stationinfo, course "
+"where busNo='"+checihao+"' and stationName='"+stationName+"' and stationinfo.stationNo=course.stationNo";
rs=db.exeQuery(sql);
if(rs.next()) exist=true;
return exist;
}
//清点某站点在某车次行程中的个数
public static int countStation(conn_Bus db,int checihao,String stationName) throws Exception{
ResultSet rs=null;
int num=0;
String sql="select count(course.stationNo) from course,stationinfo "
+"where busNo='"+checihao+"' and course.stationNo=stationinfo.stationNo "
+"and journey='down' and stationName='"+stationName+"'";
rs=db.exeQuery(sql);
if(rs.next()) num=rs.getInt(1);
return num;
}
//查询站点到站点的所有车次情况
public static Vector search_XIANLU(conn_Bus db,String qidian,String zhongdian) throws Exception{
Vector resultList = new Vector();
ResultSet rs,rs2,rs3;
int checihao=0;
boolean flag=false; //true:qidian and zhongdian is in the same route;
//false:qidian and zhongdian is not in the same route;
rs=searchSameCheCiHao(db,qidian,zhongdian); //查询到的是经过这两个站点的所有车次
while(rs.next()){
checihao=rs.getInt("busNo");
if(qidian.equals(zhongdian)){ //起点与终点相同时,此车次不管上行或下行都会经过两次
if(countStation(db,checihao,qidian)==2){
resultList.add(search_CheCiHao(db,checihao));
}
}
else{
//判断起点与终点是否都在此车次的行程上
if(isCheCiToStation(db,checihao,qidian) &&
isCheCiToStation(db,checihao,zhongdian)){
resultList.add(search_CheCiHao(db,checihao));
}
}
}
if(resultList.size()==0) resultList=null; //resultList初化了,所以获取的向量不为空
return resultList;
}
//将向量中的站点取出,进行打印(查询车次号时)(只显示行程)
public static String transformSearchResultSet(Vector result){
String result2 = "";
Vector v_checihao=(Vector)result.get(0); //获取车次号
String s_checihao="";
s_checihao=s_checihao+v_checihao.get(0);
int checihao=Integer.parseInt(s_checihao); //Vector对象的get(int) 返回值为一个对象,不能直接转化为基本类型的变量
if(result.size()==2){
Vector journeyDown = (Vector)result.get(1);
result2 ="\n"+checihao+"路行程途中经过 "+journeyDown.size()+" 个站点\n";
for(int i=0;i<journeyDown.size();i++){
if(i==0) result2=result2+journeyDown.get(i);
else if(i%5==0) result2 =result2+"\n"+" ===>>"+journeyDown.get(i);
else result2 =result2+ " ===>>"+journeyDown.get(i);
}
}
else if(result.size()==3){
Vector journeyDown = (Vector)result.get(1);
result2 =result2+ "\n行程途中经过 "+journeyDown.size()+" 个站点"+"\n";
result2 =result2+ "下行行程:\n";
for(int i=0;i<journeyDown.size();i++){
if(i==0) result2=result2+journeyDown.get(i);
else if(i%5==0 && i!=0) result2 =result2+"\n"+" ===>>"+journeyDown.get(i);
else result2 =result2+ " ===>>"+journeyDown.get(i);
}
Vector journeyUp = (Vector)result.get(2);
result2 += "\n上行行程:\n";
for(int i=0;i<journeyUp.size();i++){
if(i==0) result2=result2+journeyUp.get(i);
else if(i%5==0) result2 =result2+"\n"+" ===>>"+journeyUp.get(i);
else result2 =result2+ " ===>>"+journeyUp.get(i);
}
}
//result2=result2.replaceAll("\n","<br>");
return result2;
}
//将向量中的站点取出,进行打印(只显示行程)
public static String transformSearchResultSet2(Vector result){
String result2 = "";
for(int i=0;i<result.size();i++){
result2=result2+transformSearchResultSet((Vector)result.get(i));
result2=result2+"\n\n";
}
return result2;
}
//从结果集中获取车次号,对应着 transformSearchResultSet(Vector)
public static int transformSearchResultSet_ChiCiHao(Vector result){
Vector v_checihao=(Vector)result.get(0); //获取车次号
String s_checihao="";
s_checihao=s_checihao+v_checihao.get(0);
int checihao=Integer.parseInt(s_checihao);
return checihao;
}
//将刚才取出的数据(车次号)连接起来,对应着 transformSearchResultSet2(result)
public static String transformSearchResultSet_ChiCiHao2(Vector result){
String result1 = "";
for(int i=0;i<result.size();i++)
result1=result1+transformSearchResultSet_ChiCiHao((Vector)result.get(i))+"路\n\n";
return result1;
}
//查找一次转车
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -