import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Thesql { public Connection connect; public Thesql()throws Exception { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/ditie"; String username="root"; String password="2223640185"; connect = DriverManager.getConnection(url,username,password); } public void finalize() throws Exception { connect.close(); } //线路号查询,String name = 线路名称 public Pd chaxunxianlu(String name) throws SQLException { String sql="select * from xinxi where mingcheng='"+name+"'"; Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery(sql); Pd pdd = new Pd(); while(rs.next()) { pdd.mingcheng=rs.getString(2); String zhan=""; int i=3; while(zhan!=null) { zhan=rs.getString(i); pdd.zhan[i-3]=zhan; i++; } } stmt.close(); return pdd; } //站点查询,String name = 站点名称 public Pd[] chaxunxianlu2(String zhanname) throws Exception { String[] strings=getxianname(zhanname); Pd[] pdd = new Pd[strings.length]; for(int j=0;j<strings.length;j++) { pdd[j]=chaxunxianlu(strings[j]); } return pdd; } //得到线路名称,String zhanname = 站点名称 ,返回值为字符串数组(可能多条线路),返回内容为线路名称 public String[] getxianname(String zhanname) throws SQLException { String[] strings = new String[5]; Statement stmt = connect.createStatement(); int num=0; for(int i=1;i<36;i++) { String sql="select * from xinxi where a"+i+" ='"+zhanname+"'"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { String n=rs.getString(2); strings[num]=n; } if(strings[num]!=null) num++; } String[] strings2 = new String[num]; for(int ii=0;ii<num;ii++) strings2[ii]=strings[ii]; stmt.close(); return strings2; } //起点,线,站数,换站点,线,站数,换站点,线,站数,换站点,线,站数,终点 public String[] chaxunQiAndZhong(String qidian,String zhongdian) throws Exception { if(isxiangtong(qidian, zhongdian)!=null) { String[] jieguo = new String[1]; jieguo[0]=isxiangtong(qidian, zhongdian); String[] jieguo2={qidian,jieguo[0],getchangdu(jieguo[0], qidian, zhongdian)+"",zhongdian}; return jieguo2; } else { String[] jieguo =getbestjieguo(chaxunduoxian(qidian, zhongdian),qidian,zhongdian); String[] jieguo2=new String[100]; int n =0; jieguo2[0]=qidian; jieguo2[1]=jieguo[0]; jieguo2[2]=getchangdu(jieguo[0],qidian,gethuanzhanname(jieguo[0],jieguo[1]))+""; jieguo2[3]=gethuanzhanname(jieguo[0],jieguo[1]); for(int i=2;i<jieguo.length&&jieguo[i]!=null;i++) { jieguo2[3*i-2]=jieguo[i-1]; jieguo2[3*i-1]=getchangdu(jieguo[i-1],gethuanzhanname(jieguo[i-2],jieguo[i-1]),gethuanzhanname(jieguo[i-1],jieguo[i]))+""; jieguo2[3*i]=gethuanzhanname(jieguo[i-1],jieguo[i]); n=i; } jieguo2[3*n+1]=jieguo[n]; jieguo2[3*n+2]=getchangdu(jieguo[0],zhongdian,gethuanzhanname(jieguo[0],jieguo[1]))+""; jieguo2[3*n+3]=zhongdian; return jieguo2; } } public String[] getbestjieguo(String[][] jieguos,String qidian,String zhongdian) throws Exception { String best[] = new String[20]; int min=1000; for(int i=0;i<jieguos.length&&jieguos[i][0]!=null;i++) { int num = getchangdu(jieguos[i], qidian, zhongdian); if(num<min) { best=jieguos[i]; min=num; } } return best; } public int getchangdu(String[] strings,String qidian,String zhongdian) throws Exception { int sum=0,n=1; sum=getchangdu(strings[0],qidian,gethuanzhanname(strings[0],strings[1])); for(int i=2;i<strings.length&&strings[i]!=null;i++) { sum=sum+getchangdu(strings[i-1],gethuanzhanname(strings[i-2],strings[i-1]), gethuanzhanname(strings[i-1],strings[i])); n=i; } //if(n!=2) sum=sum+getchangdu(strings[n],zhongdian,gethuanzhanname(strings[n-1],strings[n])); return sum; } public int getchangdu(String xianname,String A,String B) throws Exception { Pd pd = chaxunxianlu(xianname); int a=0,b=0; for (int i=0;i<pd.zhan.length&&pd.zhan[i]!=null;i++) { if(A.equals(pd.zhan[i])) a=i; if(B.equals(pd.zhan[i])) b=i; } if(a>b) return (a-b); else return (b-a); } public String[][] chaxunduoxian(String A,String B) throws Exception { String[] jieguo = new String[20]; String[][] jieguos = new String[1000][20]; int n=0; String[] strings = getxianname(A); for (int i=0;i<strings.length;i++) { String[] huanzhans = gethuanzhanname(strings[i]); if(isxiangtong(huanzhans,B)!=null) { String string =isxiangtong(huanzhans,B); jieguo[0]=strings[i]; jieguo[1]=string; jieguos[n]=jieguo; n++; } for(int j=0;j<huanzhans.length;j++) { String[] strings2 = getxianname(huanzhans[j]); for (int k=0; k<strings2.length;k++) { String[] huanzhans2 = gethuanzhanname(strings2[k]); if(isxiangtong(huanzhans2,B)!=null) { String string =isxiangtong(huanzhans2,B); jieguo[0]=strings[i]; jieguo[1]=strings2[k]; jieguo[2]=string; jieguos[n]=jieguo; n++; } } } if(jieguos[0][0] != null) { return jieguos; }else { for(int j=0;j<huanzhans.length;j++) { String[] strings2 = getxianname(huanzhans[j]); for (int k=0; k<strings2.length;k++) { String[] huanzhans2 = gethuanzhanname(strings2[k]); for (int l = 0; l < huanzhans2.length; l++) { String[] strings3 = getxianname(huanzhans2[l]); for (int m = 0; m < strings3.length; m++) { String[] huanzhans3 = gethuanzhanname(strings3[m]); if(isxiangtong(huanzhans3,B)!=null) { String string =isxiangtong(huanzhans3,B); jieguo[0]=strings[i]; jieguo[1]=strings2[k]; jieguo[2]=strings3[m]; jieguo[3]=string; jieguos[n]=jieguo; n++; } } } } } } } return jieguos; } public String isxiangtong(String A,String B) throws Exception { String[] strings = getxianname(A); String[] strings2 = getxianname(B); for(int i=0;i<strings.length;i++) { for(int j=0;j<strings2.length;j++) { if(strings[i].equals(strings2[j])) return strings[i]; } } return null; } public String isxiangtong(String[] A,String B) throws Exception { int n=A.length; for(int k=0;k<n;k++) { String[] strings = getxianname(A[k]); String[] strings2 = getxianname(B); for(int i=0;i<strings.length;i++) { for(int j=0;j<strings2.length;j++) { if(strings[i].equals(strings2[j])) return strings[i]; } } } return null; } public String[] gethuanzhanname(String xianname) throws SQLException { String sql="select * from huanzhan where xian1='"+xianname+"' or xian2='"+xianname+"'"; Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery(sql); String[] strings = new String[30]; int j = 0; while(rs.next()) { strings[j]=rs.getString(2); j++; } String[] strings2 = new String[j]; for(int ii=0;ii<j;ii++) strings2[ii]=strings[ii]; stmt.close(); return strings2; } public String gethuanzhanname(String A,String B) throws Exception { String string=new String(); String sql="select * from huanzhan where xian1='"+A+"' and xian2='"+B+"'"; Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { string=rs.getString(2); } if(!string.equals("")) { return string; } else { String sql2="select * from huanzhan where xian1='"+B+"' and xian2='"+A+"'"; Statement stmt2 = connect.createStatement(); ResultSet rs2 = stmt2.executeQuery(sql2); while(rs2.next()) { string=rs2.getString(2); } if(!string.equals("")) { return string; } } return null; } }
地铁查询的相关操作代码
标签:总结,java,String,22,2023.3,Pd,sql,import,public From: https://www.cnblogs.com/wllovelmbforever/p/17245574.html