package test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Book implements IAction {
private String id;
private String bookname;
private String booktype;
private String author;
private String translator;
private String publisher;
private Date publish_time;
private double price;
private int stock;
public static Connection conn;
public static Statement stmt;
public static ResultSet rs;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTranslator() {
return translator;
}
public void setTranslator(String translator) {
this.translator = translator;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Date getPublish_time() {
return publish_time;
}
public void setPublish_time(Date publish_time) {
this.publish_time = publish_time;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return "图书编号:" + id + ", 图书名:" + bookname + ", 图书类型:"
+ booktype + ", 作者:" + author
+ ", 翻译:" + translator + ", 出版社:" + publisher + ", 出版时间:"
+ new SimpleDateFormat("yyyy-MM-dd").format(publish_time)
+ ", 价格:" + price + ", 库存:" + stock + ".";
}
public Book() throws SQLException {
}
public Book(String id, String bookname, String booktype,
String author, String translator, String publisher, Date publish_time, double price, int stock) throws SQLException {
this.id = id;
this.bookname = bookname;
this.booktype = booktype;
this.author = author;
this.translator = translator;
this.publisher = publisher;
this.publish_time = publish_time;
this.price = price;
this.stock = stock;
}
@Override
public boolean add() {
boolean flag = false;
Scanner scanner = new Scanner(System.in);
System.out.print("输入图书编号:");
String id = scanner.nextLine();
System.out.print("输入图书名称");
String bookname = scanner.nextLine();
System.out.print("输入图书类别");
String booktype = scanner.nextLine();
System.out.print("输入图书作者");
String author = scanner.nextLine();
System.out.print("输入图书译者");
String translator = scanner.nextLine();
System.out.print("输入图书出版社");
String publisher = scanner.nextLine();
System.out.print("输入图书定价");
double price = scanner.nextDouble();
System.out.print("输入图书库存数量");
int stock = scanner.nextInt();
Date date = new Date();
String insertBookSql = "insert into book values('" + id + "','" + bookname + "','" + booktype + "','" + author + "','" + translator
+ "','" + publisher + "','" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "','" + price + "','" + stock + "')";
try {
conn = JdbcUtils.getConnection();
stmt = conn.createStatement();
int i = stmt.executeUpdate(insertBookSql);
if (i > 0) {
flag = true;
System.out.println(bookname + "已经添加成功");
}
JdbcUtils.closeAll(rs, stmt, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
@Override
public boolean del() {
boolean delFlag = false;
System.out.print("输入你要删除图书的编号:");
String num = new Scanner(System.in).nextLine();
String lookBookSql = "select * from book";
String delBookSql = "delete from book where id='" + num + "'";
try {
conn = JdbcUtils.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(lookBookSql);
while (rs.next()) {
stmt = conn.createStatement();
String id = rs.getString("id");
if (id.equals(num)) {
int i = stmt.executeUpdate(delBookSql);
if (i > 0) {
System.out.println("删除成功!");
delFlag = true;
} else {
System.out.println("删除失败!");
}
}
}
JdbcUtils.closeAll(rs, stmt, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return delFlag;
}
@Override
public int getCount() {
int count = 0;
String countBookSql = "select count(*) from book";
try {
conn = JdbcUtils.getConnection();
stmt = conn.createStatement();
count = stmt.executeUpdate(countBookSql);
System.out.println("总共有:" + count + "本书");
JdbcUtils.closeAll(rs, stmt, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
@Override
public void getListInfo() {
String bookInfoSql = "select * from book";
try {
conn = JdbcUtils.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(bookInfoSql);
while (rs.next()) {
String id = rs.getString("id");
String bookname = rs.getString("bookname");
String booktype = rs.getString("booktype");
String author = rs.getString("author");
String translator = rs.getString("translator");
String publisher = rs.getString("publisher");
Date publish_time = rs.getDate("publish_time");
double price = rs.getDouble("price");
int stock = rs.getInt("stock");
Book book = new Book(id, bookname, booktype, author, translator, publisher, publish_time, price, stock);
System.out.println(book.toString());
}
JdbcUtils.closeAll(rs, stmt, conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
@Override
public boolean find() {
boolean findFlag = false;
System.out.print("输入你要查询图书的编号:");
String findId = new Scanner(System.in).nextLine();
String lookBookSql = "select * from book";
try {
conn = JdbcUtils.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(lookBookSql);
while (rs.next()) {
String id = rs.g
![avatar](https://profile-avatar.csdnimg.cn/f5e1723868d0472797964aa18be83440_weixin_44811903.jpg!1)
火飞鹰
- 粉丝: 69
- 资源: 3
最新资源
- 基于Python和多种前端技术的stable-diffusion-webui AI绘画设计源码
- BLDC无刷直流电机Matlab仿真:转速电流双闭环控制及无感反电动势过零换相方式探究,BLDC无刷直流电机Matlab仿真:转速电流双闭环控制及无感反电动势过零换相方式研究,BLDC无刷直流电机ma
- 基于MATLAB的改进量子遗传算法多变量函数寻优代码详解:动态调整量子旋转门提升计算精度,基于Matlab的改进量子遗传算法在多变量函数寻优中的应用,旋转门调整精细化处理程序,基于matlab的改进的
- 基于Java和Vue的RuoYi-Vue新露营项目设计源码
- 三菱电梯LEHY-Pro:解析地址码的奥秘与应用,三菱LEHY-Pro电梯系列:专业级地址码解析与应用指南,三菱电梯LEHY-Pro电梯地址码 ,三菱电梯; LEHY-Pro; 电梯地址码,三菱电梯L
- 基于Java开发的1001上课代码设计源码
- 基于2024年春季学期C语言学习记录的bite代码设计与实现
- 基于改进灰狼算法IGWO的LSSVM回归预测建模系统-实现自适应权重初始化、数据替换及多图可视化误差分析,基于IGWO优化LSSVM回归预测建模的改进算法-初始化优化与自适应权重策略研究,改进灰狼
- 基于HTML的Ajax前端学习设计与源码示例
- 基于Java语言的公益性同城拼车小程序设计源码
- 飞度电感均衡技术解析:三节电池控制逻辑与Sfunction逻辑运算详解,飞度电感均衡技术在三节电池中的应用:控制逻辑与Sfunction详解及逻辑运算解析,飞度电感均衡 三节电池为例 内附控制逻辑 s
- 基于SSM框架的Java+JavaScript+CSS小区物业信息管理系统设计源码
- 基于TypeScript的Cocos行为转向实现与优化设计源码
- 基于Vue框架的DIY商城网站设计源码
- 基于Objective-C的兼职APP设计源码,支持C与MATLAB多语言开发
- 基于Java Server Pages的goldenArches项目设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)