📄 airfirmquery.java
字号:
//import flight.assist.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class AirFirmQuery extends JPanel implements ActionListener
{
//用于连接到数据库和执行的SQL操作
static SqlBean sqlBean = new SqlBean();
private static DefaultComboBoxModel model = new DefaultComboBoxModel();
private static JComboBox jcb = new JComboBox(model);
//将信息存入下拉列表框中
private String airfirm;
private JButton jbQuery = new JButton("查询");
public AirFirmQuery()
{
JLabel jl = new JLabel(" 航空公司查询");
jl.setFont(new Font("Times",Font.BOLD,23));
JPanel jpTop = new JPanel();
jpTop.add(jl);
JLabel jl2 = new JLabel(" 请输入航班名称或者,本查询将");
jl2.setHorizontalAlignment(JLabel.CENTER);
jl2.setFont(new Font("Times",Font.PLAIN,12));
JLabel jl3 = new JLabel("提供你该航空公司所有的航班情况:");
jl3.setHorizontalAlignment(JLabel.CENTER);
jl3.setFont(new Font("Times",Font.PLAIN,12));
JPanel jpLabel = new JPanel(new BorderLayout());
jpLabel.add(jl2,BorderLayout.NORTH);
jpLabel.add(jl3,BorderLayout.CENTER);
JPanel jpBox = new JPanel();
jpBox.add(jcb);
JPanel jpButton = new JPanel();
jpButton.add(jbQuery);
JPanel jp = new JPanel();
jp.add(jpBox);
jp.add(jpButton);
JPanel jpCenter = new JPanel();
jpCenter.setLayout(new BorderLayout());
jpCenter.add(jpLabel,BorderLayout.NORTH);
jpCenter.add(jp,BorderLayout.CENTER);
JPanel jpQuery = new JPanel();
jpQuery.setLayout(new BorderLayout());
jpQuery.add(jpTop,BorderLayout.NORTH);
jpQuery.add(jpCenter,BorderLayout.CENTER);
this.setLayout(new BorderLayout());
this.add(new JLabel(" "),BorderLayout.NORTH);
this.add(jpQuery,BorderLayout.CENTER);
//为查询按钮设置监听器
jbQuery.addActionListener(this);
}
public static void updateAirFirmComboBox(String newPlace,int insertOrDelete)
{
if (insertOrDelete == 1)
{
if (model.getIndexOf(newPlace) == -1)
jcb.addItem(newPlace);
}
else if (insertOrDelete == 2)
{
if (model.getIndexOf(newPlace) != -1)
jcb.removeItem(newPlace);
}
}
public void actionPerformed(ActionEvent e)
{
this.airfirm = (String)jcb.getSelectedItem();
executeAirFirmQuery();
}
public void executeAirFirmQuery()
{
String sqlString = "SELECT DISTINCT * FROM " +
"flight " +
"WHERE airfirm=" + "\'" + airfirm + "\'";
ResultSet rs = sqlBean.executeQuery(sqlString);
if (rs != null)
showResult(rs);
else
JOptionPane.showMessageDialog(null,"没有连接上数据库!",
"错误信息",JOptionPane.ERROR_MESSAGE);
}
public void showResult(ResultSet rs) //将得到的信息在对话框中显示出来
{
String result = " " +
"航空公司查询" + "\n";
result += "查询的航空公司:" + airfirm + "\n";
result += "航班号 航空公司 起飞地点 抵达地点 起飞时间 抵达时间 " +
"儿童票价 成人票价 折扣 班期 " + "\n";
int originLength = result.length(); //用于确定是否有记录
String time1,time2;
String childFare,adultFare,discount1,discount2,seat;
try
{
while(rs.next())
{
result += rs.getString("flight") + rs.getString("airfirm") + rs.getString("start") +
rs.getString("destination");
time1 = rs.getString("leaveTime");
time2 = rs.getString("arriveTime");
time1 = getTime(time1);
time2 = getTime(time2);
result += time1 + " " + time2 + " ";
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"));
while(childFare.length() != 11)
childFare += " ";
while(adultFare.length() != 11)
adultFare += " ";
while(discount1.length() != 8)
discount1 += " ";
result += childFare + adultFare + discount1 +
rs.getString("week");
result += "\n";
}
}
catch(SQLException e)
{
e.printStackTrace();
}
if (result.length() == originLength)//提示用户未查找到航班信息
{
result += " " +
"对不起,找不到你想要的航班信息!" + "\n";
}
//将查询结果显示出来
JOptionPane.showMessageDialog(null,result,"查询结果",JOptionPane.PLAIN_MESSAGE);
}
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -