package com.study.bookManger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import com.mysql.jdbc.PreparedStatement;
import com.study.dao.BookDao;
import com.study.dao.BookTypeDao;
import com.study.model.Book;
import com.study.model.BookType;
import com.study.util.DBTool;
public class BookOperation {
Connection connetion ;
Book book;//图书
BookType booktype;//图书类别
BookDao bookDao = new BookDao();//图书处理
BookTypeDao bookTypeDao = new BookTypeDao();//图书类别处理
Scanner scanner = new Scanner(System.in);
ResultSet search;//查询返回结果
//----------------------------------图书类别---------------------------------
//1.图书类别添加
public void bookCategoryAdd() throws SQLException {
try {
System.out.println("请输入要添加的类型信息");
System.out.print("请输入类别名称:");
String str1 = scanner.next();
System.out.print("请输入描述信息:");
String str2 = scanner.next();
booktype = new BookType(str1, str2);
bookTypeDao.add(connetion, booktype);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//2.查询所有类别
public void bookCategoryAll() throws SQLException {
bookDao.limitShow(connetion, 1000, "图书类别表");
}
//3.搜索类别
public void bookCategorySearch() throws SQLException {
try {
System.out.println("请输入要搜索的类型信息");
String input = scanner.next();
search = bookTypeDao.search(connetion, input);
showListData(search);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//4.修改类别
public void bookCategoryUpdate() throws SQLException {
try {
bookCategoryAll();
System.out.print("请输入你想要修改的类别:");
String str1 = scanner.nextLine();
System.out.print("请输入你想要修改的类别名称:");
String str3 = scanner.nextLine();
System.out.print("请输入修改后的类别描述信息:");
String str2 = scanner.nextLine();
System.out.print("请输入修改后的id信息:");
int id = scanner.nextInt();
booktype = new BookType(id,str3, str2);
bookTypeDao.update(connetion, booktype,str1);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//5.删除类别
public void bookCategoryDelete() throws SQLException {
try {
System.out.println("请输入要删除的类型信息");
bookCategoryAll();
System.out.print("请输入类别名称:");
String input = scanner.next();
bookTypeDao.delete(connetion, input);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//
public void bookCategoryLimit() throws SQLException {
try {
System.out.println("你想要显示多少行");
int limit = scanner.nextInt();
bookDao.limitShow(connetion, limit, "图书类别表");
} catch (Exception e) {
// TODO: handle exception
}
}
//-----------------------------图书----------------------------------------------
// 1.添加图书
public void bookAdd() throws SQLException {
try {
System.out.println("请输入要添加的图书信息");
System.out.print("请输入图书名称:");
String bookName = scanner.next();
System.out.print("请输入图书作者:");
String author = scanner.next();
System.out.print("请输入作者性别:");
String sex = scanner.next();
System.out.print("请输入图书描述:");
String bookDesc = scanner.next();
System.out.print("请输入图书价格:");
Float price = scanner.nextFloat();
System.out.print("请输入图书类别ID:");
Integer bookTypeId = scanner.nextInt();
book = new Book(null, bookName, author,sex, price/1.0f, bookTypeId, bookDesc);
bookDao.add(connetion, book);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//2.查询所有图书
public void bookAll() throws SQLException {
bookDao.limitShow(connetion, 1000, "图书表");
}
// 3.搜索图书(作者)
public void bookSearchAuthor() throws SQLException {
try {
System.out.print("请输入要搜索的作者名称:");
String input = scanner.next();
System.out.println(input);
book = new Book(null,input, null, null, 0, null);
search = bookDao.search(connetion, book);
showListData(search);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//4.搜索图书(图书名)
public void bookSearch() throws SQLException {
try {
System.out.println("请输入要搜索的图书名称");
String input = scanner.next();
book = new Book(input,null, null, null, 0, null);
search = bookDao.search(connetion, book);
showListData(search);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//5.搜索图书(类别)
public void bookSearchCategory() throws SQLException {
try {
System.out.println("请输入你要搜索的类别");
String sql = "select * from t_bookType";
PreparedStatement prepareStatement = (PreparedStatement) connetion.prepareStatement(sql);
ResultSet executeQuery = prepareStatement.executeQuery();
while( executeQuery.next()) {
System.out.print(executeQuery.getObject(1)+"\t"+executeQuery.getObject(2)+"\n");
}
String next = scanner.next();
sql = "select id from t_bookType where bookTypeName like '%"+next+"%'";
prepareStatement = (PreparedStatement) connetion.prepareStatement(sql);
executeQuery = prepareStatement.executeQuery();
executeQuery.absolute(1);
search = bookDao.search(connetion, new Book(null, null, null, null, executeQuery.getInt(1), null));
showListData(search);
}
catch(SQLException e) {
System.out.println("哎呀,出错了,没有数据!");
}
}
//7.修改图书
public void bookUpdate() throws SQLException {
try {
bookAll();
System.out.print("请输入要修改的图书名称:");
String str= scanner.next();
System.out.print("请输入新的图书名称:");
String bookName = scanner.next();
System.out.print("请输入新的图书作者:");
String author = scanner.next();
System.out.print("请输入新的作者性别:");
String sex = scanner.next();
System.out.print("请输入新的图书描述:");
String bookDesc = scanner.next();
System.out.print("请输入新的图书价格:");
Float price = scanner.nextFloat();
System.out.print("请输入新的图书类别ID:");
Integer bookTypeId = scanner.nextInt();
System.out.print("请输入新的图书ID:");
Integer bookId = scanner.nextInt();
book = new Book(bookId, bookName, author, sex, price, bookTypeId, bookDesc);
bookDao.update(connetion, book,str);
} catch (Exception e) {
// TODO: handle exception
}
}
//8.删除图书
public void bookDelete() throws SQLException {
try {
System.out.println("请输入要删除的图书");
bookAll();
System.out.print("请输入图书名称:");
String input = scanner.next();
bookDao.delete(connetion, input);
}
catch (Exception e) {
System.out.println("输入错误");
}
}
//限制显示行数
public void bookLimit() throws SQLException {
try {
System.out.println("你想要显示多少行");
int limit = scanner.nextInt();
bookDao.limitShow(connetion, limit, "图书表");
} catch (Exception e) {
// TODO: handle exception
}
}
public Connection getConnetion() {
return connetion;
}
public void setConnetion(Connection connetion) {
this.connetion = connetion;
}
public void showListData(ResultSet search ) throws SQLException {
String str = null;
int col_num = se