-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathBookDelete.java
132 lines (129 loc) · 3.37 KB
/
BookDelete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//BookDelete.java
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
public class BookDelete extends Frame{
Toolkit tool= getToolkit();
String url="bookbk.png";
Image img=tool.getImage(url);
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
String[] lbname={"图书编号","图书名称","图书页数","图书作者","翻 译","出 版 社","出版时间","定价","库存数量","所属类型"};
Label idlb=new Label("图书编号");
TextField idtxt=new TextField();
Label[] booklb=new Label[10];
TextField[] booktxt=new TextField[10];
Button deletebtn=new Button("Delete");
Button closebtn=new Button("Close");
Button querybtn=new Button("查询");
public BookDelete(){
setTitle("删除图书");
setLayout(null);
setSize(500,280);setResizable(false);
int lx=50,ly=80;
idlb.setBounds(100,40,50,20);
idtxt.setBounds(160,40,100,20);
querybtn.setBounds(280,40,80,20);
add(idlb);add(idtxt);add(querybtn);
for(int i=0;i<booklb.length;i++){
if(lx>240){
lx=50;
ly=ly+30;
}
booklb[i]=new Label(lbname[i]);
booktxt[i]=new TextField();
booklb[i].setBounds(lx,ly,50,20);
booktxt[i].setBounds(lx+60,ly,100,20);
lx=lx+190;
add(booklb[i]);add(booktxt[i]);
booktxt[i].setEditable(false);
}
deletebtn.setBounds(150,240,80,25);
closebtn.setBounds(280,240,80,25);
add(deletebtn);add(closebtn);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
DbOp.close();
dispose();
}
});
querybtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
querybtnActionPerformed();
}
});
deletebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
deletebtnActionPerformed();
}
});
closebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
DbOp.close();
dispose();
}
});
setLocationRelativeTo(null);
setVisible(true);
}
public void deletebtnActionPerformed(){
String id=idtxt.getText();
if(id.equals("")){
JOptionPane.showMessageDialog(null,"图书编号不能为空");
return;
}
String sql="delete from book where id='"+id+"'";
int a=DbOp.executeUpdate(sql);
if(a==1){
JOptionPane.showMessageDialog(null,"图书删除成功");
clearAllText();
}else{
JOptionPane.showMessageDialog(null,"图书删除失败");
}
}
public void querybtnActionPerformed(){
String id=idtxt.getText();
if(id.equals("")){
JOptionPane.showMessageDialog(null,"图书编号不能为空");
return;
}
if(IfBookIdExit(id)){
Book book =BookSelect.SelectBookByID(id);
String[] infor=book.getBookinformation();
if(book!=null){
for(int i=0;i<infor.length;i++){
booktxt[i].setText(infor[i]);
}
}
}else{
JOptionPane.showMessageDialog(null,"没有该图书");
}
}
public boolean IfBookIdExit(String id){
boolean right=false;
String sql="select*from book where id='"+id+"'";
ResultSet rs=DbOp.executeQuery(sql);
try{
while(rs.next()){
right = true;
}
//right = false;
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"无法正常读取数据");
}
return right;
}
public void clearAllText(){
for(int i=0;i<booktxt.length;i++){
booktxt[i].setText("");
}
}
public static void main(String[] args){
new BookDelete();
}
}