📄 comprehenquery.java
字号:
return false;
}
//The method used to turn the designate day into the weekday
//Like turn "2004,12,25" into 6(Saturday)
private String timeToWeek(String year,String month,String day)
{
int sum=0;
int y = Integer.parseInt(year);
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int[] dayOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//Caculate the first the day of the designate year is "Xing Qi Ji"
int firstDayOfYear = firstDay(y);
for(int i = 1;i < m;i++)
{
sum=sum+dayOfMonth[i];
}
sum = sum+(d-1)+firstDayOfYear;
//If month is over February and the designate year is leap year,
//the total days should be add one
if( (m >= 2) && ((y%4 == 0 && y%100 != 0) || (y%400 == 0)))
sum ++;
int week = 0;
//The weekday for the designate day is:
int x = sum % 7;
switch(x)
{
case 1:
week = 1;
break;
case 2:
week = 2;
break;
case 3:
week = 3;
break;
case 4:
week = 4;
break;
case 5:
week = 5;
break;
case 6:
week = 6;
break;
case 0:
week = 7;
break;
}
return String.valueOf(week);
}
//The method used to caculate the first the day of the designate year is "Xing Qi Ji"
private int firstDay(int year)
{
int a,b;
if(year <= 2000)
{
a=2000-year;
b=6-(a+a/4-a/100+a/400)%7;
return b;
}
else
{
a=year-2000;
b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6;
return b%7;
}
}
//The query method for the one way query mode
public void executeSingleQuery()
{
String sqlString = formSQLString(start,arrive);
ResultSet rs = sqlBean.executeQuery(sqlString);
if (rs != null)
{
//Form result string
String result = " " +
"综合查询";
//Form the specific result string according the message you give
result += formResult(rs,leaveYear,leaveMonth,leaveDay,leaveWeek,start,arrive);
//Display result in a dialog
showResult(result);
}
else
JOptionPane.showMessageDialog(null,"没有连接上数据库!",
"错误信息",JOptionPane.ERROR_MESSAGE);
}
//The query method for the out and home way query mode
public void executeDoubleQuery()
{
//The out and home way has to query the database two times to find the
//flight infomation of from start city to destination and from destination to start city
String sqlString1 = formSQLString(start,arrive);
ResultSet rs1 = sqlBean.executeQuery(sqlString1);
String sqlString2 = formSQLString(arrive,start);
ResultSet rs2 = sqlBean.executeQuery(sqlString2);
if ( (rs1 != null) || (rs2 != null))
{
String result = " " +
"综合查询 ";
//Form the result string for the out and home way query mode
result += formDoubleResult(rs1,rs2);
showResult(result);
}
else
JOptionPane.showMessageDialog(null,"没有连接上数据库!",
"错误信息",JOptionPane.ERROR_MESSAGE);
}
//The query method for the mutiple way query mode
public void executeMutipleQuery()
{
//The out and home way has to query the database two times to find the
//flight infomation of from start city to midway destination
//and from midway destination to final destination
String sqlString1 = formSQLString(start,firstArrive);
ResultSet rs1 = sqlBean.executeQuery(sqlString1);
String sqlString2 = formSQLString(firstArrive,arrive);
ResultSet rs2 = sqlBean.executeQuery(sqlString2);
if ((rs1 != null) || (rs2 != null))
{
String result = " " +
"综合查询 ";
//Form the result string for the mutiple way query mode
result += formMutipleResult(rs1,rs2);
showResult(result);
}
else
JOptionPane.showMessageDialog(null,"没有连接上数据库!",
"错误信息",JOptionPane.ERROR_MESSAGE);
}
public String formSQLString(String begin,String end)
{
String sqlString = "SELECT DISTINCT * FROM " + "flight " +
"WHERE start=" + "\'" + begin + "\'" + " AND " +
"destination=" + "\'" + end + "\'";
if (!airFirm.equals("所有"))
sqlString += " AND " + "airFirm=" + "\'" + airFirm + "\'";
return sqlString;
}
//Get the result string from the result set
public String formResult(ResultSet rs,String year,String month,String day,
String week,String begin,String end)
{
String result = "";
//Change the English weekday into the chinese weekday
String weekDay = dayOfWeek(week);
result += "\n" + "航程:" + year + "年" + month + "月" + day + "日" +
"(星期" + weekDay + ") " + begin + "----" + end + "\n";
result += "航班号 航空公司 起飞地点 抵达地点 起飞时间 抵达时间 " +
"儿童票价 成人票价 折扣 班期 " + "\n";
//Used to determine whether there are no records found
int originLength = result.length();
String time1,time2;
String childFare,adultFare,discount1,discount2,seat;
try
{
String tempResult = "";
String tempWeek;
while(rs.next())
{
tempResult = rs.getString("flight") + rs.getString("airfirm") + rs.getString("start") +
rs.getString("destination");
//When you get the time from the resultset,it is like "1200".
//So we should change it into the form "12:00".
time1 = rs.getString("leaveTime");
time2 = rs.getString("arriveTime");
//getTime(String time) is used to change the time form into standard one
time1 = getTime(time1);
time2 = getTime(time2);
tempResult += time1 + " " + time2 + " ";
//Make sure that the following items have the exactly bits,
//so that they can be display in a neat format
childFare = String.valueOf(rs.getFloat("childFare"));
adultFare = String.valueOf(rs.getFloat("adultFare"));
discount1 = String.valueOf(rs.getFloat("discount1"));
discount2 = String.valueOf(rs.getFloat("discount2"));
seat = String.valueOf(rs.getInt("seat"));
//Make every item in a neat format
while(childFare.length() != 11)
childFare += " ";
while(adultFare.length() != 11)
adultFare += " ";
while(discount1.length() != 8)
discount1 += " ";
tempWeek = rs.getString("week");
tempResult += childFare + adultFare + discount1 +
tempWeek;
tempResult += "\n";
//If the flight schedule contains the day that the user designate,
//the record is the just one we find!So put the tempResult to the result!
//If not,it is not the result!So can't put the tempResult to the result!
if (tempWeek.indexOf(week) != -1)
result += tempResult;
}
}
catch(SQLException e)
{
e.printStackTrace();
}
//Means there are no records found
//So give user message that couldn't find correlate infomation
if (result.length() == originLength)
{
result += " " +
"对不起,找不到你想要的航班信息!" + "\n";
}
return result;
}
//The method used to change the time form
private String getTime(String time)
{
String time1,time2;
time1 = time.substring(0,2);
time2 = time.substring(2,4);
time1 = time1.concat(":");
time1 = time1.concat(time2);
return time1;
}
//The method used to change the English weekday into the chinese weekday
private String dayOfWeek(String weekNum)
{
String week = "";
int num = Integer.parseInt(weekNum);
switch(num)
{
case 1:
week = "一";
break;
case 2:
week = "二";
break;
case 3:
week = "三";
break;
case 4:
week = "四";
break;
case 5:
week = "五";
break;
case 6:
week = "六";
break;
case 7:
week = "日";
break;
}
return week;
}
//Form the result string for the out and home way query mode
public String formDoubleResult(ResultSet rs1,ResultSet rs2)
{
String result1 = formResult(rs1,leaveYear,leaveMonth,leaveDay,leaveWeek,start,arrive);
String result2 = formResult(rs2,backYear,backMonth,backDay,backWeek,arrive,start);
String result = result1 + result2;
return result;
}
//Form the result string for the mutiple way query mode
public String formMutipleResult(ResultSet rs1,ResultSet rs2)
{
String result1 = formResult(rs1,leaveYear,leaveMonth,leaveDay,leaveWeek,start,firstArrive);
String result2 = formResult(rs2,leaveYear2,leaveMonth2,leaveDay2,leaveWeek2,firstArrive,arrive);
String result = result1 + result2;
return result;
}
//Show the result in a dialog
public void showResult(String result)
{
JOptionPane.showMessageDialog(null,result,"查询结果",JOptionPane.PLAIN_MESSAGE);
}
}///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -