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

📄 bookbean.java

📁 JSP的在线书店的所有源代码 使用MyEclipse开发工具
💻 JAVA
字号:
/*  *setBookISBN():设置图书的编号,同时根据编号更新相应的书名、作者、出版社、价格*和简介
 *getBookList()— 取得书库中全部书的书名、出版社、价格、作者等信息;
 *getBookISBN()— 取得当前图书的编号 ;
 *getBookName()—取得当前图书的书名;
 *getBookAuthor()—取得当前图书的作者;
 *getPublisher()—取得当前图书的出版社信息;
 *getPrice()—取得当前图书的价格;
 *getIntroduce()取得当前图书的简介信息。
 *main()方法用于将BEAN作为一个 Application进行测试时使用,正式发布时可以删除。
 **/

package wdm;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookBean {
	private String bookISBN = null; // 图书编号

	private String bookName = null; // 书名

	private String bookAuthor = null; // 作者

	private String publisher = null; // 出版社

	private String introduce = null; // 简介

	private String price = null; // 价格

	private static String strDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

	private static String strDBUrl = "jdbc:odbc:bookstore";

	private Connection conn = null;

	private ResultSet rs = null;

	public BookBean() {
		// 加载驱动
		try {
			Class.forName(strDBDriver);
		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("BookBean ():" + e.getMessage());
			System.err.println(11);
		}
	}

	// 取当前书库中全部图书信息
	public ResultSet getBookList() {
		String strSql = null;
		try {
			// 建立与数据库的连接
			conn = DriverManager.getConnection(strDBUrl);
			Statement stmt = conn.createStatement();
			strSql = "Select bookISBN,bookName,bookAuthor,publisher,price from bookInfo ";
			rs = stmt.executeQuery(strSql);
		}
		// 捕获异常
		catch (SQLException e) {
			System.err.println("BookBean.getBookList():" + e.getMessage());
		}
		return rs;
	}

	// 根据图书的编号给图书的其他信息赋值
	private void getBookInfo(String ISBN) {
		String strSql = null;
		bookName = null;
		bookAuthor = null;
		publisher = null;
		introduce = null;
		price = null;
		try {
			// 建立和数据库的连接
			conn = DriverManager.getConnection(strDBUrl);
			Statement stmt = conn.createStatement();
			strSql = "Select * from bookInfo where bookISBN = '" + ISBN + "'";
			rs = stmt.executeQuery(strSql);
			while (rs.next()) {
				bookName = rs.getString("bookName");
				bookAuthor = rs.getString("bookAuthor");
				publisher = rs.getString("publisher");
				introduce = rs.getString("introduce");
				price = rs.getString("price");
			}
		}
		// 捕获异常
		catch (SQLException e) {
			System.err.println("BookBean.getBookInfo():" + e.getMessage());
		}
	}

	// 给图书的编号赋值,同时调用函数给图书的其他信息赋值
	public void setBookISBN(String ISBN) {
		this.bookISBN = ISBN;
		getBookInfo(bookISBN);
	}

	// 取图书编号
	public String getBookISBN() {
		return bookISBN;
	}

	// 取书名
	public String getBookName() {
		return bookName;
	}

	// 取作者信息
	public String getBookAuthor() {
		return bookAuthor;
	}

	// 取出版社信息
	public String getPublisher() {
		return publisher;
	}

	// 取图书简介
	public String getIntroduce() {
		return introduce;
	}

	// 取图书价格
	public String getPrice() {
		return price;
	}

	/*
	 * 将Bean作为一个application进行测试用 public static void main(String args[]) {
	 * BookBean book = new BookBean(); book.setBookISBN("7-5053-5316-4");
	 * 
	 * System.out.println(book.getBookName());
	 * System.out.println(book.getBookAuthor());
	 * System.out.println(book.getPublisher());
	 * System.out.println(book.getIntroduce());
	 * System.out.println(book.getPrice()); try { ResultSet tmpRS =
	 * book.getBookList(); while (tmpRS.next()) {
	 * System.out.println(tmpRS.getString("bookname")); } tmpRS.close(); } //
	 * 捕获异常 catch (Exception e) { System.err.println("main()" + e.getMessage()); } }
	 */
	public List getAllBook() {
		Statement stmt;
		String strSql = "";
		BookBean book = null;// new UserBean();
		List list = new ArrayList();
		try {
			conn = DriverManager.getConnection(strDBUrl);
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);

			strSql = "select * from bookInfo";
			// System.out.println(strSql);
			ResultSet rs = stmt.executeQuery(strSql);
			while (rs.next()) {
				book = new BookBean();
				book.setBookISBN(rs.getString("bookISBN"));
				book.setBookName(rs.getString("bookName"));
				book.setBookAuthor(rs.getString("bookAuthor"));
				book.setIntroduce(rs.getString("introduce"));
				book.setPrice(rs.getString("price"));
				book.setPublisher(rs.getString("publisher"));

				list.add(book);
			}

		} catch (Exception e) {
			System.out.println(e);
			// return false;
		}
		return list;
	}
	
	public void del(String bookISBN) {
		Statement stmt;
		String strSql = "";
		try {
			conn = DriverManager.getConnection(strDBUrl);
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);

			strSql = "delete from bookInfo where bookISBN='" + bookISBN + "'";
			stmt.executeUpdate(strSql);

		} catch (Exception e) {
			System.out.println(e);
			// return false;
		}
	}

	public void add(BookBean book){
		Statement stmt;
		String strSql = "";
		try {
			conn = DriverManager.getConnection(strDBUrl);
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);

			strSql = "insert into bookInfo values('"+book.getBookISBN()+
			"','"+book.getBookName()+
			"','"+book.getBookAuthor()+
			"','"+book.getPublisher()+
			"','"+book.getPrice()+
			"','"+book.getIntroduce()+"')";
			
			System.out.println(strSql);
			
			stmt.executeUpdate(strSql);

		} catch (Exception e) {
			System.out.println(e);
			// return false;
		}
	}

	public static String getStrDBDriver() {
		return strDBDriver;
	}

	public static void setStrDBDriver(String strDBDriver) {
		BookBean.strDBDriver = strDBDriver;
	}

	public static String getStrDBUrl() {
		return strDBUrl;
	}

	public static void setStrDBUrl(String strDBUrl) {
		BookBean.strDBUrl = strDBUrl;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public ResultSet getRs() {
		return rs;
	}

	public void setRs(ResultSet rs) {
		this.rs = rs;
	}

	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

}

⌨️ 快捷键说明

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