⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookpeer.java

📁 A JSP Based EShop for searching books and placing Orders
💻 JAVA
字号:
package eshop.model;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import eshop.beans.Book;public class BookPeer {	public static ArrayList searchBooks(DataManager dataManager,      String keyword) {		ArrayList<Book> books = new ArrayList<Book>();		Connection connection = dataManager.getConnection();		if (connection != null) {		  try {			  Statement s = connection.createStatement();			  String sql = "select book_id, title, author, price from books"					+ " where title like '%" + keyword.trim() + "%'"					+ " or author like '%" + keyword.trim() + "%'";        try {  			  ResultSet rs = s.executeQuery(sql);          try {  	  		  while (rs.next()) {    	  			Book book = new Book();	    	  		book.setId(rs.getString(1));		    	  	book.setTitle(rs.getString(2));			    	  book.setAuthor(rs.getString(3));		  		    book.setPrice(rs.getDouble(4));  		  		  books.add(book);	  		      }            }		  	  finally { rs.close(); }          }        finally { s.close(); }		    }      catch (SQLException e) {			  System.out.println("Could not search for books:" + e.getMessage());		    }      finally {        dataManager.putConnection(connection);        }      }		return books;	  }		public static ArrayList getBooksByCategory(DataManager dataManager,      String categoryId) {		ArrayList<Book> books = new ArrayList<Book>();		Connection connection = dataManager.getConnection();		if (connection != null) {  		try {	  		Statement s = connection.createStatement();		  	String sql = "select book_id, title, author, price from books"				  + " where category_id=" + categoryId;        try {  			  ResultSet rs = s.executeQuery(sql);          try {  	  		  while (rs.next()) {	    	  		Book book = new Book();		    	  	book.setId(rs.getString(1));			    	  book.setTitle(rs.getString(2));  			    	book.setAuthor(rs.getString(3));	  			    book.setPrice(rs.getDouble(4));		  		    books.add(book);			        }            }          finally { rs.close(); }          }        finally { s.close(); }		    }      catch (SQLException e) {        System.out.println("Could not get books: " + e.getMessage());        }      finally {        dataManager.putConnection(connection);        }      }		return books;	  }	public static Book getBookById(DataManager dataManager, String bookID) {    Book book = null;    Connection connection = dataManager.getConnection();		if (connection != null) {      try {        Statement s = connection.createStatement();        String sql = "select book_id, title, author, price from books"          + " where book_id=" + bookID;        try {          ResultSet rs = s.executeQuery(sql);          if (rs.next()) {            book = new Book();            book.setId(rs.getString(1));            book.setTitle(rs.getString(2));            book.setAuthor(rs.getString(3));            book.setPrice(rs.getDouble(4));            }          }        finally { s.close(); }        }      catch (SQLException e) {        System.out.println("Could not get book: " + e.getMessage());        }      finally {        dataManager.putConnection(connection);        }      }		return book;    }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -