import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Select extends JFrame implements ActionListener {
// 定义两个向量,用来存放表中数据和列名
Vector rowVectors = new Vector();
Vector columnHeaderVector = new Vector();
JTable table = new JTable();
JScrollPane jsp = new JScrollPane(table);
private int columnCount;
private DefaultTableModel tableModel = new DefaultTableModel();
private DefaultListSelectionModel listSelectionModel = new DefaultListSelectionModel();
JPanel panel = new JPanel();
JButton scheduleBu = new JButton(new ImageIcon("images/schedule.jpg"));
JButton updateBu = new JButton(new ImageIcon("images/update.jpg"));
JButton exit = new JButton(new ImageIcon("images/cancel.jpg"));
ResultSet resultSet;
int selected;
public Select() {
this.setTitle("机票预定");
this.setVisible(true);
this.setResizable(false);
this.setSize(650, 500);
this.setLocation(500, 10);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(panel);
panel.setLayout(null);
panel.add(scheduleBu);
panel.add(updateBu);
panel.add(exit);
scheduleBu.setBounds(100, 350, 105, 38);
updateBu.setBounds(400, 350, 110, 35);
exit.setBounds(250, 420, 110, 35);
scheduleBu.addActionListener(this);
updateBu.addActionListener(this);
exit.addActionListener(this);
listSelectionModel.addListSelectionListener(new
ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
listSelectionModel_valueChanged(e);
}
});
tableModel = new DefaultTableModel() {
public boolean isCellEditable(int row, int col) {
// TODO 自动生成的方法存根
if (col == 0 || col == 1 || col == 2 || col == 3 || col == 4 || col ==5 || col == 6) {
return false;
} else {
return true;
}
}
};
tableModel = connectionDatabase();
table.setModel(tableModel);
table.setSelectionModel(listSelectionModel);
addTable();
}
/*
* 连接到数据库中
*/
public DefaultTableModel connectionDatabase() {
try {
// 1.加载驱动,使用反射知识。
Class.forName("com.mysql.jdbc.Driver");
// 2.获取连接
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/plane_manager_system", "root",
"");
// 结果集可滚动和可刷新
Statement statement = connection
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from ticket where leave_address='"
+ Inquire.getLeaveAddress() + "' and arrive_address='"
+ Inquire.getarriveAddress() + "' and go_date='"
+ Inquire.getleaveDate() + "'";
ResultSet resultSet = statement.executeQuery(sql);
// 得到列的数量
columnCount = resultSet.getMetaData().getColumnCount();
// 存放表格中的所用行
while (resultSet.next()) {
Vector singleRow = new Vector(); // 行向量
for (int i = 0; i < columnCount; i++)
// 将结果集中的每条记录都复制到一个行向量中
singleRow.addElement(resultSet.getObject(i + 1));
// 将上面的行向量添加到另一个行向量中。
rowVectors.addElement(singleRow);
}
// 设置表格中的列名
columnHeaderVector.add("始发地");
columnHeaderVector.add("目的地");
columnHeaderVector.add("班次");
columnHeaderVector.add("时间");
columnHeaderVector.add("仓位");
columnHeaderVector.add("票价");
columnHeaderVector.add("剩余数量");
} catch (ClassNotFoundException cnfe) {
JOptionPane.showMessageDialog(null, "数据源错误", "错误",
JOptionPane.ERROR_MESSAGE);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
tableModel.setDataVector(rowVectors, columnHeaderVector);
return tableModel;
}
private void addTable() {
table.getTableHeader().setReorderingAllowed(false); // 禁止数列的移动
table.getTableHeader().setResizingAllowed(false);// 禁止表列间拖动改变
table.setSelectionMode(0); // 禁止多项选择
// 将JTable加入到带滚动条的面板中
jsp = new JScrollPane(table);
jsp.setBounds(15, 10, 600, 300);
panel.add(jsp);
panel.validate(); // 使用 validate() 方法会使容器再次布置其子组件
}
/*
* 改
*/
public void order() {
System.out.println(0);
DefaultTableModel model = (DefaultTableModel) table.getModel();
//得到当前修改后的值
String num = (String) model.getValueAt(selected, 6);
String num1 = (String) model.getValueAt(selected, 4);
String num2 = (String) model.getValueAt(selected, 2);
System.out.println(num+"111"+num1+"\n"+"num2");
// String id2 = (String)model.getValueAt(selected, 0);
// String pay2 =(String)model.getValueAt(selected, 6);
/* int n = Integer.parseInt(num);
int numberer = n-1;
String number = numberer+"";*/
// 修改数据库中的值
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/plane_manager_system";
String user = "root";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
// sql语句
String sql = "update ticket set remainder = ? where shipping_space = ? and classes = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, String.valueOf(Integer.parseInt(num)-1));
ps.setString(2, num1);
ps.setString(3, num2);
// alter.setString(1, pay2);
// alter.setString(2, id2);
// 执行sql语句
ps.executeUpdate();
// 关闭资源
ps.close();
conn.close();
// 显示修该成功
System.out.println(1);
JOptionPane.showMessageDialog(null, "预定成功!");
} catch (SQLException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
public DefaultTableModel update() {
listSelectionModel.addListSelectionListener(new
ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
listSelectionModel_valueChanged(e);
}
});
addTable();
try {
// 1.加载驱动,使用反射知识。
Class.forName("com.mysql.jdbc.Driver");
// 2.获取连接
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/plane_manager_system", "root",
"");
// 结果集可滚动和可刷新
Statement statement = connection
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from ticket ";
ResultSet resultSet = statement.executeQuery(sql);
// 得到列的数量
columnCount = resultSet.getMetaData().getColumnCount();
// 存放表格中的所用行
while (resultSet.next()) {
Vector singleRow = new Vector(); // 行向量
for (int i = 0; i < columnCount; i++)
// 将结果集中的每条记录都复制到一个行向量中
singleRow.addElement(resultSet.getObject(i + 1));
// 将上面的行向量添加到另一个行向量中。
rowVectors.addElement(singleRow);
}
} catch (ClassNotFoundException cnfe) {
JOptionPane.showMessageDialog(null, "数据源错误", "错误",
JOptionPane.ERROR_MESSAGE);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
tableModel.setDataVector(rowVectors, columnHeaderVector);
return tableModel;
}
void listSelectionModel_valueChanged(ListSelectionEvent e) {
int selectedRow = table.getSelectedRow();
try {
resultSet.absolute(selectedRow + 1);