第一次结对开发作业——网页版
本次是我第一次和同学一起结队开发项目,学到了很多新知识,也在沟通中学到了很多与人相处沟通的方法。
以下是项目代码:
1.Line
package Bean;
import java.util.ArrayList;
/*
*
*/
public class Line
{
private String startstopname;//起始站站名
private String endstopname;//结束站站名
private int startstopID;// 起始站站编号
private int endstopID;//结束站站编号
private String startline1=null;//起始点的第一条线
private String startline2=null;//起始点第二条线
private String endline1=null;//结束点的第一条线
private String endline2=null;//结束点的第二条线
private ArrayList<String> array = new ArrayList<String>();//存放过程站点
private int originline;//起始站所在线路
private int finishline;//结束点所在线路
private String middlestop;//中转站站名
private String temporarystartstopname;//临时起始站点名称
private String temporaryendstopname;//临时终点站点名称
private int temporaryline=0;//临时线路
public Line() {
}
public int getTemporaryline() {
return temporaryline;
}
public void setTemporaryline(int temporaryline) {
this.temporaryline = temporaryline;
}
public String getTemporarystartstopname() {
return temporarystartstopname;
}
public void setTemporarystartstopname(String temporarystartstopname) {
this.temporarystartstopname = temporarystartstopname;
}
public String getTemporaryendstopname() {
return temporaryendstopname;
}
public void setTemporaryendstopname(String temporaryendstopname) {
this.temporaryendstopname = temporaryendstopname;
}
public String getMiddlestop() {
return middlestop;
}
public void setMiddlestop(String middlestop) {
this.middlestop = middlestop;
}
public int getOriginline() {
return originline;
}
public void setOriginline(int originline) {
this.originline = originline;
}
public int getFinishline() {
return finishline;
}
public void setFinishline(int finishline) {
this.finishline = finishline;
}
public ArrayList<String> getArray() {
return array;
}
public void setArray(ArrayList<String> array) {
this.array = array;
}
public String getStartline1() {
return startline1;
}
public void setStartline1(String startline1) {
this.startline1 = startline1;
}
public String getStartline2() {
return startline2;
}
public void setStartline2(String startline2) {
this.startline2 = startline2;
}
public String getEndline1() {
return endline1;
}
public void setEndline1(String endline1) {
this.endline1 = endline1;
}
public String getEndline2() {
return endline2;
}
public void setEndline2(String endline2) {
this.endline2 = endline2;
}
public String getStartstopname() {
return startstopname;
}
public void setStartstopname(String startstopname) {
this.startstopname = startstopname;
}
public String getEndstopname() {
return endstopname;
}
public void setEndstopname(String endstopname) {
this.endstopname = endstopname;
}
public int getStartstopID() {
return startstopID;
}
public void setStartstopID(int startstopID) {
this.startstopID = startstopID;
}
public int getEndstopID() {
return endstopID;
}
public void setEndstopID(int endstopID) {
this.endstopID = endstopID;
}
}
2.JdbcUtils
package Dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/*
* �����������ݿ�
*/
import java.sql.*;
public class JdbcUtils {
private static Connection connection = null;
private static String url = "jdbc:mysql://localhost:3306/subway";
private static String usrname = "root";
private static String password = "1003392478";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
connection = DriverManager.getConnection(url, usrname, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void release(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.NJDao
package Dao;
public class NJDao {
}
4.SJZDao
package Dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RunnableScheduledFuture;
import Bean.Line;
public class SJZDao {
int demostartid=0,demoendid=0;
ArrayList<String> array=new ArrayList<String>();
/*
* 起始点与终点在一条线上且非换乘站
*/
public List<String> find1(String line){
List<String>list=new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
String sql = "select * from SJZfirstline where Line=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, line);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String s=resultSet.getString(2);
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(connection, preparedStatement, resultSet);
}
return list;
}
public ArrayList<String> Connectonlyoneline(Line line) throws ClassNotFoundException
{
ArrayList<String> array1=new ArrayList<String>();
int c=0;
try {
//连接数据库
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
//是否成功连接
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
String sql="SELECT * FROM SJZfirstline f1 JOIN SJZfirstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证起始站(同时有可能是中转站)与终点站(同时有可能是中转站)在同一条线路上
ResultSet rs=statement.executeQuery(sql);
while(rs.next()) {
line.setTemporaryline(rs.getInt("Line"));
}
//准备数据库查询:将起始站和终点站的中间站
System.out.println(line.getTemporaryline());
array.add("乘坐"+line.getTemporaryline()+"号线");
c=select(line); //得到起始站与终点站的顺序
System.out.println(c);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql1="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next())
{//遍历结果集
array.add(rs1.getString("StopName"));
}
rs1.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}
rs2.close();
}
con.close();
statement.close();
}catch(SQLException e) {
e.printStackTrace();
}finally {
line.setArray(array);//似乎无意义,在下面已经传回array
for(int i=0;i<array.size();i++) {
System.out.println(array.get(i)+" ");
}
}
return array;
}
/*
* 先不着急,起始站/终点站是换乘站的情况最后考虑
* 首先判断起始站或结束站是否为中转站
*/
public void JudgeChangeStop(Line line) throws ClassNotFoundException {
try {
Connection con=JdbcUtils.getConnection();
Statement statement=con.createStatement();
String sql="select * from SJZchangeline";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("ChangeStopName")))
{
line.setStartline1(rs.getString("ID1"));
line.setStartline2(rs.getString("ID2"));
}
if(line.getEndstopname().equals(rs.getString("ChangeStopName")))
{
line.setEndline1(rs.getString("ID1"));
line.setEndline2(rs.getString("ID2"));
}
}
con.close();
statement.close();
rs.close();
}catch(SQLException e) {
throw new RuntimeException(e);
}finally {
if(line.getStartline1()==null)
{
}
}
}
/*
* 看站点顺序
*/
public int select(Line line) throws ClassNotFoundException {
int i=0;//返回值,1为顺序(数据库中),2为逆序(数据库中)
try {
Connection con = JdbcUtils.getConnection();
Statement statement=con.createStatement();
String sql="select * from SJZfirstline where StopName='"+line.getStartstopname()+"'and Line='"+line.getTemporaryline()+"' or StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"'";
/*
* 找到与起始站名称相同,结束站名称相同,且同时属于同一条线的两个站
*/
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("StopName")))
{
line.setStartstopID(rs.getInt("StopID"));
}
if(line.getEndstopname().equals(rs.getString("StopName"))) {
line.setEndstopID(rs.getInt("StopID"));
}
}
con.close();
statement.close();
rs.close();
}catch(SQLException e) {
throw new RuntimeException(e);
}
System.out.println(line.getStartstopID()+" "+line.getEndstopID());
if(line.getStartstopID()<line.getEndstopID()) {
i=1;
return i;
}
else {
i=2;
return i;
}
}
/*
* 判断是否为同一条线
*/
public int same(Line line) throws ClassNotFoundException {
int checksameline=0;//同一条线
try {
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="select * from SJZfirstline where StopName='"+line.getStartstopname()+"' or StopName='"+line.getEndstopname()+"'";
/*
* 同上,但用不着判断是否属于同一线路(本判断只存在于起始点与结束点是普通点)
*/
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("StopName")))
{
line.setOriginline(rs.getInt("Line"));
}
if(line.getEndstopname().equals(rs.getString("StopName"))) {
line.setFinishline(rs.getInt("Line"));
}
}
}catch(SQLException e) {
throw new RuntimeException(e);
}
if(line.getFinishline()==line.getOriginline()) {//是同一条线
checksameline=1;
return checksameline;
}
else {//不是同一条线
checksameline=2;
return checksameline;
}
}
/*
* 两条不同的、有交点的线路
*/
public ArrayList<String> Connecttwoline(Line line) throws SQLException, ClassNotFoundException{
int c=0;
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
same(line);
String sql="select * from SJZchangeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
//根据交点线路查询中转站
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
line.setMiddlestop(rs.getString("ChangeStopName"));
}
rs.close();
line.setTemporarystartstopname(line.getStartstopname());
//记录当前起始点与结束点
line.setTemporaryendstopname(line.getEndstopname());
//同上
String sql1="select * from SJZfirstline where Line='"+line.getOriginline()+"' and StopName='"+line.getMiddlestop()+"'";
//根据起始线路和中转站名称标出中转站位置
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next()) {
line.setEndstopname(rs1.getString("StopName"));
//起始点不变,结束点设置为中转点
}
rs1.close();
line.setStartstopname(line.getTemporarystartstopname());
line.setTemporaryline(line.getOriginline());
//将起始点和中转点所在线路一致的放入临时站线路
/*
*
*
*
*
*/
array.add("乘坐"+line.getOriginline()+"号线");
c=select(line); //得到起始站与终点站的顺序
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array1=new ArrayList<String>();
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}
rs2.close();
}
array.add("转乘"+line.getFinishline()+"号线");
/*
*
*
*
*
*/
line.setStartstopname(line.getMiddlestop());
//将起始点设为中转点
line.setEndstopname(line.getTemporaryendstopname());
//将结束点回归
line.setTemporaryline(line.getFinishline());
//将中转点和结束点所在线路一致的放入临时站线路
/*
*
*
*
*/
c=select(line); //得到起始站与终点站的顺序
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array1=new ArrayList<String>();
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}
rs2.close();
}
/*
*
*
*
*
*/
//由于array是全局变量,所以继续直接接受数据即可
return array;
}
/*
* 判断有无交点
*/
public int checklink(Line line) throws ClassNotFoundException {
int c=0;
Connection con;
try {
same(line);
con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="select * from SJZchangeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs=statement.executeQuery(sql);
while(rs.next()) {
if(rs.getString("ChangeStopName").equals("")) {
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return c;
}
/*
* 解决两条线之间没有换乘站,只能通过第三条线换乘
* 即A(普通站)->B(中转站)->C(中转站)->D(普通站)
*
*/
public ArrayList<String> Connectthreeline(Line line) throws ClassNotFoundException {
ArrayList<Integer> array1=new ArrayList<Integer>();
int c=0;
int min=0;
try
{
same(line);
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="SELECT * FROM SJZchangeline c1 JOIN SJZchangeline c2 ON c1.ID2 = c2.ID1 WHERE trim(c1.ChangeStopName) != '' AND trim(c2.ChangeStopName) != '' AND c1.ID1 = '"+line.getOriginline()+"' AND c2.ID2 = '"+line.getFinishline()+"'";
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
if(line.getOriginline()==rs.getInt("ID1"))
{
array1.add(rs.getInt("ID2"));
}
}
rs.close();
line.setTemporarystartstopname(line.getStartstopname());
line.setTemporaryendstopname(line.getEndstopname());
/*
*
* 第一次转乘
* A(普通站)->B(中转站)
*
*/
for(int j=0;j<array1.size();j++)
{
ArrayList<String> array3=new ArrayList<String>();
System.out.println("也许这是我想要的起始站"+line.getTemporarystartstopname());
String sql1="select * from SJZchangeline where ID1='"+line.getOriginline()+"' and ID2='"+array1.get(j)+"'";
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next())
{
line.setEndstopname(rs1.getString("ChangeStopName"));
}
rs1.close();
array3.add("乘坐"+line.getOriginline()+"号线");
array3.add(line.getTemporarystartstopname());
String sql4="SELECT * FROM SJZfirstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
ResultSet rs4=statement.executeQuery(sql4);
while(rs4.next()) {
line.setTemporaryline(rs4.getInt("Line"));
}
rs.close();
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
/*
* 第二次换乘
* B(中转站)->C(中转站)
*
*/
line.setStartstopname(line.getEndstopname());
String sql3="select * from SJZchangeline where ID1='"+array1.get(j)+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs3 = statement.executeQuery(sql3);
while(rs3.next())
{
line.setEndstopname(rs3.getString("ChangeStopName"));
}
String sql5="SELECT * FROM SJZfirstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
ResultSet rs5=statement.executeQuery(sql5);
while(rs5.next()) {
line.setTemporaryline(rs5.getInt("Line"));
}
rs5.close();
array3.add("转乘"+line.getTemporaryline()+"号线");
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
/*
*
* 最后一次换乘
* 即C(中转站)->D(普通站)
*/
line.setStartstopname(line.getEndstopname());
line.setEndstopname(line.getTemporaryendstopname());
String sql6="SELECT * FROM SJZfirstline f1 JOIN SJZfirstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
ResultSet rs6=statement.executeQuery(sql6);
while(rs6.next()) {
line.setTemporaryline(rs6.getInt("Line"));
}
rs.close();
array3.add("转乘"+line.getTemporaryline()+"号线");
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from SJZfirstline where StopID>=(select StopID from SJZfirstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from SJZfirstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
if(j==0) {
min=array3.size();
}
/*
* 第一次:5 min=5 array.size=5
* 第二次:3 array.size=3 min=5
* 第三次:7 array.size=7 min=3
*/
//输出array3测试
System.out.println();
System.out.println();
for(int x=0;x<array3.size();x++) {
System.out.print(array3.get(x)+" ");
}
System.out.println();
System.out.println(min);
if(min>=array3.size()) {
min=array3.size();
for(int x=0;x<array3.size();x++) {
System.out.print(array3.get(x)+" ");
}
array=array3;
}
}
statement.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return array;
}
public int CheckAll(Line line) throws SQLException {
int checkall=0;
Connection con=JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="SELECT * FROM SJZfirstline f1 JOIN SJZfirstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
line.setTemporaryline(rs.getInt("Line"));
}
rs.close();
if(line.getTemporaryline()!=0) {
checkall=1;
return checkall;
}
else if(line.getTemporaryline()==0){
String sql1="select * from SJZfirstline where StopName='"+line.getStartstopname()+"' or StopName='"+line.getEndstopname()+"'";
/*
* 同上,但用不着判断是否属于同一线路(本判断只存在于起始点与结束点是普通点)
*/
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next()) {
if(line.getStartstopname().equals(rs1.getString("StopName")))
{
line.setOriginline(rs1.getInt("Line"));
}
if(line.getEndstopname().equals(rs1.getString("StopName"))) {
line.setFinishline(rs1.getInt("Line"));
}
}
rs1.close();
if(line.getOriginline()==0||line.getFinishline()==0) {
checkall=4;
return checkall;
}
System.out.println("这不合理!所以线一为"+line.getOriginline()+",线二为"+line.getFinishline());
String sql2="select * from SJZchangeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next()) {
line.setMiddlestop(rs2.getString("ChangeStopName"));
}
System.out.println(line.getMiddlestop()+"也许不是我想要的");
if(line.getMiddlestop().equals("")) {
checkall=3;
return checkall;
}
else {
checkall=2;
return checkall;
}
}
return checkall;
}
}
5.TextJdbcUtils
package Dao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
/*
* ����JdbcUtils
*/
public class TextJdbcUtils {
public void text() throws SQLException {
Connection con = null;
try {
con = JdbcUtils.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(con);
}
}
6.UserDao
package Dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RunnableScheduledFuture;
import Bean.Line;
public class UserDao {
int demostartid=0,demoendid=0;
ArrayList<String> array=new ArrayList<String>();
/*
* 起始点与终点在一条线上且非换乘站
*/
public List<String> find1(String line){
List<String>list=new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
String sql = "select * from firstline where Line=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, line);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String s=resultSet.getString(2);
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(connection, preparedStatement, resultSet);
}
return list;
}
public ArrayList<String> Connectonlyoneline(Line line) throws ClassNotFoundException
{
ArrayList<String> array1=new ArrayList<String>();
int c=0;
try {
//连接数据库
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
//是否成功连接
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
String sql="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证起始站(同时有可能是中转站)与终点站(同时有可能是中转站)在同一条线路上
ResultSet rs=statement.executeQuery(sql);
while(rs.next()) {
line.setTemporaryline(rs.getInt("Line"));
}
//准备数据库查询:将起始站和终点站的中间站
System.out.println(line.getTemporaryline());
array.add("乘坐"+line.getTemporaryline()+"号线");
c=select(line); //得到起始站与终点站的顺序
System.out.println(c);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql1="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next())
{//遍历结果集
array.add(rs1.getString("StopName"));
}
rs1.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}
rs2.close();
}
con.close();
statement.close();
}catch(SQLException e) {
e.printStackTrace();
}finally {
line.setArray(array);//似乎无意义,在下面已经传回array
for(int i=0;i<array.size();i++) {
System.out.println(array.get(i)+" ");
}
}
return array;
}
/*
* 先不着急,起始站/终点站是换乘站的情况最后考虑
* 首先判断起始站或结束站是否为中转站
*/
public void JudgeChangeStop(Line line) throws ClassNotFoundException {
try {
Connection con=JdbcUtils.getConnection();
Statement statement=con.createStatement();
String sql="select * from changeline";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("ChangeStopName")))
{
line.setStartline1(rs.getString("ID1"));
line.setStartline2(rs.getString("ID2"));
}
if(line.getEndstopname().equals(rs.getString("ChangeStopName")))
{
line.setEndline1(rs.getString("ID1"));
line.setEndline2(rs.getString("ID2"));
}
}
con.close();
statement.close();
rs.close();
}catch(SQLException e) {
throw new RuntimeException(e);
}finally {
if(line.getStartline1()==null)
{
}
}
}
/*
* 看站点顺序
*/
public int select(Line line) throws ClassNotFoundException {
int i=0;//返回值,1为顺序(数据库中),2为逆序(数据库中)
try {
Connection con = JdbcUtils.getConnection();
Statement statement=con.createStatement();
String sql="select * from firstline where StopName='"+line.getStartstopname()+"'and Line='"+line.getTemporaryline()+"' or StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"'";
/*
* 找到与起始站名称相同,结束站名称相同,且同时属于同一条线的两个站
*/
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("StopName")))
{
line.setStartstopID(rs.getInt("StopID"));
}
if(line.getEndstopname().equals(rs.getString("StopName"))) {
line.setEndstopID(rs.getInt("StopID"));
}
}
con.close();
statement.close();
rs.close();
}catch(SQLException e) {
throw new RuntimeException(e);
}
System.out.println(line.getStartstopID()+" "+line.getEndstopID());
if(line.getStartstopID()<line.getEndstopID()) {
i=1;
return i;
}
else {
i=2;
return i;
}
}
/*
* 判断是否为同一条线
*/
public int same(Line line) throws ClassNotFoundException {
int checksameline=0;//同一条线
try {
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="select * from firstline where StopName='"+line.getStartstopname()+"' or StopName='"+line.getEndstopname()+"'";
/*
* 同上,但用不着判断是否属于同一线路(本判断只存在于起始点与结束点是普通点)
*/
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("StopName")))
{
line.setOriginline(rs.getInt("Line"));
}
if(line.getEndstopname().equals(rs.getString("StopName"))) {
line.setFinishline(rs.getInt("Line"));
}
}
}catch(SQLException e) {
throw new RuntimeException(e);
}
if(line.getFinishline()==line.getOriginline()) {//是同一条线
checksameline=1;
return checksameline;
}
else {//不是同一条线
checksameline=2;
return checksameline;
}
}
/*
* 两条不同的、有交点的线路
*/
public ArrayList<String> Connecttwoline(Line line) throws SQLException, ClassNotFoundException{
int c=0;
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
same(line);
String sql="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
//根据交点线路查询中转站
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
line.setMiddlestop(rs.getString("ChangeStopName"));
}
rs.close();
line.setTemporarystartstopname(line.getStartstopname());
//记录当前起始点与结束点
line.setTemporaryendstopname(line.getEndstopname());
//同上
String sql1="select * from firstline where Line='"+line.getOriginline()+"' and StopName='"+line.getMiddlestop()+"'";
//根据起始线路和中转站名称标出中转站位置
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next()) {
line.setEndstopname(rs1.getString("StopName"));
//起始点不变,结束点设置为中转点
}
rs1.close();
line.setStartstopname(line.getTemporarystartstopname());
line.setTemporaryline(line.getOriginline());
//将起始点和中转点所在线路一致的放入临时站线路
/*
*
*
*
*
*/
array.add("乘坐"+line.getOriginline()+"号线");
c=select(line); //得到起始站与终点站的顺序
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array1=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}
rs2.close();
}
array.add("转乘"+line.getFinishline()+"号线");
/*
*
*
*
*
*/
line.setStartstopname(line.getMiddlestop());
//将起始点设为中转点
line.setEndstopname(line.getTemporaryendstopname());
//将结束点回归
line.setTemporaryline(line.getFinishline());
//将中转点和结束点所在线路一致的放入临时站线路
/*
*
*
*
*/
c=select(line); //得到起始站与终点站的顺序
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array1=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}
rs2.close();
}
/*
*
*
*
*
*/
//由于array是全局变量,所以继续直接接受数据即可
return array;
}
/*
* 判断有无交点
*/
public int checklink(Line line) throws ClassNotFoundException {
int c=0;
Connection con;
try {
same(line);
con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs=statement.executeQuery(sql);
while(rs.next()) {
if(rs.getString("ChangeStopName").equals("")) {
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return c;
}
/*
* 解决两条线之间没有换乘站,只能通过第三条线换乘
* 即A(普通站)->B(中转站)->C(中转站)->D(普通站)
*
*/
public ArrayList<String> Connectthreeline(Line line) throws ClassNotFoundException {
ArrayList<Integer> array1=new ArrayList<Integer>();
int c=0;
int min=0;
try
{
same(line);
Connection con = JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="SELECT * FROM changeline c1 JOIN changeline c2 ON c1.ID2 = c2.ID1 WHERE trim(c1.ChangeStopName) != '' AND trim(c2.ChangeStopName) != '' AND c1.ID1 = '"+line.getOriginline()+"' AND c2.ID2 = '"+line.getFinishline()+"'";
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
if(line.getOriginline()==rs.getInt("ID1"))
{
array1.add(rs.getInt("ID2"));
}
}
rs.close();
line.setTemporarystartstopname(line.getStartstopname());
line.setTemporaryendstopname(line.getEndstopname());
/*
*
* 第一次转乘
* A(普通站)->B(中转站)
*
*/
for(int j=0;j<array1.size();j++)
{
ArrayList<String> array3=new ArrayList<String>();
System.out.println("也许这是我想要的起始站"+line.getTemporarystartstopname());
String sql1="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+array1.get(j)+"'";
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next())
{
line.setEndstopname(rs1.getString("ChangeStopName"));
}
rs1.close();
array3.add("乘坐"+line.getOriginline()+"号线");
array3.add(line.getTemporarystartstopname());
String sql4="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
ResultSet rs4=statement.executeQuery(sql4);
while(rs4.next()) {
line.setTemporaryline(rs4.getInt("Line"));
}
rs.close();
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
/*
* 第二次换乘
* B(中转站)->C(中转站)
*
*/
line.setStartstopname(line.getEndstopname());
String sql3="select * from changeline where ID1='"+array1.get(j)+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs3 = statement.executeQuery(sql3);
while(rs3.next())
{
line.setEndstopname(rs3.getString("ChangeStopName"));
}
String sql5="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
ResultSet rs5=statement.executeQuery(sql5);
while(rs5.next()) {
line.setTemporaryline(rs5.getInt("Line"));
}
rs5.close();
array3.add("转乘"+line.getTemporaryline()+"号线");
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
/*
*
* 最后一次换乘
* 即C(中转站)->D(普通站)
*/
line.setStartstopname(line.getEndstopname());
line.setEndstopname(line.getTemporaryendstopname());
String sql6="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
ResultSet rs6=statement.executeQuery(sql6);
while(rs6.next()) {
line.setTemporaryline(rs6.getInt("Line"));
}
rs.close();
array3.add("转乘"+line.getTemporaryline()+"号线");
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
if(j==0) {
min=array3.size();
}
/*
* 第一次:5 min=5 array.size=5
* 第二次:3 array.size=3 min=5
* 第三次:7 array.size=7 min=3
*/
//输出array3测试
System.out.println();
System.out.println();
for(int x=0;x<array3.size();x++) {
System.out.print(array3.get(x)+" ");
}
System.out.println();
System.out.println(min);
if(min>=array3.size()) {
min=array3.size();
for(int x=0;x<array3.size();x++) {
System.out.print(array3.get(x)+" ");
}
array=array3;
}
}
statement.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return array;
}
public int CheckAll(Line line) throws SQLException {
int checkall=0;
Connection con=JdbcUtils.getConnection();
Statement statement = con.createStatement();
String sql="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
line.setTemporaryline(rs.getInt("Line"));
}
rs.close();
if(line.getTemporaryline()!=0) {
checkall=1;
return checkall;
}
else if(line.getTemporaryline()==0){
String sql1="select * from firstline where StopName='"+line.getStartstopname()+"' or StopName='"+line.getEndstopname()+"'";
/*
* 同上,但用不着判断是否属于同一线路(本判断只存在于起始点与结束点是普通点)
*/
ResultSet rs1 = statement.executeQuery(sql1);
while(rs1.next()) {
if(line.getStartstopname().equals(rs1.getString("StopName")))
{
line.setOriginline(rs1.getInt("Line"));
}
if(line.getEndstopname().equals(rs1.getString("StopName"))) {
line.setFinishline(rs1.getInt("Line"));
}
}
rs1.close();
if(line.getOriginline()==0||line.getFinishline()==0) {
checkall=4;
return checkall;
}
System.out.println("这不合理!所以线一为"+line.getOriginline()+",线二为"+line.getFinishline());
String sql2="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs2 = statement.executeQuery(sql2);
while(rs2.next()) {
line.setMiddlestop(rs2.getString("ChangeStopName"));
}
System.out.println(line.getMiddlestop()+"也许不是我想要的");
if(line.getMiddlestop().equals("")) {
checkall=3;
return checkall;
}
else {
checkall=2;
return checkall;
}
}
return checkall;
}
}
7.UserService
package service;
import Dao.UserDao;
public class UserService {
private UserDao userdao=new UserDao();
}
8.servlet
package Servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Bean.Line;
import Dao.NJDao;
import Dao.SJZDao;
import Dao.UserDao;
import service.UserService;
/**
* Servlet implementation class servlet
*/
@WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8"); //1
response.setContentType("text/html;charset=utf-8"); //2
response.setCharacterEncoding("utf-8"); //3
UserDao dao = new UserDao();
SJZDao sjzDao=new SJZDao();
NJDao njDao=new NJDao();
String method = request.getParameter("method");
if (method.equals("find1")) {
List<String> list=new ArrayList<>();
String line=request.getParameter("line");
String city=request.getParameter("city");
if(city.equals("北京")) {
list = dao.find1(line);
request.setAttribute("stop", list);
request.getRequestDispatcher("result.jsp").forward(request, response);
}
else if(city.equals("石家庄")){
list=sjzDao.find1(line);
request.setAttribute("stop", list);
request.getRequestDispatcher("result.jsp").forward(request, response);
}
else if(city.equals("南京")){
}
} else {
Line line = new Line();
ArrayList<String> array = new ArrayList<String>();
String startstopname = request.getParameter("startstopname");
String endstopname = request.getParameter("endstopname");
String city=request.getParameter("city");
line.setStartstopname(startstopname);
line.setEndstopname(endstopname);
try {
int x = dao.CheckAll(line);
System.out.println("实际上传过来的值是" + x);
if(city.equals("北京")) {
if (x == 1) {
array = dao.Connectonlyoneline(line);
} else if (x == 2) {
array = dao.Connecttwoline(line);
} else if (x == 3) {
array = dao.Connectthreeline(line);
} else {
request.setAttribute("message", "请输入正确的站名!");
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
request.setAttribute("stop", array);
}
else if(city.equals("石家庄")){
if (x == 1) {
array = dao.Connectonlyoneline(line);
} else if (x == 2) {
array = dao.Connecttwoline(line);
} else if (x == 3) {
array = dao.Connectthreeline(line);
} else {
request.setAttribute("message", "请输入正确的站名!");
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
//array=dao.Connectthreeline(line);
request.setAttribute("stop", array);
}
else if(city.equals("南京")){
if (x == 1) {
array = dao.Connectonlyoneline(line);
} else if (x == 2) {
array = dao.Connecttwoline(line);
} else if (x == 3) {
array = dao.Connectthreeline(line);
} else {
request.setAttribute("message", "请输入正确的站名!");
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
request.setAttribute("stop", array);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
request.getRequestDispatcher("result.jsp").forward(request, response);
}
}
}
}
9.bj.html
<html>
<head>
<meta charset="UTF-8">
<!--重要meta, 必须!-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0,shrink-to-fit=no"/>
<title>北京地铁</title>
</head>
<body>
<div id="mysubway"></div>
<script src="https://webapi.amap.com/subway?v=1.0&key=your_key&callback=cbk"></script>
<script type="text/javascript">
//开启easy模式, 直接完成地铁图基本功能, 无需自己写交互
window.cbk = function(){
var mysubway = subway("mysubway", {//石家庄
easy: 1
});
};
</script>
</body>
</html>
10.db1.jsp
<%@ page language="java" contentType="text[表情]ml; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="Dao.JdbcUtils" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>查询页面</title>
</head>
<body>
<table border="1"style="text-align:center;">
<tr>
<td align="center" width=8%>路线1</td>
<td align="center" width=10%>路线2</td>
<td align="center" width=10%>换乘点名</td>
</tr>
<%
String StopName = request.getParameter("StopName");
System.out.println(StopName);
//接收客户端传递过来的参数
try {
Connection conn = JdbcUtils.getConnection(); //连接状态
PreparedStatement stmt = null;
ResultSet rs = null;
StopName="%"+StopName+"%";
String sql = "select * from changeline where changeline.ChangeStopName like ?"; //查询语句
stmt = conn.prepareStatement(sql);
stmt.setString(1, StopName);
rs = stmt.executeQuery();
while (rs.next()) {
String str1 = rs.getString("ID1");
String str2 = rs.getString("ID2");
String str3 = rs.getString("ChangeStopName");
%>
<tr>
<td align="center"><%=rs.getObject(1) %></td>
<td align="center"><%=rs.getObject(2) %></td>
<td align="center"><%=rs.getObject(3) %></td>
</tr>
</table>
<%
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
}
%>
</body>
</html>
11.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<head>
<body background="images/td.jpg">
<audio src="images/若把你.mp3" controls>
</audio></body>
<title style="color: red">地铁查询系统</title>
<link rel="stylesheet" href="./style.css">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="./script.js"></script>
</head>
<body>
<div id="datetime">
<script>
setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
</script>
</div>
<div id="tabtitle" >
<div style="width: 100%; font-family: 宋体; text-align: center; font-size: 20pt;">欢迎来到地铁查询系统</div>
</div>
<div id="tabDiv" >
<div id="tabsHead">
<a id="tabs1" class="curtab" href="javascript:showTab('tabs1','tabContent1')">
路线查询
</a>
<a id="tabs2" class="tabs" href="javascript:showTab('tabs2','tabContent2')">
站点查询
</a>
<a id="tabs3" class="tabs" href="javascript:showTab('tabs3','tabContent3')">
<a href="NewFile1.html" >全国地图查询</a>
</a>
</div>
<div id="tabContent1" >
<table id="table1" >
<tr>
<td colspan="3" rowspan="3">
<table id="table2" >
<div align="center">
<h1 style="color:red" align="center">地铁</h1>
<form action="${pageContext.request.contextPath}/servlet" method="post" onsubmit="return check();">
请输入线路:<input type="text" name="line"><br>
请选择城市:<input type="radio" name="city" value="北京">北京
<input type="radio" name="city" value="南京">南京
<input type="radio" name="city" value="石家庄">石家庄<br>
<input name="method" value="find1" type="hidden"><br>
<input type="submit" value="提交" onclick="check();"/>
</div>
</form>
<div align="center">
<img alt="北京" src="images/pic.jpg" style="width: 800px" style="heigh: 500px" />
</div>
</table>
</td>
</tr>
</table>
</div>
<div id="tabContent2" style="display: none">
<% String msg="";
String message=(String)request.getAttribute("message");
if(message!=null){
msg=message;
}
%>
<h1 style="color:red" align="center">北京地铁</h1>
<form action="${pageContext.request.contextPath}/servlet" method="post" onsubmit="return check();">
<div class=a>
<%=msg %>
</div>
<div align="center">
起始站:<input type="text" name="startstopname" id="startstopname"/>
<br/>
终点站:<input type="text" name="endstopname" id="endstopname"/><br>
请选择城市:<input type="radio" name="city" value="北京">北京
<input type="radio" name="city" value="南京">南京
<input type="radio" name="city" value="石家庄">石家庄
</div>
<div align="center">
<br>
<input type="submit" value="提交" onclick="check();"/>
</br>
</div>
<input type="hidden" name="method" value="find2">
</form>
<div align="center">
<img alt="北京" src="images/pic.jpg" style="width: 600px" style="heigh: 500px"/>
</div>
</div>
</div>
<div id="tabContent3" style="display: none">
</div>
<div style="text-align: center; width: 100%; font-size: 12px; color: #333;">©版权所有:石铁大软工赵金荣,贾承霖</div>
</body>
</html>
12.map.css
@charset "UTF-8";
#container {
width: 100%;
height: 100%;
}
html,body{
width: 100%;
height: 100%;
}
.amap-icon img {
width: 25px;
height: 34px;
}
13.Newdedao.css
@charset "UTF-8";
html, body {
margin: 0;
height: 100%;
width: 100%;
position: absolute;
}
.button-group {
position: absolute;
bottom: 20px;
right: 20px;
font-size: 12px;
padding: 10px;
}
.button-group .button {
height: 28px;
line-height: 28px;
background-color: #0D9BF2;
color: #FFF;
border: 0;
outline: none;
padding-left: 5px;
padding-right: 5px;
border-radius: 3px;
margin-bottom: 4px;
cursor: pointer;
}
.button-group .inputtext {
height: 26px;
line-height: 26px;
border: 1px;
outline: none;
padding-left: 5px;
padding-right: 5px;
border-radius: 3px;
margin-bottom: 4px;
cursor: pointer;
}
#tip {
background-color: #fff;
padding-left: 10px;
padding-right: 10px;
position: absolute;
font-size: 12px;
right: 10px;
top: 20px;
border-radius: 3px;
border: 1px solid #ccc;
line-height: 30px;
}
.amap-info-content {
font-size: 12px;
}
#myPageTop {
position: absolute;
top: 5px;
right: 10px;
background: #fff none repeat scroll 0 0;
border: 1px solid #ccc;
margin: 10px auto;
padding:6px;
font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";
font-size: 14px;
border-radius:5px;
}
#myPageTop label {
margin: 0 20px 0 0;
color: #666666;
font-weight: normal;
}
#myPageTop input {
width: 170px;
}
#myPageTop .column2{
padding-left: 25px;
}
14.NewFile1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">//密钥
window._AMapSecurityConfig = {
securityJsCode:'9be0d23cef93d90d8e3217f49715cd84',
}
</script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=06c9674ca7423e2f981ecedec8312da2">
</script>
<script type="text/javascript" src="search.js"></script>
<link href="map.css" rel="stylesheet" type="text/css"/>
<link href="Newdedao.css" rel="stylesheet" type="text/css"/>
<title>铁路线路</title>
</head>
<body>
<div id="container"></div>
<div id="tip" class="info">地图正在加载</div>
<script>
var map = new AMap.Map('container', {
resizeEnable: true, //是否监控地图容器尺寸变化
zoom: 5, //初始化地图层级
zooms:[2,20],
center: [116.397428, 39.90923], //初始化地图中心点
});
var marker = new AMap.Marker({
position:[116.397428, 39.90923],
image: 'images\\marker.png',
iconSize:new AMap.Size(25,34)
});
marker.on("click", function () {
window.location.href = 'bj.html';
});
marker.setMap(map);
var marker2 = new AMap.Marker({
position:[114.53952, 38.03647],
image: 'images\\marker.png',
iconSize:new AMap.Size(25,34)
});
marker2.on("click", function () {
window.location.href = 'sjz.html';
});
marker2.setMap(map);
map.on('complete', function() {
document.getElementById('tip').innerHTML = "地图图块加载完毕!当前地图中心点为:" + map.getCenter();
});
</script>
</body>
</html>
15.result.jsp
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<body background="images/td.jpg">
</body>
<meta charset="UTF-8">
<title>查询页面</title>
</head>
<body>
<div id="datetime">
<script>
setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
</script>
<h5>当前时间</h5>
</div>
<div id="tabtitle" >
<div style="width: 100%; font-family: 宋体; text-align: center; font-size: 20pt;">查询结果</div>
</div>
<div id="tabDiv" >
<div id="tabsHead">
</div>
<div id="tabContent1" >
<table id="table1" >
<tr>
<%ArrayList array=(ArrayList)request.getAttribute("stop"); %>
<%if(array==null){
request.setAttribute("message", "请输入数据!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{%>
<a style="color:blue" href='db1.jsp?StopName=<%=(array.get(9))%>' onclick="del()"> <%=array.get(9) %></a>
<%for(int i=0;i<array.size();i++){%>
<th>
<%=array.get(i) %>
</th>
<%}
}%>
<div align="center">
<form action="index.jsp">
<button type="submit">回到首页</button>
</form>
</div>
<td colspan="3" rowspan="3">
<table id="table2" >
<div align="center">
<h1 style="color:blue" align="center">北京地铁</h1>
<div align="center">
<img alt="北京" src="images/pic.jpg" style="width: 500px" style="heigh: 500px" />
</div>
</table>
</td>
</tr>
</table>
</div>
<div style="text-align: center; width: 100%; font-size: 12px; color: #333;">©版权所有:石铁大软工赵金荣,贾承霖</div>
</html>
16.script.js
//显示tab(tabHeadId:tab头中当前的超链接;tabContentId要显示的层ID)
function showTab(tabHeadId,tabContentId)
{
//tab层
var tabDiv = document.getElementById("tabDiv");
//将tab层中所有的内容层设为不可见
//遍历tab层下的所有子节点
var taContents = tabDiv.childNodes;
for(i=0; i<taContents.length; i++)
{
//将所有内容层都设为不可见
if(taContents[i].id!=null && taContents[i].id != 'tabsHead')
{
taContents[i].style.display = 'none';
}
}
//将要显示的层设为可见
document.getElementById(tabContentId).style.display = 'block';
//遍历tab头中所有的超链接
var tabHeads = document.getElementById('tabsHead').getElementsByTagName('a');
for(i=0; i<tabHeads.length; i++)
{
//将超链接的样式设为未选的tab头样式
tabHeads[i].className='tabs';
}
//将当前超链接的样式设为已选tab头样式
document.getElementById(tabHeadId).className='curtab';
document.getElementById(tabHeadId).blur();
}
17.sjz.html
<html>
<head>
<meta charset="UTF-8">
<!--重要meta, 必须!-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0,shrink-to-fit=no"/>
<title>石家庄地铁</title>
</head>
<body>
<div id="mysubway"></div>
<script src="https://webapi.amap.com/subway?v=1.0&key=your_key&callback=cbk"></script>
<script type="text/javascript">
//开启easy模式, 直接完成地铁图基本功能, 无需自己写交互
window.cbk = function(){
var mysubway = subway("mysubway", {//石家庄
adcode:1301,
easy: 1
});
};
</script>
</body>
</html>
18.style.css
@charset "UTF-8";
table {
border-spacing: 0;
}
div a:link {
color:threedshadow;
text-decoration:none;
}
/* visited,定义鼠标链接访问过的样式 */
div a:visited{
color:threedshadow;
text-decoration:none;
}
/* hover,定义鼠标悬浮在链接上时的样式 */
div a:hover {
color:threedhighlight;
text-decoration:none;
}
/* active,定义鼠标点击链接时的样式 */
div a:active {
color:threedface;
text-decoration:none;
}
p {
font-size: 12pt;
text-indent: 2em;
}
ul{
list-style: none;
}
li {
border-bottom-style: solid;
border-bottom-color: #EEE;
border-bottom-width: thin;
height: 25px;
font-family: "宋体";
font-size: 12pt;
}
#tabtitle{
width: 100%;
font-family: 微软雅黑;
text-align: center;
font-size: 20pt;
}
#tabDiv {
width: 1000px;
margin: 1em auto;
padding-bottom: 10px;
border: #b2c9d3 1px solid;
background: menu;
}
#tabsHead {
padding-left: 0px;
height: 26px;
font-size: 1em;
margin: 1px 0px 0px;
line-height: 26px;
}
#tabContent1{
display: block;
}
#table1{
border-width: 0;
margin: 0;
width: 100%
}
#table2{
border:0px;
width: 700px;
height: 380px;
}
#table2 td{
border: 1px solid grey;
width: 50%;
height: 15px;
}
#table3{
border:0px;
width: 700px;
height: 380px;
}
#table3 td{
border: 1px solid grey;
width: 50%;
height: 15px;
}
#table4{
border:0px;
width: 700px;
height: 380px;
}
#table4 td{
border: 1px solid grey;
width: 50%;
height: 15px;
}
#tabContent2{
}
#tabContent3{
}
.curtab {
padding-top: 0px;
padding-right: 10px;
padding-bottom: 0px;
padding-left: 10px;
border-right: #b2c9d3 1px solid;
font-weight: bold;
float: left;
cursor: pointer;
background: #ffffff;
}
/*未选tab头(超链接)的样式*/
.tabs {
border-right: #c1d8e0 1px solid;
padding: 0px 10px;
font-weight: normal;
float: left;
cursor: pointer;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现