📄 bookdao.java
字号:
package cn.dang.dao;
import ghy_db.DB_operate_interface;
import java.util.ArrayList;
import java.util.List;
import cn.dang.entity.Author;
import cn.dang.entity.Book;
import cn.dang.entity.BookType;
import cn.dang.entity.Concem;
public class BookDAO {
//创建数据库操作通用类实现接口引用
DB_operate_interface dbRef;
/**
* @param DB_operate_interface 获得DB_operate_interface引用
*/
public BookDAO(DB_operate_interface dbRef){
super();
this.dbRef=dbRef;
}
public void nonQueryBook(String sql,ArrayList values_list) throws Throwable{
//插入Book信息
dbRef.update(sql,values_list);
}
public List getBookInfoOne(String sql) throws Throwable{
List list=dbRef.query(sql);
return list;
}
//查询一本书的信息
public Book getBookInfo(String sql,Book book) throws Throwable{
List list=dbRef.query(sql);
if(list.size()>0){
String[] str=(String[])list.get(0);
book.setId(Integer.parseInt(str[0]));
book.setBookName(str[1]);
book.getConcem().setCid(Integer.parseInt(str[2]));
book.setIsbn(str[3]);
book.setPublishTime(str[4]);
book.setPrice(Double.parseDouble(str[5]));
book.setClickCount(Integer.parseInt(str[6]));
book.setFree(Double.parseDouble(str[7]));
book.setDescription(str[8]);
book.getAuthor().setAid(Integer.parseInt(str[9]));
book.setPoint(Integer.parseInt(str[10]));
book.setCover("D:\\"+str[11]);
book.getBookType().setId(Integer.parseInt(str[12]));
}
return book;
}
public List<Book> queryBooksInfo(String sql) throws Throwable{
//从db包中连接mysql数据库的类中查询出book表的所有数据
List list=dbRef.query(sql);
//创建一个放置Book类对象的泛型容器
List<Book> books=new ArrayList<Book>();
//循环遍历结合把每一行数据用String[]的数组保存
for(int i=0;i<list.size();i++){
String[] ts=(String[])list.get(i);
//创建Book类型的对象,添加数据到对象中,并把对象防入容器中
Book book=new Book();
Concem concem=new Concem();
Author author=new Author();
BookType bt=new BookType();
book.setId(Integer.parseInt(ts[0]));
book.setBookName(ts[1]);
concem.setCid(Integer.parseInt(ts[2]));
book.setConcem(concem);
book.setIsbn(ts[3]);
book.setPublishTime(ts[4]);
book.setPrice(Double.parseDouble(ts[5]));
book.setClickCount(Integer.parseInt(ts[6]));
book.setFree(Double.parseDouble(ts[7]));
book.setDescription(ts[8]);
author.setAid(Integer.parseInt(ts[9]));
book.setAuthor(author);
book.setPoint(Integer.parseInt(ts[10]));
book.setCover(ts[11]);
bt.setId(Integer.parseInt(ts[12]));
book.setBookType(bt);
books.add(book);
}
return books;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -