sjz地铁作业
合作人:杨瑞
实现的功能:基本实现功能,能在网页实现最少换乘的路线,还未实现最短路线的功能,页面不够美观,甚至可以说太难看;代码冗余,接下来需要优化。
时间花费:预计花费10个小时,实际花费7个小时;
实验心得:这次是真正尝到了写代码的乐趣,也体会到了算法的重要性,从头到尾感觉到了这个项目的实施过程。通过将一个大问题逐渐分成若干小问题,然后各个击破,再次体会到了分而治之的重要性。
实验算法:
package com.subway.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.subway.Model.Subway;
import com.subway.Util.DBUtil;
public class SubwayDao {
public static Subway load(String zhan) {
//获得链接对象
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_subway where station like ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Subway subway=null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, zhan);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
subway=new Subway();
subway.setId(resultSet.getInt("id"));
subway.setLine(resultSet.getString("line"));
subway.setStation(zhan);
subway.setTransfer(resultSet.getString("transfer"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return subway;
}
public static String [] load(int id1,int id2) {
String []list=new String[200];
int num=0;
//获得链接对象
Connection connection = DBUtil.getConnection();
String sql=null;
//准备sql语句
if(id1<id2) {
sql = "select * from t_subway where id > ? &&id<= ?";
}
else {
sql = "select * from t_subway where id < ? &&id>= ? order by id desc";
}
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id1);
preparedStatement.setInt(2, id2);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
list[num]=resultSet.getString("station");
num++;
}
// for(int i=0;i<num;i++) {
// System.out.println(list[i]);
// }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return list;
}
public static String[] load(Subway subway1,Subway subway2) {
String []list=new String[200];
Subway subway=null;
//获得链接对象
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_subway where line like ? && transfer like ? ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,subway1.getLine());
preparedStatement.setString(2,subway2.getLine());
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
subway=new Subway();
subway.setId(resultSet.getInt("id"));
subway.setLine(resultSet.getString("line"));
subway.setStation(resultSet.getString("station"));
subway.setTransfer(resultSet.getString("transfer"));
}
list=load(subway1.getId(),subway.getId());
int n=0;
for(n=0;n<list.length;n++)
{
if(list[n]==null)
break;
}
list[n-1]=list[n-1]+"(换乘"+subway.getTransfer()+")";
System.out.println("*********************"+n);
sql = "select * from t_subway where station like ? && line != ? ";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,subway.getStation());
preparedStatement.setString(2,subway.getLine());
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
subway=new Subway();
subway.setId(resultSet.getInt("id"));
subway.setLine(resultSet.getString("line"));
subway.setStation(resultSet.getString("station"));
subway.setTransfer(resultSet.getString("transfer"));
}
String[]arr=load(subway.getId(),subway2.getId());
for(int j=0;j<arr.length;j++)
if(arr[j]!=null) {
list[n]=arr[j];
n++;
}
} catch (Exception e) {
// TODO: handle exception
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return list;
}
public String[] load(String qidian,String zhongdian) {
String []list=new String[200];
Subway subway1=load(qidian);
Subway subway2=load(zhongdian);
System.out.println("起点站:"+subway1.getId()+" "+subway1.getLine()+" "+subway1.getStation()+" "+subway1.getTransfer());
System.out.println("终点站:"+subway2.getId()+" "+subway2.getLine()+" "+subway2.getStation()+" "+subway2.getTransfer());
if(subway1.getLine().equals(subway2.getLine()))
list=load(subway1.getId(),subway2.getId());
else
list=load(subway1,subway2);
return list;
}
}
实验截图:




浙公网安备 33010602011771号