public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
while (true) {
System.out.println("欢迎来到小王书城");
System.out.println("1.展示书籍");
System.out.println("2.上新书籍");
System.out.println("3.下架书籍");
System.out.println("4.退出应用");
//借助Scanner类:
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要的功能序号");
//利用键盘录入序号:
int choice = sc.nextInt();//键盘录入必须回车才能录入
//根据choice录入进行判断
if (choice == 1) {
//录入书籍编号
System.out.println("请输入你想要查看的书籍编号:");
int bno =sc.nextInt();
//根据编号查询书籍
Book b = findBookByBno(bno);
//通过b的结果判断书籍是否查到
if(b == null){
System.out.println("你想要查询的书籍不存在");
}
else{
System.out.println("当前查询到一本书: 《" + b.getName() + "》");
}
}
if (choice == 2) {
//查询所有书籍信息
ArrayList books = findBooks();
if(books.size() == 0){
System.out.println("没有查询到书籍");
}else{
for(int i = 0; i <= books.size()-1; i++ ){
Book b =(Book)(books.get(i));
System.out.println(b.getId() + "--" + b.getName());
}
}
}
if (choice == 3) {
//录入要删除的书籍编号
System.out.println("请录入你想要删除的书籍编号:");
int bno = sc.nextInt();
//删除指定书籍编号对应的书籍
int n = delBookByBno(bno);
if(n <= 0){
System.out.println("删除失败");
}else{
System.out.println("删除成功");
}
}
if (choice == 4) {
System.out.println("小王书城>>>>4.退出应用");
break;//退出
}
}
}
//根据编号查询书籍
public static Book findBookByBno(int bno) throws ClassNotFoundException, SQLException {
Book b = null;
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true";
String username = "root";
String password = "111111";
Connection conn = DriverManager.getConnection(url, username, password);
//创建会话
Statement sta = conn.createStatement();
//发送sql 加入结果集
ResultSet rs = sta.executeQuery("select * from t_book where id = " + bno);
//处理结果
if (rs.next()) {//如果结果只有一条,想要获取的话用if语句
//接收结果
int id = rs.getInt("id");
String name = rs.getString("name");
String author = rs.getString("author");
double price = rs.getDouble("price");
//封装为Book对象
b = new Book();
b.setId(id);
b.setName(name);
b.setAuthor(author);
b.setPrice(price);
}
//关闭资源
sta.close();
conn.close();
return b;
}
public static ArrayList findBooks() throws ClassNotFoundException, SQLException {
//定义集合
ArrayList list = new ArrayList();
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true";
String username = "root";
String password = "111111";
Connection conn = DriverManager.getConnection(url, username, password);
//创建会话
Statement sta = conn.createStatement();
//发送sql 加入结果集
ResultSet rs = sta.executeQuery("select * from t_book ");
//处理结果
while (rs.next()) {
//接收结果
int id = rs.getInt("id");
String name = rs.getString("name");
String author = rs.getString("author");
double price = rs.getDouble("price");
//封装为Book对象
Book b = new Book();
b.setId(id);
b.setName(name);
b.setAuthor(author);
b.setPrice(price);
//将书籍放入集合中
list.add(b);
}
//关闭资源
sta.close();
conn.close();
return list;
}
public static int delBookByBno(int bno) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true";
String username = "root";
String password = "111111";
Connection conn = DriverManager.getConnection(url, username, password);
//创建会话
Statement sta = conn.createStatement();
//发送sql
int n = sta.executeUpdate("delete from t_book where id = " + bno);
//关闭资源
sta.close();
conn.close();
return n;
}
}
05-27
3157