<%--
Document : addBook02
Created on : 2013-12-10, 11:13:09 Author : new --%>
<%@page contentType=\"text/html\" pageEncoding=\"utf-8\" errorPage=\"error.jsp\"
import=\"java.sql.*,bms_bean.*,javax.ws.rs.core.Response,java.util.Arrays\"%> <%@include file=\"include.jsp\" %>
<%! //添加图书
String addBookSql = \"INSERT INTO books (book_name, price,author_hobby,author_sex,type_id) VALUES (?,?,?,?,?,?,?)\";
public void addBook(Book book) { Connection conn = null;
PreparedStatement prep = null; try {
conn = getConnection();
prep = conn.prepareStatement(addBookSql); prep.setString(1, book.getBookName()); prep.setString(2, book.getIsbn()); prep.setString(3, book.getAuthor()); prep.setFloat(4, book.getPrice());
prep.setString(5, book.getAuthorHobby()); prep.setString(6, book.getAuthorSex()); prep.setInt(7, book.getBookType().getId()); prep.executeUpdate(); } catch (SQLException ex) { ex.printStackTrace();
} catch (ClassNotFoundException ex) { ex.printStackTrace(); } finally { try {
if (prep != null) { prep.close(); }
closeConnection();
} catch (SQLException ex) { ex.printStackTrace(); } } } %>
isbn,
author,
<%
request.setCharacterEncoding(\"utf-8\"); Book book = new Book();
BookType bookType = new BookType();
book.setBookName(request.getParameter(\"bookName\")); book.setAuthor(request.getParameter(\"author\")); book.setIsbn(request.getParameter(\"isbn\"));
book.setPrice(Float.parseFloat(request.getParameter(\"price\")));
//爱好的添加
String[] hobbyRel = request.getParameterValues(\"authorHobby\"); StringBuffer authorHobbySB = new StringBuffer();
String[] hobbyArr = {\"java\旅游\上网\看书\睡觉\钓鱼\吃货\跳舞\美术\唱歌\
for (int i = 0; i < hobbyArr.length; i++) { String str = hobbyArr[i]; boolean flag = false;
for (int j = 0; j < hobbyRel.length; j++) { if (str.equals(hobbyRel[j])) {
flag = true;//说明有这项爱好 break; } }
if (flag) {
authorHobbySB.append(\"1\"); } else {
authorHobbySB.append(\"0\"); } }
book.setAuthorHobby(authorHobbySB.toString());
book.setAuthorSex(request.getParameter(\"authorSex\"));
bookType.setId(Integer.parseInt(request.getParameter(\"typeId\"))); book.setBookType(bookType); addBook(book);
response.setCharacterEncoding(\"utf-8\"); out.print(true); %>
addBookForm.jsp
<%--
Document : addBook
Created on : 2013-12-10, 10:00:38
Author : new --%>
<%@page contentType=\"text/html\" pageEncoding=\"utf-8\" errorPage=\"error.jsp\"%>
addValidateISBN.jsp
<%--
Document : validateIsbn
Created on : 2013-12-18, 9:00:45 Author : new --%>
<%@page contentType=\"text/html\" pageEncoding=\"utf-8\" errorPage=\"error.jsp\" import=\"java.sql.*,java.io.PrintWriter\"%> <%@include file=\"include.jsp\"%> <%! //验证isbn
String validateIsbnSql = \"select * from books where isbn=?\";
public boolean validateIsbn(String isbn) { Connection conn = null;
PreparedStatement prep = null; ResultSet rst = null; try {
conn = getConnection();
prep = conn.prepareStatement(validateIsbnSql); prep.setString(1, isbn); rst = prep.executeQuery(); if (rst.next()) { return true; } else {
return false; }
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println(\"驱动加载失败\"); return true;
} catch (SQLException ex) { ex.printStackTrace();
System.out.println(\"数据库查询失败\"); return true; } finally { try {
if (rst != null) { rst.close(); }
if (prep != null) { prep.close(); }
closeConnection();
} catch (SQLException ex) { ex.printStackTrace();
System.out.println(\"数据库关闭异常\"); return true; } } } %> <%
request.setCharacterEncoding(\"utf-8\"); response.setCharacterEncoding(\"utf-8\"); String isbn = request.getParameter(\"isbn\"); PrintWriter pw = response.getWriter(); pw.print(validateIsbn(isbn)); %>
Book_type.jsp
<%--
Document : book_type
Created on : 2013-12-27, 10:13:40 Author : new --%>
<%@page contentType=\"text/html\" pageEncoding=\"UTF-8\" errorPage=\"error.jsp\" import=\"net.sf.json.JSONArray,bms_bean.BookType,java.util.*,java.sql.*\"%> <%@include file=\"include.jsp\"%>
<%!
//查找到所有的书籍类型
String findBookTypeSql = \"select * from book_type\";
public List List PreparedStatement prep = null; ResultSet rst = null; try { conn = getConnection(); prep = conn.prepareStatement(findBookTypeSql); rst = prep.executeQuery(); BookType bookType = null; while (rst.next()) { bookType = new BookType(); bookType.setId(rst.getInt(\"id\")); bookType.setTypeName(rst.getString(\"type_name\")); bookTypes.add(bookType); } } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (SQLException ex) { ex.printStackTrace(); return null; } finally { try { if (rst != null) { rst.close(); } if (prep != null) { prep.close(); } closeConnection(); } catch (SQLException ex) { ex.printStackTrace(); return null; } } return bookTypes; } %> <% JSONArray bookTypeArr = JSONArray.fromObject(findAllBookType()); out.print(bookTypeArr.toString()); %> Books_list.jsp <%-- Document : books_list Created on : 2013-12-23, 17:01:49 Author : new --%> <%@page contentType=\"text/html\" pageEncoding=\"utf-8\" errorPage=\"error.jsp\" import=\"java.sql.*,java.util.*,bms_bean.*,com.sun.crypto.provider.RSACipher,net.sf.json.JSONArray\"%> <%@include file=\"include.jsp\"%> <%! int pageSize = 10; //根据条件动态构建查询到总页数的SQL语句 public String createPageCountSql(String bookName, String isbn, String author, Float price, Integer typeId, List
Copyright © 2019- zrrp.cn 版权所有 赣ICP备2024042808号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务