添加图书的方法会在servlet里面调用并将结果保存在数据库中。
/**
* 添加图书
*
* @param book 待添加的图书对象
* @return 添加图书是否成功,成功返回true,否则返回false
*/
public boolean addBook(Book book) {
// 获取图书信息
boolean flag = false;
String bookName = book.getBookName();
float bookPrice = book.getBookPrice();
String bookPublisher = book.getBookPublisher();
String bookDespriction = book.getBookDesc();
// 拼接插入图书的sql语句
String sql = "insert into Book(book_name,book_price,book_publisher,book_description) values ('" + bookName
+ "'," + bookPrice + ",'" + bookPublisher + "','" + bookDespriction + "')";
int n = -1;// 存储插入的记录数
try {
n = dbUtil.addDataToTable(sql);
if (n > 0) {
flag = true;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置编码方式
// request.setCharacterEncoding("UTF-8");
// response.setContentType("text/html;charset=UTF-8");
// 获取图书信息
BookService bookservice = new BookService();
String bookName = request.getParameter("bookName");
String bookPublisher = request.getParameter("bookPublisher");
String bookDescription = request