1. Demand
Known as two tables:
Publisher the above mentioned id name (unique) address
Book the above mentioned id isbn name publisher_id
Welcome to the book management system
1. Press management: add, delete (name), change the (name), Charles (name)
2. Books management : add, delete (name), changing (name), search (name)
3. exit
. 1
2
. 3
. 4
. 5
. 6
. 7
2.log4j.properties Why
3.db.properties Why
4.PropertiesUtil
5.DBUtil
6.IRowMapper
. 7. PublisherManager
8.BookManager
9.Client
a first create the following two tables in the mysql database software
Create Table Publisher (
ID char (36) Primary Key,
name VARCHAR (24) UNIQUE,
address VARCHAR (120)
)
Create Table Book (
ID char (36) Primary Key,
ISBN VARCHAR (12 is) UNIQUE,
name VARCHAR (24),
char publisher_id (36),
Foreign Key (publisher_id) References Publisher (ID)
)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
knowledge of the SQL statement constraints mentioned below in the blog
https: //blog.csdn the .NET / qq_44724446 / Article This article was / the Details / 90,027,794
https://blog.csdn.net/qq_44724446/article/details/90031272
log4j Why how to configure, view,
https://blog.csdn.net/qq_44724446/article/details/90044781
the Properties Why, how to configure, implement java code - Encapsulated tools PropertiesUtil View
https://blog.csdn.net/qq_44724446/article/details / 90,048,065
DBUtil and IRowMapper view
https://blog.csdn.net/qq_44724446/article/details/89918464
was not taking into account SQL injection, so supplemental code, introduce the concept of PreparedStatement interface.
// sql injection step on the prevention of reference to the following
http://www.cnblogs.com/flei/p/6727520.html
. 1
2
create the UUID tools 36
import java.util.UUID;
public class StringUtil {
public static String getId() {
return UUID.randomUUID().toString();
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
Creating Press CRUD system implementation class for a package
the java.sql.ResultSet Import;
Import java.util.Scanner;
public class PublisherManager {
static Scanner Scanner new new SC = (the System.in);
/ **
* Press Management
* /
public static void pManager () {
the System.out. println ( "Please enter the imprint management operations:");
System.out.println (. "Press 1 to add information");
System.out.println (. "2 delete Press information");
System.out.println ( "3. modify the press information");
System.out.println ( "press 4 query information.");
System.out.println ( "Please enter the operation, press the enter key to end");
int the Option = sc. the nextInt ();
Switch (Option) {
Case. 1:
pInsert ();
BREAK;
Case 2:
pDelete ();
BREAK;
Case. 3:
pUpdate ();
BREAK;
case 4:
pSelect ();
BREAK;
default:
System.out.println ( "Sorry, there IS not at The" + the Option + "the Option, please re-enter selection:");
BREAK;
}
}
/ **
* Add Publishing information
* /
static void pInsert public () {
String the above mentioned id = StringUtil.getId ();
System.out.println ( "Press enter the name you want to add");
String name = sc.next ();
IF (new new PublisherManager () .exit (name)) {// determine whether there are publishers
System.out.println ( "Press name must be unique, can not be added, the operation is stopped");
return;
}
System.out.println ( "enter to add the publishing house's address ");
String address = sc.next ();
String SQL =" INSERT INTO Publisher (the above mentioned id, name, address) values (,,) ";???
IF (DButil.upDate (SQL, the above mentioned id , name, address)) {
System.out.println ( "add information successfully");
return;
}
System.out.println ( "System error, add information failed");
}
/ **
* Delete Press information
* /
public static void pDelete () {
System.out.println ( "Please enter the name you want to delete Press");
String name = sc.next ();
(!. new new PublisherManager () Exit (name)) {// iF determine whether there is a publishing house, no way to remove non-existent
; System.out.println ( "Press the name does not exist, you can not delete, stop")
return;
}
String SQL = "Publisher the delete from the WHERE name =?";
IF (DButil.upDate (SQL, name)) {
System.out.println ( "success information deleting");
return;
} the else {
System.out.println ( "system error, deletion information failed");
}
}
/ **
* Review Press information
* /
static void pUpdate public () {
System.out.println ( "Please enter the name you want to modify Press:");
String name = sc.next ();
IF (!. new new PublisherManager () Exit (name)) { Press // determine whether there is, there is no way to modify
System.out.println ( "Press the name does not exist, can not be modified, stop operation");
return;
}
after System.out.println ( "Please enter the modification Press name: ");
String = sc.next the rename ();
System.out.println (" Please enter an address to modify Press ");
String address = sc.next ();
String SQL =" Update the SET Publisher name WHERE ?, address name = = =?? ";
IF (DButil.upDate (SQL, the rename, address, name)) {
System.out.println (" successful modification information ");
return;
} {the else
the System.out. println ( "system error, the modification information failed");
}
System.out.println (SQL);
}
/**
* Find press information
* /
public static void pSelect () {
System.out.println ( "Please enter the name you want to find publishers:");
String name = sc.next ();
String SQL = "from the SELECT * WHERE name = Publisher ";?
// IRowMapper the rowMapper class implements the interface
class the implements the rowMapper IRowMapper {
// override method rowMapper IRowMapper interface
@Override
public void rowMapper (the ResultSet RS) {
the try {
// queries one by one to the data in the database
if (rs.next ()) {// the name is unique because Press
// according to different table column name in getString
// can pass different parameters for different tables getString method
String id = rs .getString ( "the above mentioned id");
String name = rs.getString ( "name");
String address = rs.getString ( "address");
System.out.println ( "successfully inquiry information, the information is:");
System.out.println ( "Press id is:" + id + ", Press named:" + name + ", Publisher address:" address +);
return;
} the else {
System.out.println ( "Press absent");
}
} the catch (Exception E) {
e.printStackTrace ();
}
}
}
// create objects RowMapper
RowMapper rowMapper new new RowMapper = ();
DButil.sel (rowMapper, SQL, name);
}
/ **
* The name is determined whether there Press
* /
public Boolean Exit (String name) {
return getId (name) = null;!
}
/ **
* Get Press ID
* /
public String getId (String name) {
String = SQL "from the SELECT the above mentioned id Publisher the WHERE name =?";
class the implements the RowMapper for a IRowMapper {
ID String;
@Override
public void rowMapper (the ResultSet RS) {
the try {
IF (rs.next ()) {
ID = rs.getString ( "ID");
}
} the catch (Exception E) {
e.printStackTrace ();
}
}
}
the rowMapper rowMapper the rowMapper new new = ();
DButil.sel (rowMapper, SQL, name); //-injection, the callback
return rowMapper.id;
}
/ **
* Press show all information
* /
public static void Show () {
System.out.println ( "All of the information Press:");
String SQL = "from the SELECT * Publisher";
class the implements the rowMapper for a IRowMapper {
@Override
public void rowMapper (ResultSet rs) {
the try {
the while (rs .next ()) {
String id = rs.getString("id");
String name = rs.getString("name");
String address = rs.getString("address");
System.out.println(id + "," + name + "," + address);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
DButil.sel(rowMapper, sql);
}
}
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
Creating a system to achieve CRUD book package type
noted foreign key constraints
the java.sql.ResultSet Import;
Import java.sql.SQLException;
Import java.util.Scanner;
public class the BookManager {
static Scanner Scanner new new SC = (the System.in);
/ **
* book management function interface
* /
public static void bManager () {
System.out.println ( "Please enter the library management operation:");
System.out.println ( "1, add the book information");
System.out.println ( "2, delete the book information");
System.out.println ( "3, modify book information");
System.out.println ( ". 4, book information query");
int = sc.nextInt Option ();
Switch (Option) {
Case. 1:
bInsert () ;
BREAK;
Case 2:
BDELETE ();
BREAK;
Case. 3:
bUpdate ();
BREAK;
Case. 4: {
System.out.println ( "Please select a query mode:");
System.out.println (. "1 According to the title list query");
System.out.println (. "2 According ISBN unique query");
the Option = sc.nextInt ();
Switch (Option) {
Case. 1:
bSelect ();
BREAK;
Case 2:
bquery ();
BREAK;
default:
System.out.println ( "invalid operation, please re-enter select a query mode:") ;
BREAK;
}
BREAK;
}
default:
System.out.println ( "invalid operation");
BREAK;
}
}
/ **
* add book information
* /
public static void bInsert () {
System.out.println ( "enter to add a bar code books: ");
String ISBN sc.next = ();
// determines whether ISBN exists
if (exit (isbn)) {
System.out.println ( "ISBN allowed to repeat, can not add, stop operation");
return;
}
System.out.println ( "Please enter the books you want to add the name:");
String name = sc.next () ;
// call StringUtil class util tools get 36 automatically generated UUID consisting id number
String id = StringUtil.getId ();
// will show all of the information publishers out
PublisherManager.show ();
system. out.println ( "Please select which books belong in the name of the publishing house:");
String pName = sc.next ();
IF (!. new new PublisherManager () Exit (pName)) {
System.out.println ( "you entered Press the wrong name, stop operation ");
return;
}
// If you select publishers, obtain the name of the publishing house according to press id
String = pId new new PublisherManager () getId (pName);.
// Add book database table information
String sql = "insert into book ( id, isbn, name, publisher_id) values (,,,????)";
IF (DButil.upDate (SQL, ID, ISBN, name, pId)) {
System.out.println ( "add information successfully");
return;
} the else {
System.out.println ( "system error, add information failed" );
}
}
/ **
* Remove the book information
* /
public static void BDELETE () {
System.out.println ( "Please enter the books you want to delete ISBN:");
String isbn = sc.next ();
// judge ISBN the existence of
iF (Exit (isbn)!) {
System.out.println ( "ISBN does not exist, you can not delete, stop");
return;
}
String SQL = "Book the delete from the WHERE isbn =?";
iF (DBUtil. UPDATE (SQL, isbn)) {
System.out.println ( "deleted successfully!");
} the else {
System.out.println ( "system error, delete failed!");
}
}
/ **
* Modify the book information
* /
static void bUpdate public () {
System.out.println ( "Please enter the ISBN book to be modified:");
String isbn = sc.next ();
// determine whether there is ISBN
IF {(Exit (isbn)!)
System .out.println ( "ISBN does not exist, can not be modified, stop operation");
return;
}
System.out.println ( "Please enter the books change the name:");
String = sc.next the rename ();
System .out.println ( "Please enter the bar coded modified books:");
String reIsbn sc.next = ();
IF (Exit (reIsbn)) {
System.out.println ( "ISBN already exists, because they do not allow duplicate all can not be modified, stop operation ");
return;
}
// will show all of the information out of publishing
PublisherManager.show ();
System.out.println (" Please enter the imprint name the revised book belongs to: ") ;
String sc.next pName = ();
// publisher determines whether there is a manual input
if (new PublisherManager ()!.exit(pName)) {
System.out.println ( "Press the name you entered is incorrect, stop operation");
return;
}
// If you select publishers exist, according to Press acquire publishing id name
. String pId = new PublisherManager () getId ( pName);
String SQL = "Update Book the SET name = ?, isbn = ?, publisher_id the WHERE isbn = =??";
IF (DButil.upDate (SQL, the rename, reIsbn, pId, isbn)) {
System.out.println ( "modified successfully!");
return;
} the else {
System.out.println ( "system error, modification failed!");
}
}
/ **
* Find a book you can check more information
* /
public static void bSelect () {
System.out.println ( "Please enter the name of the book you want to query:");
String name = sc.next ();
String SQL = "b.id the SELECT, b.isbn, b.name book_name, p.name publisher_name, p.address "+" from book b "
+ "Inner Publisher the Join P ON = b.publisher_id p.id" + "WHERE b.name like '%" + name + "%'";
System.out.println (SQL);
class the implements either a IRowMapper the RowMapper {
@Override
public rowMapper void (the ResultSet RS) {
the try {
the while (rs.next ()) {
String ID = rs.getString ( "ID");
String = ISBN rs.getString ( "ISBN");
String bookName = rs.getString ( " book_name ");
String PublisherName = rs.getString (" publisher_name ");
System.out.println (" successfully inquiry information, the information is: ");
System.out.println (
" id book is: "+ id +" books coding "+ isbn +", the name for the book: "+ bookName +", publishing house named: "+ publisherName);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
= New new rowMapper the RowMapper the RowMapper ();
DButil.sel (rowMapper, SQL);
}
/ **
* The uniqueness of the inquiry ISBN
* /
public static void bquery () {
System.out.println ( "Please enter the book to be queried ISBN: ");
String ISBN = sc.next ();
String SQL =" Book the SELECT * from the WHERE isbn = ";?
class the implements the rowMapper for a IRowMapper {
@Override
public void rowMapper (ResultSet rs) {
the try {
IF (rs.next ()) {
String the above mentioned id = rs.getString ( "the above mentioned id");
String isbn = rs.getString ( "isbn");
String bookName = rs.getString ( "name");
System.out.println ( "successfully inquiry information information is: ");
System.out.println (" id book is: "+ id +" books coding "+ isbn +",Books named: "+ bookName);
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
DButil.sel(rowMapper, sql,ISBN);
}
/**
* 根据ISBN的唯一性判断图书是否存在
*/
public static boolean exit(String isbn) {
String sql = "select id from book where isbn= ? ";
class RowMapper implements IRowMapper {
boolean state;
@Override
public void rowMapper(ResultSet rs) {
try {
if (rs.next()) {
state = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RowMapper rowMapper = new RowMapper();
DButil.sel(rowMapper,sql , isbn);
return rowMapper.state;
}
}
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
creation system master methods
java.util.Scanner Import;
public class Client {
static Scanner sc = new new Scanner (System.in);
public static void the MENU () {
the while (to true) {
System.out.println ( "Welcome to the books management system");
System.out.println ( "1 Publishing management.");
System.out.println ( "2 book management.");
System.out.println ( "otherwise, exit the system");
System.out.println ( " enter operation, press enter end ");
int = sc.nextInt Option ();
Switch (Option) {
Case. 1:
PublisherManager.pManager ();
BREAK;
Case 2:
BookManager.bManager ();
BREAK;
default:
the System .out.println ( "end system");
System.exit (0);// Exit the system
}
}
}
public static void main (String [] args) {
menu();
}
}
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