ở cái bài này:
http://giasutinhoc.vn/lap-trinh-co-s...va-jdbc-bai-5/
em đã làm đúng hết như bài đó viết, nhưng có cái lúc delete, thì nó vẫn hiện ra là delete , vẫn bấm xóa , và có bảng deleted luôn, nhưng thực tế trong database nó không xóa nghĩa là nó báo là đã xóa rồi, nhưng thực tế nó chưa xóa, vậy e phải làm sao ạ?
Code:
package chuyen.TH3;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
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.Statement;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;
public class BookInformation extends JFrame{
private int id;
private String header[] = {"ID", "Title", "Price"};
private DefaultTableModel tblModel = new DefaultTableModel(header, 0);
JPanel pnl1 = new JPanel();
JLabel lblTitle = new JLabel();
JTextField txtTitle = new JTextField();
JButton btnSaerch = new JButton();
JButton btnExit = new JButton();
JScrollPane pnl2 = new JScrollPane();
JTable jTable1 = new JTable();
JPanel pnl3 = new JPanel();
JButton btnDelete = new JButton();
public BookInformation(){
pnl1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Filter"));
lblTitle.setText("Title : ");
lblTitle.setPreferredSize(new Dimension(40,30));
txtTitle.setColumns(25);
txtTitle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
txtTitleA(evt);
}
});
btnSaerch.setText("Search");
btnSaerch.setPreferredSize(new Dimension(80, 20));
btnSaerch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnSearchA(evt);
}
});
btnExit.setText("Exit");
btnExit.setPreferredSize(new Dimension(60, 20));
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnExitA(evt);
}
});
pnl1.add(lblTitle);
pnl1.add(txtTitle);
pnl1.add(btnSaerch);
pnl1.add(btnExit);
pnl2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
jTable1.setModel(new DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"ID", "Title", "Price"
}
));
pnl2.setViewportView(jTable1);
btnDelete.setText("Delete");
btnDelete.setPreferredSize(new Dimension(80, 20));
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnDelA(evt);
}
});
pnl3.add(btnDelete);
getContentPane().add(pnl1);
getContentPane().add(pnl2);
getContentPane().add(pnl3);
setTitle("Book Information");
setSize(550, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new GridLayout(3,1));
setCenter();
}
private void setCenter() {
int width = this.getWidth();
int height = this.getHeight();
Toolkit tk = Toolkit.getDefaultToolkit();
int screenW = (int)tk.getScreenSize().getWidth();
int screeH = (int)tk.getScreenSize().getHeight();
this.setBounds((screenW-width)/2, (screeH-height)/2, width, height);
}
private void searchAction() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"
+ "library", "root", "");
String sql = "select * from Books ";
if (txtTitle.getText().length() > 0) {
sql = sql + " where title like '%" + txtTitle.getText() + "%'";
}
st = conn.createStatement();
rs = st.executeQuery(sql);
Vector data = null;
int count = 1;
tblModel.setRowCount(0);
if (rs.isBeforeFirst() == false) {
JOptionPane.showMessageDialog(this, "The book is not available!");
return;
}
while (rs.next()) {
data = new Vector();
data.add(String.valueOf(count));
data.add(rs.getString("title"));
data.add(rs.getString("price"));
tblModel.addRow(data);
count = count + 1;
}
jTable1.setModel(tblModel);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
if (st != null) {
st.close();
}
if (rs != null) {
rs.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void DeleteAction(){
int ret = JOptionPane.showConfirmDialog(this, "Do you want to delete?", "Confirm", JOptionPane.YES_NO_OPTION);
if(ret != JOptionPane.YES_OPTION) {
return;
}
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"
+ "library", "root", "");
ps = conn.prepareStatement("Delete From Books where id = ?");
ps.setInt(1, id);
ret = ps.executeUpdate();
if (ret != -1) {
JOptionPane.showMessageDialog(this, "This book has been deleted");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
if (ps != null) {
ps.close();
}
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
}
private void btnSearchA(ActionEvent evt){
searchAction();
}
private void btnExitA(ActionEvent evt){
System.exit(0);
}
private void txtTitleA(ActionEvent evt){
searchAction();
}
private void btnDelA(ActionEvent evt){
DeleteAction();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new BookInformation().setVisible(true);
}
}
Bookmarks