-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBook.java
More file actions
202 lines (145 loc) · 3.83 KB
/
GetBook.java
File metadata and controls
202 lines (145 loc) · 3.83 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package Action;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.opensymphony.xwork2.ActionSupport;
public class GetBook extends ActionSupport{
private String author;
private int age;
private String isbn;
private String title;
private String authorid;
private String publisher;
private Date publishdate;
private String country;
private float price;
public String getTitle() {
return title;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Date getPublishdate() {
return publishdate;
}
public void setPublishdate(Date publishdate) {
this.publishdate = publishdate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthorid() {
return authorid;
}
public void setAuthorid(String authorid) {
this.authorid = authorid;
}
private Connection conn = null;
private Statement st = null;
/**
* execute
*/
public String execute(){
//isbn = "Hello everybody!!";
queryAuthorid();
queryBook();
return "success";
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bookdb", "root", "123456");// 创建数据连接
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}
public void queryAuthorid(){
conn = getConnection(); //同样先要获取连接,即连接到数据库
try{
String sql = "select * from author where name = '"+author+"'"; // 查询数据的sql语句
st = conn.createStatement(); //创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rs = st.executeQuery(sql); //执行sql查询语句,返回查询数据的结果集
while (rs.next()) {
authorid = rs.getString("authorid");
country = rs.getString("country");
age = rs.getInt("age");
System.out.println(authorid);
}
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void queryBook() {
conn = getConnection(); //同样先要获取连接,即连接到数据库
try{
String sql = "select * from book where authorid = '"+authorid+"'"; // 查询数据的sql语句
st = conn.createStatement(); //创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rs = st.executeQuery(sql); //执行sql查询语句,返回查询数据的结果集
while (rs.next()) {
isbn = rs.getString("isbn");
title = rs.getString("title");
publisher = rs.getString("publisher");
publishdate = rs.getDate("publishdate");
price = rs.getFloat("price");
System.out.println(isbn);
System.out.println(publisher);
System.out.println(publishdate);
System.out.println(title);
}
rs.close();
}catch (Exception e) {
System.out.println("failed!!!");
}finally{
try {
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
*
* @set and get
*/
}