📄 bookdao.java
字号:
/*
* 创建日期:2006-7-22 17:07:00
*
* 代码作者:万星
*
* 项目名称:eShop
*
* Copyright 2006 北京交通大学信息管理与信息系统系. All right reserved.
*/
package eshop.dataaccess;
import java.sql.*;
import java.util.*;
import eshop.common.*;
/**
* <h5>功能说明:</h5>
*
* <h5>对应需求:</h5>
*
*
* @version 1.0
* @author Administrator
*/
public class BookDAO
{
public BookPO getBookInfoFromDB(int intBookID)throws Exception
{
BookPO book=new BookPO();
Connection conn=DBConnection.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=null;
try
{
String strSQL="select * from T_BOOK,T_CATEGORY where " +
" T_CATEGORY.F_CATEGORYID=T_BOOK.F_CATEGORYID and" +
" F_BOOKID="+String.valueOf(intBookID);
rs=stmt.executeQuery(strSQL);
if(rs.next())
{
String strBookName=rs.getString("F_BOOKNAME");
String strAuthor=rs.getString("F_AUTHOR");
String strDesc=rs.getString("F_DESC");
String strPubDate=rs.getString("F_PUBDATE");
float flPrice=rs.getFloat("F_PRICE");
float flDisCount=rs.getFloat("F_DISCOUNT");
String strCategory=rs.getString("F_CATEGORY");
String strPress=rs.getString("F_PRESS");
String strPicture=rs.getString("F_PICTURE");
if(strAuthor==null) strAuthor="";
if(strDesc==null) strDesc="";
if(strPubDate==null) strPubDate="";
if(strPress==null) strPress="";
book.setBookName(strBookName);
book.setBookAuthor(strAuthor);
book.setBookDesc(strDesc);
book.setBookPubDate(strPubDate);
book.setBookPrice(flPrice);
book.setBookDisCount(flDisCount);
book.setBookCategory(strCategory);
book.setBookPress(strPress);
book.setBookPicture(strPicture);
}
else
{
book=null;
}
}
catch(SQLException e)
{
e.printStackTrace();
throw e;
}
finally
{
conn.close();
}
return book;
}
public Iterator getBookInfoFromDB()throws Exception
{
Collection clnBooks=new ArrayList();
Iterator itrBook=null;
Connection conn=DBConnection.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=null;
try
{
String strSQL="select top 4 * from T_BOOK,T_CATEGORY where " +
" T_CATEGORY.F_CATEGORYID=T_BOOK.F_CATEGORYID order by F_BOOKID desc";
rs=stmt.executeQuery(strSQL);
while(rs.next())
{
int intBookID=rs.getInt("F_BOOKID");
String strBookName=rs.getString("F_BOOKNAME");
String strAuthor=rs.getString("F_AUTHOR");
String strDesc=rs.getString("F_DESC");
String strPubDate=rs.getString("F_PUBDATE");
float flPrice=rs.getFloat("F_PRICE");
float flDisCount=rs.getFloat("F_DISCOUNT");
String strCategory=rs.getString("F_CATEGORY");
String strPress=rs.getString("F_PRESS");
String strPicture=rs.getString("F_PICTURE");
if(strAuthor==null) strAuthor="";
if(strDesc==null) strDesc="";
if(strPubDate==null) strPubDate="";
if(strPress==null) strPress="";
BookPO book=new BookPO();
book.setBookID(intBookID);
book.setBookName(strBookName);
book.setBookAuthor(strAuthor);
book.setBookDesc(strDesc);
book.setBookPubDate(strPubDate);
book.setBookPrice(flPrice);
book.setBookDisCount(flDisCount);
book.setBookCategory(strCategory);
book.setBookPress(strPress);
book.setBookPicture(strPicture);
clnBooks.add(book);
}
itrBook=clnBooks.iterator();
}
catch(SQLException e)
{
e.printStackTrace();
throw e;
}
finally
{
conn.close();
}
return itrBook;
}
public Iterator getBookInfoFromDB(int intBookNum,String returnMethod)throws Exception
{
Collection clnBooks=new ArrayList();
Iterator itrBook;
Connection conn=DBConnection.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=null;
try
{
String strOrderBy=null;
if(returnMethod.equals("Method1"))
{
strOrderBy="ORDER BY F_PRICE*(1-F_DISCOUNT) DESC";
}
else if
(returnMethod.equals("Method2"))
{
strOrderBy="ORDER BY F_DISCOUNT DESC";
}
String strSQL="select top "+String.valueOf(intBookNum)+" * from T_BOOK " +strOrderBy;
rs=stmt.executeQuery(strSQL);
int intNum=0;
while(rs.next())
{
int intBookID=rs.getInt("F_BOOKID");
String strBookName=rs.getString("F_BOOKNAME");
float flPrice=rs.getFloat("F_PRICE");
float flDisCount=rs.getFloat("F_DISCOUNT");
BookPO book=new BookPO();
book.setBookID(intBookID);
book.setBookName(strBookName);
book.setBookPrice(flPrice);
book.setBookDisCount(flDisCount);
intNum++;
if(intNum>intBookNum)
{
break;
}
clnBooks.add(book);
}
itrBook=clnBooks.iterator();
}
catch(SQLException e)
{
e.printStackTrace();
throw e;
}
finally
{
conn.close();
}
return itrBook;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -