📄 insertpanel.java~12~
字号:
package flight.manage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import flight.assist.DataBaseManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class InsertPanel
extends JPanel {
DataBaseManager db = new DataBaseManager();
private JTextField[] jtf = new JTextField[12];
private JLabel[] label = new JLabel[12];
private JButton[] button = new JButton[3];
private JComboBox hour1, hour2, min1, min2;
public InsertPanel() {
hour1 = new JComboBox();
hour2 = new JComboBox();
min1 = new JComboBox();
min2 = new JComboBox();
for (int i = 1; i <= 24; i++) {
if (i < 10) {
hour1.addItem("" + 0 + i);
hour2.addItem("" + 0 + i);
}
else {
hour1.addItem("" + i);
hour2.addItem("" + i);
}
}
for (int i = 0; i <= 59; i++) {
if (i < 10) {
min1.addItem("" + 0 + i);
min2.addItem("" + 0 + i);
}
else {
min1.addItem("" + i);
min2.addItem("" + i);
}
}
JPanel p3 = new JPanel();
p3.add(hour1);
p3.add(new JLabel("时"));
p3.add(min1);
p3.add(new JLabel("分"));
JPanel p4 = new JPanel();
p4.add(hour2);
p4.add(new JLabel("时"));
p4.add(min2);
p4.add(new JLabel("分"));
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder("基本信息"));
p1.setLayout(new GridLayout(6, 4, 5, 5));
p1.add(label[0] = new JLabel("航班号"));
p1.add(jtf[0] = new JTextField(10));
p1.add(label[1] = new JLabel("班期"));
p1.add(jtf[1] = new JTextField(10));
p1.add(label[0] = new JLabel("公司"));
p1.add(jtf[2] = new JTextField(10));
p1.add(label[0] = new JLabel("座位"));
p1.add(jtf[3] = new JTextField(10));
p1.add(label[0] = new JLabel("起飞地"));
p1.add(jtf[4] = new JTextField(10));
p1.add(label[0] = new JLabel("抵达地"));
p1.add(jtf[5] = new JTextField(10));
p1.add(label[0] = new JLabel("起飞时间"));
p1.add(p3);
// p1.add(jtf[6]=new JTextField(10));
p1.add(label[0] = new JLabel("抵达时间"));
p1.add(p4);
// p1.add(jtf[7]=new JTextField(10));
p1.add(label[0] = new JLabel("儿童票价"));
p1.add(jtf[8] = new JTextField(10));
p1.add(label[0] = new JLabel("成人票价"));
p1.add(jtf[9] = new JTextField(10));
p1.add(label[0] = new JLabel("提前折扣"));
p1.add(jtf[10] = new JTextField(10));
p1.add(label[0] = new JLabel("退票率"));
p1.add(jtf[11] = new JTextField(10));
JPanel p2 = new JPanel();
p2.setBorder(new TitledBorder("操作"));
p2.add(button[0] = new JButton("插入"));
// p2.add(button[1]=new JButton("删除"));
// p2.add(button[2]=new JButton("更新"));
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
button[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jtf[0].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "关键字不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[2].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "公司为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[3].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "座位数为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[4].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "起飞地为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[5].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "抵达地为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[8].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "信息不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[9].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "信息不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[10].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "信息不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (jtf[11].getText().length() == 0) {
JOptionPane.showMessageDialog(null, "信息不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
else {
int maxremark = 1;
String sql = "select remark from flight";
ResultSet rs = db.getResult(sql);
int remark = 0;
try {
while (rs.next()) {
System.out.println(maxremark);
remark = rs.getInt(1);
if (remark > maxremark) maxremark = remark;
}
}
catch (SQLException sqle) {
System.out.println(sqle.toString());
}
catch (Exception ex) {
System.out.println(ex.toString());
}
String time1 = (String) hour1.getSelectedItem() +
(String) min1.getSelectedItem();
String time2 = (String) hour2.getSelectedItem() +
(String) min2.getSelectedItem();
maxremark = maxremark + 1; //ID
sql = "insert into flight values('" + jtf[0].getText() +
"','" + jtf[2].getText() + "','" +
jtf[4].getText() + "','" + jtf[5].getText() + "','" + time1 +
"','" +
time2 + "'," + Float.parseFloat(jtf[8].getText().trim()) + "," +
Float.parseFloat(jtf[9].getText().trim()) + ","
+ Float.parseFloat(jtf[10].getText().trim()) + "," +
Float.parseFloat(jtf[11].getText().trim()) + "," +
Integer.parseInt(jtf[3].getText().trim())
+ ",'" + jtf[1].getText() + "'," + maxremark + ")";
int affectedNum=db.updateSql(sql);
if(affectedNum>0)
JOptionPane.showMessageDialog(null, "插入记录成功!");
else
JOptionPane.showMessageDialog(null, "插入记录失败!");
db.closeConnection();
}
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -