package com.eins.databasedemo.service;
import com.eins.databasedemo.controller.WindowController;
import com.eins.databasedemo.mapper.BooksMapper;
import com.eins.databasedemo.pojo.Books;
import com.eins.databasedemo.pojo.LendRecord;
import com.eins.databasedemo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.sql.*;
import java.util.List;
import java.util.Vector;
@Service
public class BooksService {
@Autowired
private BooksMapper booksMapper;
private Connection con = WindowController.con;
private PreparedStatement pstmt;
private String sql;
/**
* 添加图书
* @param books
*/
public void InsertBook(Books books) {
booksMapper.Insert(books);
}
/**
* 查询全部图书
* @return
*/
public List<Books> list() {
return booksMapper.list();
}
/**
* 查询出版社信息
* @return
*/
public Vector<String> getPublisher() {
return booksMapper.getPublisher();
}
/**
* 删除图书
* @param id
* @return
*/
public int deleteBooks(int id) {
try{
booksMapper.deleteBooksById(id);
} catch (Exception e) {
return -1;
}
return 0;
}
/**
* 检索图书
* @param con
* @param name
* @param author
* @param publisher
* @param radio
* @return
*/
public Vector<String> selectBooks(Connection con,String name, String author, String publisher, String radio) {
// Vector<String> books = new Vector<>();
// if(!name.equals("") && author.equals("")) {
// if(radio.equals("模糊查询")) {
// name = "%" + name + "%";
// }
// Vector<String> v = booksMapper.selectByName(name);
// }
// if(!author.equals("")) {
//
// }
this.con = con;
Vector<String> result = new Vector<String>();
if(publisher.equals("null"))publisher = "";
sql = "select * from books";
int flag = 0;
if (name.equals("")) {
if (author.equals("")) {
if (!publisher.equals("")) {
sql += " where publisher like ?";
flag = 1;
}
} else {
if (publisher.equals("")) {
sql += " where author like ?";
flag = 2;
} else {
sql += " where author like ? and publisher like ?";
flag = 3;
}
}
} else {
if (author.equals("") && publisher.equals("")) {
sql += " where name like ?";
flag = 4;
}
if (author.equals("") && !publisher.equals("")) {
sql += " where name like ? and publisher like ?";
flag = 5;
}
if (!author.equals("") && publisher.equals("")) {
sql += " where name like ? and author like ?";
flag = 6;
}
if (!author.equals("") && !publisher.equals("")) {
sql += " where name like ? and author like ? and publisher like ?";
flag = 7;
}
}
if (radio.equals("模糊查询")) {
name = "%" + name + "%";
author = "%" + author + "%";
publisher = "%" + publisher + "%";
}
try {
pstmt = con.prepareStatement(sql);
switch (flag) {
case 0:
break;
case 1:
pstmt.setString(1, publisher);
break;
case 2:
pstmt.setString(1, author);
break;
case 3:
pstmt.setString(1, author);
pstmt.setString(2, publisher);
break;
case 4:
pstmt.setString(1, name);
break;
case 5:
pstmt.setString(1, name);
pstmt.setString(2, publisher);
break;
case 6:
pstmt.setString(1, name);
pstmt.setString(2, author);
break;
case 7:
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
break;
}
ResultSet rs = pstmt.executeQuery();
String str = "";
while (rs.next()) {
for (int i = 1; i <= 10/* rs.getMetaData().getColumnCount() */; i++) {
if (i == 1 || i == 8 || i == 10) {
if (i == 1) {
str += "序号: " + rs.getInt(i) + "; ";
}
if (i == 8) {
str += "此书的总数量: " + rs.getInt(i) + "; ";
}
if (i == 10) {
str += "存放时间: " + rs.getDate(i) + " " + rs.getTime(i) + ";";
}
} else {
if (i == 2) {
str += "书号: ";
}
if (i == 3) {
str += "书名: ";
}
if (i == 4) {
str += "作者: ";
}
if (i == 5) {
str += "出版社: ";
}
if (i == 6) {
str += "出版日期: ";
}
if (i == 7) {
str += "位置: ";
}
if (i == 9) {
str += "借出数量: ";
}
str += rs.getString(i).trim() + "; ";
}
}
str = str.substring(0, str.length());
result.add(str);
str = "";
}
} catch (SQLException e) {
System.out.println("查询数据库出错");
System.out.println(e);
}
return result;
}
/**
* 修改图书
* @param bookInfo1
* @param id
* @return
*/
public int updateBook(Connection con,Vector<String> bookInfo1, String id) {
this.con = con;
int Id = Integer.parseInt(id);
//System.out.println(Id);
sql = "update books set isbn = ?, name = ?, author = ?, publisher = ?, pubDate = ?, deposit = ?, quantify = ?, deposit_time = ? where id = ?"; // 存储sql语句
try {
pstmt = con.prepareStatement(sql); // 预处理sql语句
for (int i = 1; i <= bookInfo1.size(); i++) {// 循环遍历得到录入内容
//System.out.println(bookInfo1.elementAt(i - 1).trim());
if (i == 7) { // 录入书本数量为整数,要转化为整数
pstmt.setInt(i, Integer.parseInt(bookInfo1.elementAt(i - 1).trim()));// elmentAt返回指定索引处的组件。
} else { // 录入内容为字符串
pstmt.setString(i, bookInfo1.elementAt(i - 1).trim());
}
}
pstmt.setInt(bookInfo1.size() + 1, Id);
pstmt.executeUpdate(); // 更新数据库
retu
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
数据库课程设计(图书馆管理系统)springboot+swing+mysql+mybatis (124个子文件)
BooksService.class 18KB
BooksController-副本20241121151115.class 14KB
BooksController.class 13KB
UpdateBooksNext.class 7KB
WindowController-副本20241121151115.class 7KB
InsertBooks.class 7KB
Books.class 6KB
WindowController.class 6KB
User.class 6KB
WindowController$RegisterAction.class 6KB
WindowController$RegisterAction-副本20241121151115.class 6KB
LendRecordBooks.class 6KB
ReturnRecordBooks.class 5KB
ReturnBooks.class 5KB
DeleteBooks.class 5KB
UpdateBooks.class 5KB
LendBooks.class 5KB
SelectBooks.class 5KB
BooksController$MyButtonEditor.class 4KB
LendRecord.class 4KB
LendBooks$LendListener.class 4KB
WindowController$RegisterAction$regisiteListener2-副本20241121151115.class 3KB
InsertBooks$SubmitListener.class 3KB
DeleteBooks$DeleteListener.class 3KB
WindowController$LoginAction-副本20241121151115.class 3KB
WindowController$LoginAction.class 3KB
WindowController$RegisterAction$regisiteListener2.class 3KB
UpdateBooksNext$Update1Listener.class 3KB
ReturnBooks$returnListener.class 3KB
BooksController$MyButtonEditor$3.class 3KB
LendRecordBooks$SelectListener.class 3KB
SelectBooks$SelectListener.class 3KB
DeleteBooks$SelectListener.class 3KB
UpdateBooks$SelectListener.class 3KB
LendBooks$SelectListener.class 3KB
ReturnRecordBooks$SelectListener.class 3KB
ReturnBooks$SelectListener.class 2KB
ReturnRecordBooks$returnRecordListener.class 2KB
LendRecordBooks$LendRecordListener.class 2KB
BooksController$2-副本20241121151115.class 2KB
BooksController$2.class 2KB
BooksController$MyButtonRender.class 2KB
UserService.class 2KB
BooksController$MyButtonEditor$1.class 2KB
UpdateBooks$UpdateListener.class 2KB
UpdateBooksNext$1.class 2KB
WindowController$RegisterAction$1.class 2KB
WindowController$RegisterAction$1-副本20241121151115.class 2KB
LendRecordBooks$2.class 2KB
LendRecordBooks$1.class 2KB
ReturnRecordBooks$1.class 2KB
UserMapper.class 2KB
SelectBooks$1-副本20241121151115.class 2KB
InsertBooks$1.class 2KB
ReturnBooks$1.class 2KB
UpdateBooks$1.class 2KB
DeleteBooks$1.class 2KB
LendBooks$1.class 1KB
BooksMapper.class 1KB
UpdateBooksNext$GetbackListener.class 1KB
BooksController$3.class 1KB
ReturnRecordBooks$GetbackListener.class 1KB
LendRecordBooks$GetbackListener.class 1KB
BooksController$3-副本20241121151115.class 1KB
UpdateBooks$GetbackListener.class 1KB
ReturnBooks$GetbackListener.class 1KB
SelectBooks$GetbackListener.class 1KB
DeleteBooks$GetbackListener.class 1KB
InsertBooks$GetbackListener.class 1KB
LendBooks$GetbackListener.class 1KB
SelectBooks$1.class 1KB
WindowController$RegisterAction$3-副本20241121151115.class 1KB
WindowController$RegisterAction$2.class 1KB
WindowController$RegisterAction$2-副本20241121151115.class 1KB
WindowController$RegisterAction$3.class 1KB
BooksController$MyButtonEditor$2.class 1KB
DatabaseDemoApplication.class 1KB
BooksController$4-副本20241121151115.class 893B
BooksController$4.class 893B
UpdateBooksNext$CancelListener.class 879B
InsertBooks$CancelListener.class 855B
BooksController$1.class 765B
BooksController$1-副本20241121151115.class 765B
DatabaseDemoApplicationTests.class 565B
.gitignore 184B
BooksService.java 31KB
BooksController.java 27KB
WindowController.java 18KB
UpdateBooksNext.java 12KB
InsertBooks.java 11KB
LendBooks.java 10KB
DeleteBooks.java 9KB
ReturnBooks.java 9KB
ReturnRecordBooks.java 8KB
UpdateBooks.java 8KB
LendRecordBooks.java 7KB
SelectBooks.java 7KB
UserMapper.java 2KB
UserService.java 2KB
BooksMapper.java 1KB
共 124 条
- 1
- 2
资源评论
yigan_Eins
- 粉丝: 248
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功