0% found this document useful (0 votes)
11 views5 pages

đề tour

Uploaded by

Bui Minh Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

đề tour

Uploaded by

Bui Minh Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Câu 1:

JtextField tenTourTF = new JTextField();


JtextField ngKhoiHanhTF = new JTextField();
JtextField soNgayTF = new JTextField();
JtextField soDemTF = new JTextField();
JtextField giaTourTF = new JTextField();
JButton themBtn = new JButton("Thêm");

themBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "INSERT INTO TOUR (TenTour, NgayKhoiHanh, SoNgay, SoDem, Gia) VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, tenTourTF.getText());
statement.setDate(2, java.sql.Date.valueOf(ngKhoiHanhTF.getText()));
statement.setInt(3, Integer.parseInt(soNgayTF.getText()));
statement.setInt(4, Integer.parseInt(soDemTF.getText()));
statement.setDouble(5, Double.parseDouble(giaTourTF.getText()));
statement.executeUpdate();
databaseConnection.closeDatabaseConnection(conn);
}
});

Câu 2:

JTextField tenDiemDuLichTF = new JTextField();


JTextField dacTrungTF = new JTextField();
JButton themBtn = = new JButton("Thêm");
JComboBox<String> tenTinhComboBox = new JComboBox<String>();

//Load từ csdl lên combobox

ArrayList<String> maTinh = new ArrayList<String>();


ArrayList<String> tenTinh = new ArrayList<String>();
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "SELECT MATTP, TENTTP FROM TINHTP";
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while(rs.next()) {
maTinh.add(rs.getString(1));
tenTinh.add(rs.getString(2));
tenTinhComboBox.addItem(rs.getString(2));
}
databaseConnection.closeDatabaseConnection(conn);

//Cách 1:Không dùng ItemListener

themBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "INSERT INTO DIEMDL (MaTTP, TenDDL, DacTrung) VALUES (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, (String)maTinh.get(tenTinhComboBox.getSelectedIndex()));
statement.setString(2, tenDiemDuLichTF.getText());
statement.setString(3, dacTrungTF.getText());
statement.executeUpdate();
}
});
//Cách 2:Dùng ItemListener

String outMaTinh;

tenTinhComboBox.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
{
outMaTinh = (String)maTinh.get(tenTinhComboBox.getSelectedIndex());
}
}
});
themBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "INSERT INTO DIEMDL (MaTTP, TenDDL, DacTrung) VALUES (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, outMaTinh);
statement.setString(2, tenDiemDuLichTF.getText());
statement.setString(3, dacTrungTF.getText());
statement.executeUpdate();
}
});

Câu 3:

JTextField maTourTF = new JTextField();


JTextField tenTourTF = new JTextField();
JTextField ngKhoiHanhTF = new JTextField();
JTextField soNgayTF = new JTextField();
JTextField soDemTF = new JTextField();
JTextField giaTourTF = new JTextField();
JButton themBtn = new JButton("Thêm");
JComboBox tenTinhComboBox = new JComboBox();

DefaultTableModel tbModel1 = new DefaultTableModel();


String tieuDe[] = {"Tên điểm du lịch", "Đặc trưng"};
tbModel1.setColumnIdentifiers(tieuDe);
JTable table1 = new JTable(tbModel1);
JScrollPane scrollPane1 = new JScrollPane();
scrollPane1.setViewportView(table1);

DefaultTableModel tbModel2 = new DefaultTableModel();


JTable table2 = new JTable(tbModel2);
String tieuDe2[] = {"Tên điểm du lịch"};
tbModel2.setColumnIdentifiers(tieuDe2);
JScrollPane scrollPane2 = new JScrollPane();

//Load từ csdl lên combobox

ArrayList<String> maTinh = new ArrayList<String>();


ArrayList<String> tenTinh = new ArrayList<String>();
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "SELECT MATTP, TENTTP FROM TINHTP";
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while(rs.next()) {
maTinh.add(rs.getString(1));
tenTinh.add(rs.getString(2));
tenTinhComboBox.addItem(rs.getString(2));
}
databaseConnection.closeDatabaseConnection(conn);
//Xử lý sự kiện nhấn enter

tenTourTF.setEditable(false);
ngKhoiHanhTF.setEditable(false);
soNgayTF.setEditable(false);
soDemTF.setEditable(false);
giaTourTF.setEditable(false);
maTourTF.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "SELECT TENTOUR, NGAYKHOIHANH, SONGAY, SODEM, GIA FROM TOUR WHERE MATOUR = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, maTourTF.getText());
ResultSet rs = statement.executeQuery();
while(rs.next()) {
tenTourTF.setText(rs.getString(1));
ngKhoiHanhTF.setText(rs.getString(2));
soNgayTF.setText(rs.getString(3));
soDemTF.setText(rs.getString(4));
giaTourTF.setText(rs.getString(5));
}
databaseConnection.closeDatabaseConnection(conn);
}
}
});
//Xử lý sự kiện chọn combobox tên tỉnh sẽ hiển thị danh sách điểm du lịch thuộc tỉnh đó

String outMaTinh;

tenTinhComboBox.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
{
outMaTinh = (String)maTinh.get(tenTinhComboBox.getSelectedIndex());
}
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "SELECT TENDDL, DACTRUNG FROM DIEMDL WHERE MATTP = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, outMaTinh);
ResultSet rs = statement.executeQuery();
DefaultTableModel temp = new DefaultTableModel();
temp.setColumnIdentifiers(tieuDe);
while(rs.next()) {
Object dataRow[] = new Object[2];
dataRow[0] = rs.getString(1);
dataRow[1] = rs.getString(2);
temp.addRow(dataRow);
}
table1.setModel(temp);
databaseConnection.closeDatabaseConnection(conn);
});
//Xử lý sự kiện chọn điểm du lịch sẽ đưa vào bảng điểm du lịch được chọn

table1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
int selectedRow = table1.getSelectedRow();
Object[] rowData = new Object[table1.getColumnCount()];
for (int i = 0; i < table1.getColumnCount(); i++) {
rowData[i] = table1.getValueAt(selectedRow, i);
}
tbModel2.addRow(rowData);
}
});
//Xử lý sự kiện thêm lưu các điểm du lịch được chọn cho tour vào CSDL.

themBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String maTour = maTourTF.getText();
String maDDLList[] = new String[tbModel2.getRowCount()];
int cnt = 0;
for(int i = 0; i < tbModel2.getRowCount(); i++) {
String outTenDDL = (String) tbModel2.getValueAt(i, 0);
Connection conn = databaseConnection.getDatabaseConnection();
String sql = "SELECT MADDL FROM DIEMDL WHERE TENDDL = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, outTenDDL);
ResultSet rs = statement.executeQuery();
while(rs.next()) {
maDDLList[cnt] = rs.getString(1);
cnt++;
}
databaseConnection.closeDatabaseConnection(conn);
}

for(int i = 0; i < maDDLList.length; i++) {


Connection conn = databaseConnection.getDatabaseConnection();
String sql = "INSERT INTO CHITIET (MaTour, MaDDL) VALUES (?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, maTour);
statement.setString(2, maDDLList[i]);
statement.executeUpdate();
System.out.println("ok");
databaseConnection.closeDatabaseConnection(conn);
}
}
});

You might also like