From 5c1eae8353a734623e5ab855722692f8573147f6 Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Mon, 1 Jul 2024 22:55:09 +0800 Subject: [PATCH] a --- src/client/Client.java | 232 +++++++++++++++++++++++++++++--- src/pa/dangdang/Driver.java | 4 +- src/pa/dangdang/NewsThread.java | 4 +- 3 files changed, 217 insertions(+), 23 deletions(-) diff --git a/src/client/Client.java b/src/client/Client.java index 11e29c7..26c0db6 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -8,6 +8,10 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; import java.util.HashMap; import java.util.Map; import java.util.Scanner; @@ -17,6 +21,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import pa.dangdang.Book; +import pa.dangdang.Driver; +import server.tools.DBConnection; +import server.tools.ExportToCSV; +import server.tools.ExportToXLS; import server.vo.User; public class Client { @@ -28,6 +37,11 @@ public class Client { public static PrintWriter write; // 字符输出流 public static User currentUser; // 当前登录用户 + public static Connection con = DBConnection.getConnection(); + public static PreparedStatement ps = null; + public static Statement statement = null; + public static ResultSet rs = null; + // 主方法 public static void main(String[] args) throws Exception, IOException { String ip = "127.0.0.1"; // 服务器IP @@ -49,14 +63,26 @@ public class Client { int choose = menu(); while (choose != 8) { // 不是退出 switch (choose) { - case 1: // 修改密码 - updatepassword(); + case 1: + display(); break; - case 2:// 修改密码 - + case 2:// 当当网站爬取 + Driver.crawl(); break; - case 3:// 增加数据:显示二级菜单 - + case 3:// 查询 + query(); + break; + case 4: + delete(); + break; + case 5: + update(); + break; + case 6: + ExportToCSV.to(); + break; + case 7: + ExportToXLS.to(); break; default: break; @@ -134,16 +160,16 @@ public class Client { // 显示操作界面,返回用户的选择1,2,..... public static int menu() { - System.out.println("======新闻管理系统======="); + System.out.println("======图书管理系统======="); System.out.println("作者:****&******"); System.out.println("当前用户:" + currentUser.getName()); - System.out.println("1.密码修改"); - System.out.println("2.增加数据"); // 需要增加二级菜单:【键盘录入,网页爬取,txt文件导入,xls文件导入】 - System.out.println("3.修改数据"); // 按key修改其他字段 - System.out.println("4.删除数据"); // 按key删除记录 - System.out.println("5.查询数据"); // 需要增加二级菜单,【自动设计查询条件】 - System.out.println("6.统计数据"); // 需要增加二级菜单,【自行设计统计条件】 - System.out.println("7.导出数据至文件"); // 需要增加二级菜单:【导出至txt,导出至xls】 + System.out.println("1.图书信息现实"); + System.out.println("2.当当网站爬取"); // 需要增加二级菜单:【键盘录入,网页爬取,txt文件导入,xls文件导入】 + System.out.println("3.查询"); // 按key修改其他字段 + System.out.println("4.删除"); // 按key删除记录 + System.out.println("5.修改"); // 需要增加二级菜单,【自动设计查询条件】 + System.out.println("6.导出到csv文件"); // 需要增加二级菜单,【自行设计统计条件】 + System.out.println("7.导出到xls文件"); // 需要增加二级菜单:【导出至txt,导出至xls】 System.out.println("8.退出"); System.out.println("请输入选项(1-8):"); @@ -163,17 +189,185 @@ public class Client { } // 修改密码 - public static void updatepassword() { + public static void display() { + String sql = "select * from book limit 10"; + try { + statement = con.createStatement(); + rs = statement.executeQuery(sql); + System.out.println(111); + while (rs.next()) { + // System.out.println(rs.getString(1)); + Book book = new Book(); + book.setTitle(rs.getString(1)); + + book.setAuthor(rs.getString(2)); + book.setPublisher(rs.getString(3)); + book.setOldprice(rs.getDouble(4)); + book.setNewprice(rs.getDouble(5)); + + book.setHref(rs.getString(6)); + // System.out.println(book.getHref()); + System.out.println(book.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } } // 从键盘输入 - public static void inputFromKeyboard() { - + public static void query() { + String sql = null; + System.out.println("======查询图书======="); + System.out.println("1.按名称查询"); + System.out.println("2.按作者查询"); // 需要增加二级菜单:【键盘录入,网页爬取,txt文件导入,xls文件导入】 + System.out.println("3.按出版社查询"); // 按key修改其他字段 + System.out.println("4.按价格查询"); // 按key删除记录 + System.out.println("5.退出"); + System.out.println("请输入选项(1-5):"); + Scanner scanner = new Scanner(System.in); + int n = 0; + n = scanner.nextInt(); + switch (n) { + case 1:// 名称查询 + sql = "select * from book where title like ?"; + System.out.println("请输入名称:"); + outputQuery(sql); + break; + case 2:// 作者查询 + sql = "select * from book where author like ?"; + System.out.println("请输入作者:"); + outputQuery(sql); + break; + case 3:// 出版社查询 + sql = "select * from book where publisher like ?"; + System.out.println("请输入出版社:"); + outputQuery(sql); + break; + case 4:// 价格查询 + sql = "select * from book where newprice like ?"; + System.out.println("请输入价格:"); + outputQuery(sql); + break; + default: + break; + } } - // 将电子表格文件编码为base64形式的字符串进行传输 - public static void importFromXLS() { + public static void outputQuery(String sql) { + Scanner scanner = new Scanner(System.in); + String title, author, pubilsher, oldprice, newprice, url; + String tianchong = null; + tianchong = scanner.next(); + // Connection con = DBConnection.getConnection(); + // PreparedStatement ps =null; + // ResultSet rs=null; + try { + ps = con.prepareStatement(sql); + ps.setString(1, '%' + tianchong + '%'); + rs = ps.executeQuery(); + while (rs.next()) { + title = rs.getString(1); + author = rs.getString(2); + pubilsher = rs.getString(3); + oldprice = rs.getString(4); + newprice = rs.getString(5); + url = rs.getString(6); + System.out.println("书名:" + title); + System.out.println("作者:" + author); + System.out.println("出版社:" + pubilsher); + System.out.println("原价格:" + oldprice); + System.out.println("折后价格:" + newprice); + System.out.println("详情地址:" + url); + } + rs.close(); + DBConnection.close(con, ps); + } catch (Exception e) { + e.printStackTrace(); + } + } + public static void delete() { + System.out.println("请输入书名"); + Scanner sc = new Scanner(System.in); + String bookname = sc.next(); + String sql = "delete from book where title like ?"; + try { + ps = con.prepareStatement(sql); + ps.setString(1, '%' + bookname + '%'); + ps.executeUpdate(); + DBConnection.close(con, ps); + System.out.println("删除成功"); + } catch (Exception e) { + System.err.println("删除失败"); + } + } + + public static void update() { + String sql = null; + Scanner sc = new Scanner(System.in); + System.out.println("======修改图书======="); + System.out.println("1.修改作者姓名"); + System.out.println("2.修改出版社"); // 需要增加二级菜单:【键盘录入,网页爬取,txt文件导入,xls文件导入】 + System.out.println("3.修改价格"); // 按key修改其他字段 + System.out.println("4.退出"); + System.out.println("请输入需要修改的图书的名称"); + String title = sc.next(); + + System.out.println("请输入选项(1-4):"); + Scanner scanner = new Scanner(System.in); + int n = 0; + n = scanner.nextInt(); + String name = null; + switch (n) { + case 1:// + System.out.println("请输入修改后的作责姓名"); + name = sc.next(); + sql = "update book set author = ? where title = ?"; + try { + ps = con.prepareStatement(sql); + ps.setString(1, name); + ps.setString(2, title); + ps.executeUpdate(); + DBConnection.close(con, ps); + } catch (Exception e) { + e.printStackTrace(); + } + break; + case 2: + System.out.println("请输入修改后的出版社名"); + name = sc.next(); + sql = "update book set publisher = ? where title = ?"; + try { + ps = con.prepareStatement(sql); + ps.setString(1, name); + ps.setString(2, title); + ps.executeUpdate(); + DBConnection.close(con, ps); + } catch (Exception e) { + e.printStackTrace(); + } + break; + case 3: + System.out.println("请输入修改后的价格"); + name = sc.next(); + sql = "update book set oldprice = newprice where title = ?"; + String sql2 = "update book set newprice = ? where title = ?"; + try { + ps = con.prepareStatement(sql); + ps.setString(1, title); + ps.executeUpdate(); + ps = con.prepareStatement(sql2); + ps.setString(1, name); + ps.setString(2, title); + ps.executeUpdate(); + DBConnection.close(con, ps); + } catch (Exception e) { + e.printStackTrace(); + } + break; + default: + break; + } } } diff --git a/src/pa/dangdang/Driver.java b/src/pa/dangdang/Driver.java index cd1d703..7129fb1 100644 --- a/src/pa/dangdang/Driver.java +++ b/src/pa/dangdang/Driver.java @@ -1,7 +1,7 @@ package pa.dangdang; public class Driver { - public static void pashu() { + public static void crawl() { try { String url = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1"; Thread thread1 = new Thread(new NewsThread(url)); @@ -15,7 +15,7 @@ public class Driver { Thread.sleep(10000); } } catch (Exception e) { - + e.printStackTrace(); } } diff --git a/src/pa/dangdang/NewsThread.java b/src/pa/dangdang/NewsThread.java index 0338dc4..e616ec3 100644 --- a/src/pa/dangdang/NewsThread.java +++ b/src/pa/dangdang/NewsThread.java @@ -64,7 +64,7 @@ public class NewsThread implements Runnable { ps.setString(6, href); ps.executeUpdate(); } catch (Exception e) { - + e.printStackTrace(); } System.out.println(title); System.out.println(author); @@ -77,7 +77,7 @@ public class NewsThread implements Runnable { ps.close(); con.close(); } catch (Exception e) { - + e.printStackTrace(); } }